在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):jbochi/lua-resty-cassandra开源软件地址(OpenSource Url):https://github.com/jbochi/lua-resty-cassandra开源编程语言(OpenSource Language):Lua 94.5%开源软件介绍(OpenSource Introduction):This repo is not being actively maintained. I highly recommend that you try lua-cassandra lua-resty-cassandraPure Lua Cassandra client using CQL binary protocol v2. It is 100% non-blocking if used in Nginx/Openresty but can also be used with luasocket. InstallationLuarocksInstallation through luarocks is recommended: $ luarocks install cassandra ManualCopy the UsageOverview: local cassandra = require "cassandra"
local session = cassandra.new()
session:set_timeout(1000) -- 1000ms timeout
local connected, err = session:connect("127.0.0.1", 9042)
session:set_keyspace("lua_tests")
-- simple query
local table_created, err = session:execute [[
CREATE TABLE users(
user_id uuid PRIMARY KEY,
name varchar,
age int
)
]]
-- query with arguments
local ok, err = session:execute([[
INSERT INTO users(name, age, user_id) VALUES(?, ?, ?)
]], {"John O'Reilly", 42, cassandra.uuid("1144bada-852c-11e3-89fb-e0b9a54a6d11")})
-- select statement
local users, err = session:execute("SELECT name, age, user_id from users")
assert(1 == #users)
local user = users[1]
ngx.say(user.name) -- "John O'Reilly"
ngx.say(user.user_id) -- "1144bada-852c-11e3-89fb-e0b9a54a6d11"
ngx.say(user.age) -- 42 You can check more examples in the tests or here. Socket methodssession, err = cassandra.new()Creates a new session. Create a socket with the cosocket API if available, fallback on luasocket otherwise.
session:set_timeout(timeout)Sets timeout (in miliseconds). Uses Nginx tcpsock:settimeout.
ok, err = session:connect(contact_points, port)Connects to a single or multiple hosts at the given port.
ok, err = session:set_keepalive(max_idle_timeout, pool_size) -- Nginx onlyPuts the current Cassandra connection immediately into the ngx_lua cosocket connection pool. Note: Only call this method in the place you would have called the close method instead. Calling this method will immediately turn the current cassandra session object into the closed state. Any subsequent operations other than connect() on the current objet will return the closed error.
times, err = session:get_reused_times() -- Nginx onlyThis method returns the (successfully) reused times for the current connection. In case of error, it returns Note: If the current connection does not come from the built-in connection pool, then this method always returns
ok, err = session:close()Closes the current connection and returns the status.
Client methodsAll errors returned by functions in this section are tables with the following properties:
Error tables implement the ok, err = session:set_keyspace(keyspace_name)Sets session keyspace to the given
stmt, err = session:prepare(query, options)Prepare a statement for later execution.
result, err = session:execute(query, args, options)Execute a query or previously prepared statement.
batch, err = cassandra.BatchStatement(type)Initialized a batch statement. See the example below on how to use batch statements and this for informations about the type of batch to use.
batch:add(query, args)Add an operation to a batch statement. See the example below on how to use batch statements.
trace, err = session:get_trace(result)Return the trace of a given result, if possible.
ExamplesBatches: -- Create a batch statement
local batch = cassandra.BatchStatement()
-- Add a query
batch:add("INSERT INTO users (name, age, user_id) VALUES (?, ?, ?)",
{"James", 32, cassandra.uuid("2644bada-852c-11e3-89fb-e0b9a54a6d93")})
-- Add a prepared statement
local stmt, err = session:prepare("INSERT INTO users (name, age, user_id) VALUES (?, ?, ?)")
batch:add(stmt, {"John", 45, cassandra.uuid("1144bada-852c-11e3-89fb-e0b9a54a6d11")})
-- Execute the batch
local result, err = session:execute(batch) Pagination might be very useful to build web services: -- Assuming our users table contains 1000 rows
local query = "SELECT * FROM users"
local rows, err = session:execute(query, nil, {page_size = 500}) -- default page_size is 5000
assert.same(500, #rows) -- rows contains the 500 first rows
if rows.meta.has_more_pages then
local next_rows, err = session:execute(query, nil, {paging_state = rows.meta.paging_state})
assert.same(500, #next_rows) -- next_rows contains the next (and last) 500 rows
end Automated pagination: -- Assuming our users table now contains 10.000 rows
local query = "SELECT * FROM users"
for _, rows, page, err in session:execute(query, nil, {auto_paging=true}) do
assert.same(5000, #rows) -- rows contains 5000 rows on each iteration in this case
-- page: will be 1 on the first iteration, 2 on the second
-- err: in case any fetch returns an error
-- _: (the first for argument) is the current paging_state used to fetch the rows
end Running unit testsWe use $ luarocks install busted
$ make test Running coverage$ luarocks install luacov
$ make coverage Report will be in Running linting$ luarocks install luacheck
$ make lint ContributorsJuarez Bochi (@jbochi) Thibault Charbonnier (@thibaultCha) -> Several contributions, including paging support, improved batch statements, better documentation, specs and code style. Leandro Moreira (@leandromoreira) -> Added support for doubles Marco Palladino (@thefosk) |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论