Ruby legacy drivers
This example can be used to attach to Astra from Ruby using the DataStax Driver. Before proceeding you might also consider using the REST, GraphQL or Document APIs to connect to Astra without the need for a custom driver.
Prerequisites
-
Ensure the Ruby driver is installed. For more, see DataStax Ruby Driver for Apache Cassandra.
-
Collect the required information for Connecting with legacy drivers.
Procedure
-
Copy the certificate files from the Astra secure connect bundle to
/etc/ssl/certs/astra-secure-connect
. This folder should contain theca.cert
,cert
, andkey
files.
-
Replace the
<hostname>
with your Astra database hostname.
-
Replace the
<port>
with your Astra database port.
-
Replace
<username>
and<password>
with the Astra database Client ID and Client Secret.
require 'cassandra'
cluster = Cassandra.cluster(
username: '<username>',
password: '<password>',
hosts: ['<hostname>'],
port: <port>,
server_cert: '/etc/ssl/certs/astra-secure-connect/ca.cert',
client_cert: '/etc/ssl/certs/astra-secure-connect/cert',
private_key: '/etc/ssl/certs/astra-secure-connect/key'
)
keyspace = 'system_schema'
session = cluster.connect(keyspace) # create session, optionally scoped to a keyspace, to execute queries
future = session.execute_async('SELECT keyspace_name, table_name FROM tables') # fully asynchronous api
future.on_success do |rows|
rows.each do |row|
puts "The keyspace #{row['keyspace_name']} has a table #{row['table_name']}"
end
end
future.join
Example:
require 'cassandra'
cluster = Cassandra.cluster(
username: 'john.smith',
password: 'jsP@ssw0rd',
hosts: ['602c8f3c-ea96-41b0-a7c1-6c6c5bdd1f34-us-east1.db.astra.datastax.com'],
port: 31575,
server_cert: '/etc/ssl/certs/astra-secure-connect/ca.cert',
client_cert: '/etc/ssl/certs/astra-secure-connect/cert',
private_key: '/etc/ssl/certs/astra-secure-connect/key'
)
keyspace = 'system_schema'
session = cluster.connect(keyspace) # create session, optionally scoped to a keyspace, to execute queries
future = session.execute_async('SELECT keyspace_name, table_name FROM tables') # fully asynchronous api
future.on_success do |rows|
rows.each do |row|
puts "The keyspace #{row['keyspace_name']} has a table #{row['table_name']}"
end
end
future.join
In the example above, the following variables are used:
-
hostname
is602c8f3c-ea96-41b0-a7c1-6c6c5bdd1f34-us-east1.db.astra.datastax.com
-
port
is31575
-
username
isjohn.smith
-
password
isjsP@ssw0rd