Ilios
Ilios that Cassandra driver written by C language for Ruby using DataStax C/C++ Driver.
Installation
Install the gem and add to the application's Gemfile by executing:
$ bundle add ilios
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install ilios
Requirements
- cmake (in order to build the DataStax C/C++ Driver and libuv)
Supported
- Ruby 3.0 or later
- Cassandra 3.0 or later
- Linux and macOS platform
Example
Basic usage
Create the keyspace in advance using the cqlsh command.
CREATE KEYSPACE IF NOT EXISTS ilios
WITH REPLICATION = {
'class' : 'SimpleStrategy',
'replication_factor' : 1
};
Then, you can run the following code.
require 'ilios'
Ilios::Cassandra.config = {
keyspace: 'ilios',
hosts: ['127.0.0.1'],
}
# Create the table
statement = Ilios::Cassandra.session.prepare("CREATE TABLE IF NOT EXISTS ilios.example (\n id bigint,\n message text,\n created_at timestamp,\n PRIMARY KEY (id)\n) WITH compaction = { 'class' : 'LeveledCompactionStrategy' }\nAND gc_grace_seconds = 691200;\n")
Ilios::Cassandra.session.execute(statement)
# Insert the records
statement = Ilios::Cassandra.session.prepare("INSERT INTO ilios.example (\n id,\n message,\n created_at\n) VALUES (?, ?, ?)\n")
100.times do |i|
statement.bind({
id: i,
message: 'Hello World',
created_at: Time.now,
})
Ilios::Cassandra.session.execute(statement)
end
# Select the records
statement = Ilios::Cassandra.session.prepare("SELECT * FROM ilios.example\n")
statement.idempotent = true
statement.page_size = 25
result = Ilios::Cassandra.session.execute(statement)
result.each do |row|
p row
end
while(result.next_page)
result.each do |row|
p row
end
end
Synchronous API
Ilios::Cassandra::Session#prepare and Ilios::Cassandra::Session#execute are provided as synchronous API.
statement = Ilios::Cassandra.session.prepare("SELECT * FROM ilios.example\n")
result = Ilios::Cassandra.session.execute(statement)
Asynchronous API
Ilios::Cassandra::Session#prepare_async and Ilios::Cassandra::Session#execute_async are provided as asynchronous API.
prepare_future = Ilios::Cassandra.session.prepare_async("INSERT INTO ilios.example (\n id,\n message,\n created_at\n) VALUES (?, ?, ?)\n")
prepare_future.on_success { |statement|
futures = []
10.times do |i|
statement.bind({
id: i,
message: 'Hello World',
created_at: Time.now,
})
result_future = Ilios::Cassandra.session.execute_async(statement)
result_future.on_success { |result|
p result
p "success"
}
result_future.on_failure {
p "fail"
}
futures << result_future
end
futures.each(&:await)
}
prepare_future.await
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/Watson1978/ilios.