Connect an application

Yugabyte Ruby Driver for YCQL is based on DataStax Ruby Driver with additional smart driver features.

YugabyteDB Managed

To use the driver's partition-aware load balancing feature in a YugabyteDB Managed cluster, applications must be deployed in a VPC that has been peered with the cluster VPC so that they have access to all nodes in the cluster. For more information, refer to Using YCQL drivers with YugabyteDB Managed.

Install the YugabyteDB Ruby Driver for YCQL

To install the YugabyteDB Ruby Driver for YCQL, run the following gem install command:

$ gem install yugabyte-ycql-driver

Create a sample Ruby application

Create a file yb-ycql-helloworld.rb and copy the following content to it.

require 'ycql'

# Create the cluster connection, connects to localhost by default.
cluster = Cassandra.cluster
session = cluster.connect()

# Create the keyspace.
session.execute('CREATE KEYSPACE IF NOT EXISTS ybdemo;')
puts "Created keyspace ybdemo"

# Create the table.
session.execute(
  """
  CREATE TABLE IF NOT EXISTS ybdemo.employee (id int PRIMARY KEY,
                                              name varchar,
                                              age int,
                                              language varchar);
  """)
puts "Created table employee"

# Insert a row.
session.execute(
  """
  INSERT INTO ybdemo.employee (id, name, age, language)
  VALUES (1, 'John', 35, 'Ruby');
  """)
puts "Inserted (id, name, age, language) = (1, 'John', 35, 'Ruby')"

# Query the row.
rows = session.execute('SELECT name, age, language FROM ybdemo.employee WHERE id = 1;')
rows.each do |row|
  puts "Query returned: %s %s %s" % [ row['name'], row['age'], row['language'] ]
end

# Close the connection.
cluster.close()

Run the application

To use the application, run the following command:

$ ruby yb-cql-helloworld.rb

You should see the following output.

Created keyspace ybdemo
Created table employee
Inserted (id, name, age, language) = (1, 'John', 35, 'Ruby')
Query returned: John 35 Ruby

Learn more