forked from thibaultcha/lua-cassandra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintro.lua
More file actions
118 lines (95 loc) · 3.09 KB
/
Copy pathintro.lua
File metadata and controls
118 lines (95 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
--------------------------------
-- Single host module, plain Lua
--------------------------------
local cassandra = require "cassandra"
local client = assert(cassandra.new {
host = "127.0.0.1",
keyspace = "my_keyspace"
})
client:settimeout(1000)
assert(client:connect())
local res = assert(client:execute [[
CREATE TABLE users(
id uuid PRIMARY KEY,
name varchar,
age int
)
]])
print(res.type) -- "SCHEMA_CHANGE"
res = assert(client:execute("INSERT INTO users(id, name, age) VALUES(?, ?, ?)", {
cassandra.uuid("1144bada-852c-11e3-89fb-e0b9a54a6d11"),
"John O Reilly",
42
}))
print(res.type) -- "VOID"
local rows = assert(client:execute("SELECT * FROM users WHERE age = ?", {
age = 42 -- key/value args
}, {
named = true -- named arguments
}))
print(rows.type) -- "ROWS"
print(#rows) -- 1
print(rows[1].name) -- "John O Reilly"
client:close()
-------------------------------------------------------------------------------
-- Cluster module, OpenResty
-- This approach allows the cluster to live as an upvalue in your module's main
-- chunk, assuming the `lua_code_cache` directive is enabled in your nginx
-- config. This approach will be the most efficient as it will avoid re-creating
-- the cluster variable on each request and will preserve the cached state of
-- your load-balancing policy and prepared statements directly in the Lua land.
-------------------------------------------------------------------------------
--
-- my_module.lua
--
local cassandra = require "cassandra"
local Cluster = require "resty.cassandra.cluster"
-- cluster instance as an upvalue
local cluster
local _M = {}
function _M.init_cluster(...)
cluster = assert(Cluster.new(...))
-- we also retrieve the cluster's nodes informations early, to avoid
-- slowing down our first incoming request, which would have triggered
-- a refresh should this not be done already.
assert(cluster:refresh())
end
function _M.execute(...)
return cluster:execute(...)
end
return _M
--
-- nginx.conf
--
http {
lua_shared_dict cassandra 1m; # shm storing cluster information
lua_code_cache on; # ensure the upvalue is preserved beyond a single request
init_by_lua_block {
-- will trigger a refresh of the cluster before the first request, but requires
-- LuaSocket since cosockets are not available in the 'init_by_lua' context.
local my_module = require "my_module"
my_module.init_cluster {
shm = "cassandra", -- defined in http block
contact_points = {"127.0.0.1", "127.0.0.2"},
keyspace = "my_keyspace"
}
}
server {
location / {
content_by_lua_block {
local my_module = require "my_module"
local rows, err = my_module.execute("SELECT * FROM users WHERE id = ? AND name = ?", {
cassandra.uuid("1144bada-852c-11e3-89fb-e0b9a54a6d11"),
"John O Reilly"
})
if not rows then
ngx.log(ngx.ERR, "could not retrieve users: ", err)
ngx.exit(500)
end
for i, row in ipairs(rows) do
ngx.say(i, ": ", rows[i].name) -- "1: John O Reilly"
end
}
}
}
}