Skip to content

Instantly share code, notes, and snippets.

@mpenick
Last active April 1, 2020 16:26
Show Gist options
  • Save mpenick/7c43cc677378765549fbc754f2193df5 to your computer and use it in GitHub Desktop.
Save mpenick/7c43cc677378765549fbc754f2193df5 to your computer and use it in GitHub Desktop.
-- To run:
-- # cd <some-path>/lua-cassandra
-- # LUA_PATH="lib/?/init.lua;lib/?.lua" resty --errlog-level info --shdict 'cassandra 1m' cloud.lua
local Cluster = require 'resty.cassandra.cluster'
local Auth = require 'cassandra.auth'
local Socket = require 'cassandra.socket'
-- Use luasocket
Socket.force_luasocket("timer", true)
local auth = Auth.plain_text("cassandra", "cassandra") -- 1) Change credentials
local cluster, err = Cluster.new {
shm = 'cassandra', -- defined by the lua_shared_dict directive
contact_points = {'7393056f-7ea4-4ebf-ac48-8502e2fd4656'}, -- 2) Provide valid sni contact points
sni_host = '127.0.0.1', -- 3) Use cloud host
default_port = 30002, -- 4) Change port
ssl = true,
verify = true,
cert = "./secure-connect/cert",
key = "./secure-connect/key",
cafile = "./secure-connect/ca.crt",
auth = auth,
keyspace = 'system',
}
if not cluster then
ngx.log(ngx.ERR, 'could not create cluster: ', err)
return ngx.exit(500)
end
local rows, err = cluster:execute "SELECT * FROM peers"
if not rows then
ngx.log(ngx.ERR, 'could not retrieve peers: ', err)
return ngx.exit(500)
end
ngx.say('peers: ', #rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment