Connect an application
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
- Build Ruby applications using Pg Gem Driver
- Build Ruby applications using Active Record ORM