Module: Cql::Client

Defined in:
lib/cql/client.rb,
lib/cql/client/query_result.rb,
lib/cql/client/column_metadata.rb,
lib/cql/client/result_metadata.rb,
lib/cql/client/synchronous_client.rb,
lib/cql/client/asynchronous_client.rb,
lib/cql/client/synchronous_prepared_statement.rb,
lib/cql/client/asynchronous_prepared_statement.rb

Overview

A CQL client manages connections to one or more Cassandra nodes and you use it run queries, insert and update data.

Client instances are threadsafe.

See Client for the full client API.

Examples:

Connecting and changing to a keyspace

# create a client and connect to two Cassandra nodes
client = Cql::Client.connect(host: 'node01.cassandra.local,node02.cassandra.local')
# change to a keyspace
client.use('stuff')

Query for data

rows = client.execute('SELECT * FROM things WHERE id = 2')
rows.each do |row|
  p row
end

Inserting and updating data

client.execute("INSERT INTO things (id, value) VALUES (4, 'foo')")
client.execute("UPDATE things SET value = 'bar' WHERE id = 5")

Prepared statements

statement = client.prepare('INSERT INTO things (id, value) VALUES (?, ?)')
statement.execute(9, 'qux')
statement.execute(8, 'baz')

Defined Under Namespace

Classes: AsynchronousClient, AsynchronousPreparedStatement, Client, ColumnMetadata, Pipeline, PreparedStatement, QueryResult, ResultMetadata, SynchronousClient, SynchronousPreparedStatement

Constant Summary collapse

NotConnectedError =
Class.new(ClientError)
InvalidKeyspaceNameError =
Class.new(ClientError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.connect(options = {}) ⇒ Cql::Client::Client

Create a new client and connects to Cassandra.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :host (String) — default: 'localhost'

    One or more (comma separated) hostnames for the Cassandra nodes you want to connect to.

  • :port (String) — default: 9042

    The port to connect to

  • :connection_timeout (Integer) — default: 5

    Max time to wait for a connection, in seconds

  • :keyspace (String)

    The keyspace to change to immediately after all connections have been established, this is optional.

Returns:



61
62
63
# File 'lib/cql/client.rb', line 61

def self.connect(options={})
  SynchronousClient.new(AsynchronousClient.new(options)).connect
end

Instance Method Details

#closeObject

Disconnect from all nodes.

Returns:

  • self



# File 'lib/cql/client.rb', line 79

#connectObject

Connect to all nodes.

You must call this method before you call any of the other methods of a client. Calling it again will have no effect.

If ‘:keyspace` was specified when the client was created the current keyspace will also be changed (otherwise the current keyspace will not be set).

Returns:

  • self



# File 'lib/cql/client.rb', line 66

#connected?true, false

Returns whether or not the client is connected.

Returns:

  • (true, false)


# File 'lib/cql/client.rb', line 85

#execute(cql, consistency = :quorum) ⇒ nil, Cql::Client::QueryResult

Execute a CQL statement

Parameters:

  • cql (String)
  • consistency (Symbol) (defaults to: :quorum)

Returns:

Raises:

  • (Cql::NotConnectedError)

    raised when the client is not connected

  • (Cql::QueryError)

    raised when the CQL has syntax errors or for other situations when the server complains.



# File 'lib/cql/client.rb', line 109

#keyspacenil

Returns the name of the current keyspace, or ‘nil` if no keyspace has been set yet.

Parameters:

  • keyspace (String)

Returns:

  • (nil)


# File 'lib/cql/client.rb', line 91

#prepare(cql) ⇒ Cql::Client::PreparedStatement

Returns a prepared statement that can be run over and over again with different values.

Parameters:

  • cql (String)

Returns:

Raises:

  • (Cql::NotConnectedError)

    raised when the client is not connected



# File 'lib/cql/client.rb', line 122

#use(keyspace) ⇒ nil

Changes keyspace by sending a ‘USE` statement to all connections.

The the second parameter is meant for internal use only.

Parameters:

  • keyspace (String)

Returns:

  • (nil)

Raises:

  • (Cql::NotConnectedError)

    raised when the client is not connected



# File 'lib/cql/client.rb', line 99