cassandra.query - Prepared Statements, Batch Statements, Tracing, and Row Factories¶
-
cassandra.query.tuple_factory(colnames, rows)[source]¶ Returns each row as a tuple
Example:
>>> from cassandra.query import tuple_factory >>> session = cluster.connect('mykeyspace') >>> session.row_factory = tuple_factory >>> rows = session.execute("SELECT name, age FROM users LIMIT 1") >>> print rows[0] ('Bob', 42)
Changed in version 2.0.0: moved from
cassandra.decodertocassandra.query
-
cassandra.query.named_tuple_factory(colnames, rows)[source]¶ Returns each row as a namedtuple. This is the default row factory.
Example:
>>> from cassandra.query import named_tuple_factory >>> session = cluster.connect('mykeyspace') >>> session.row_factory = named_tuple_factory >>> rows = session.execute("SELECT name, age FROM users LIMIT 1") >>> user = rows[0] >>> # you can access field by their name: >>> print "name: %s, age: %d" % (user.name, user.age) name: Bob, age: 42 >>> # or you can access fields by their position (like a tuple) >>> name, age = user >>> print "name: %s, age: %d" % (name, age) name: Bob, age: 42 >>> name = user[0] >>> age = user[1] >>> print "name: %s, age: %d" % (name, age) name: Bob, age: 42
Changed in version 2.0.0: moved from
cassandra.decodertocassandra.query
-
cassandra.query.dict_factory(colnames, rows)[source]¶ Returns each row as a dict.
Example:
>>> from cassandra.query import dict_factory >>> session = cluster.connect('mykeyspace') >>> session.row_factory = dict_factory >>> rows = session.execute("SELECT name, age FROM users LIMIT 1") >>> print rows[0] {u'age': 42, u'name': u'Bob'}
Changed in version 2.0.0: moved from
cassandra.decodertocassandra.query
-
cassandra.query.ordered_dict_factory(colnames, rows)[source]¶ Like
dict_factory(), but returns each row as an OrderedDict, so the order of the columns is preserved.Changed in version 2.0.0: moved from
cassandra.decodertocassandra.query
-
class
cassandra.query.Statement(retry_policy=None, consistency_level=None, routing_key=None, serial_consistency_level=None, fetch_size=<object object>, keyspace=None, custom_payload=None)[source]¶ An abstract class representing a single query. There are three subclasses:
SimpleStatement,BoundStatement, andBatchStatement. These can be passed toSession.execute().-
trace= None¶ If
Session.execute()is run with trace set toTrue, this will be set to aQueryTraceinstance.
-
trace_id= None¶ If
Session.execute()is run with trace set toTrue, this will be set to the tracing ID from the server.
-
retry_policy= None¶ An instance of a
cassandra.policies.RetryPolicyor one of its subclasses. This controls when a query will be retried and how it will be retried.
-
consistency_level= None¶ The
ConsistencyLevelto be used for this operation. Defaults toNone, which means that the default consistency level for the Session this is executed in will be used.
-
fetch_size= <object object>¶ How many rows will be fetched at a time. This overrides the default of
Session.default_fetch_sizeThis only takes effect when protocol version 2 or higher is used. See
Cluster.protocol_versionfor details.New in version 2.0.0.
-
keyspace= None¶ The string name of the keyspace this query acts on. This is used when
TokenAwarePolicyis configured forCluster.load_balancing_policyIt is set implicitly on
BoundStatement, andBatchStatement, but must be set explicitly onSimpleStatement.New in version 2.1.3.
-
custom_payload= None¶ Custom Payloads to be passed to the server.
These are only allowed when using protocol version 4 or higher.
New in version 2.6.0.
-
routing_key¶ The
partition_keyportion of the primary key, which can be used to determine which nodes are replicas for the query.If the partition key is a composite, a list or tuple must be passed in. Each key component should be in its packed (binary) format, so all components should be strings.
-
serial_consistency_level¶ The serial consistency level is only used by conditional updates (
INSERT,UPDATEandDELETEwith anIFcondition). For those, theserial_consistency_leveldefines the consistency level of the serial phase (or “paxos” phase) while the normalconsistency_leveldefines the consistency for the “learn” phase, i.e. what type of reads will be guaranteed to see the update right away. For example, if a conditional write has aconsistency_levelofQUORUM(and is successful), then aQUORUMread is guaranteed to see that write. But if the regularconsistency_levelof that write isANY, then only a read with aconsistency_levelofSERIALis guaranteed to see it (even a read with consistencyALLis not guaranteed to be enough).The serial consistency can only be one of
SERIALorLOCAL_SERIAL. WhileSERIALguarantees full linearizability (with otherSERIALupdates),LOCAL_SERIALonly guarantees it in the local data center.The serial consistency level is ignored for any query that is not a conditional update. Serial reads should use the regular
consistency_level.Serial consistency levels may only be used against Cassandra 2.0+ and the
protocol_versionmust be set to 2 or higher.New in version 2.0.0.
-
-
class
cassandra.query.SimpleStatement(query_string, *args, **kwargs)[source]¶ A simple, un-prepared query.
query_string should be a literal CQL statement with the exception of parameter placeholders that will be filled through the parameters argument of
Session.execute().All arguments to
Statementapply to this class as well
-
class
cassandra.query.PreparedStatement[source]¶ A statement that has been prepared against at least one Cassandra node. Instances of this class should not be created directly, but through
Session.prepare().A
PreparedStatementshould be prepared only once. Re-preparing a statement may affect performance (as the operation requires a network roundtrip).-
bind(values)[source]¶ Creates and returns a
BoundStatementinstance using values.See
BoundStatement.bind()for rules on inputvalues.
-
-
class
cassandra.query.BoundStatement(prepared_statement, *args, **kwargs)[source]¶ A prepared statement that has been bound to a particular set of values. These may be created directly or through
PreparedStatement.bind().prepared_statement should be an instance of
PreparedStatement.All arguments to
Statementapply to this class as well-
prepared_statement= None¶ The
PreparedStatementinstance that this was created from.
-
values= None¶ The sequence of values that were bound to the prepared statement.
-
bind(values)[source]¶ Binds a sequence of values for the prepared statement parameters and returns this instance. Note that values must be:
- a sequence, even if you are only binding one value, or
- a dict that relates 1-to-1 between dict keys and columns
Changed in version 2.6.0:
UNSET_VALUEwas introduced. These can be bound as positional parameters in a sequence, or by name in a dict. Additionally, when using protocol v4+:- short sequences will be extended to match bind parameters with UNSET_VALUE
- names may be omitted from a dict with UNSET_VALUE implied.
-
-
cassandra.query.UNSET_VALUE¶ Specifies an unset value when binding a prepared statement.
Unset values are ignored, allowing prepared statements to be used without specify
See https://issues.apache.org/jira/browse/CASSANDRA-7304 for further details on semantics.
New in version 2.6.0.
Only valid when using native protocol v4+
-
class
cassandra.query.BatchStatement(batch_type=BatchType.LOGGED, retry_policy=None, consistency_level=None)[source]¶ A protocol-level batch of operations which are applied atomically by default.
New in version 2.0.0.
batch_type specifies The
BatchTypefor the batch operation. Defaults toBatchType.LOGGED.retry_policy should be a
RetryPolicyinstance for controlling retries on the operation.consistency_level should be a
ConsistencyLevelvalue to be used for all operations in the batch.custom_payload is a Custom Payloads passed to the server. Note: as Statement objects are added to the batch, this map is updated with any values found in their custom payloads. These are only allowed when using protocol version 4 or higher.
Example usage:
insert_user = session.prepare("INSERT INTO users (name, age) VALUES (?, ?)") batch = BatchStatement(consistency_level=ConsistencyLevel.QUORUM) for (name, age) in users_to_insert: batch.add(insert_user, (name, age)) session.execute(batch)
You can also mix different types of operations within a batch:
batch = BatchStatement() batch.add(SimpleStatement("INSERT INTO users (name, age) VALUES (%s, %s)"), (name, age)) batch.add(SimpleStatement("DELETE FROM pending_users WHERE name=%s"), (name,)) session.execute(batch)
New in version 2.0.0.
Changed in version 2.1.0: Added serial_consistency_level as a parameter
Changed in version 2.6.0: Added custom_payload as a parameter
-
serial_consistency_level= None¶ The same as
Statement.serial_consistency_level, but is only supported when using protocol version 3 or higher.
-
batch_type= None¶ The
BatchTypefor the batch operation. Defaults toBatchType.LOGGED.
-
-
class
cassandra.query.BatchType[source]¶ A BatchType is used with
BatchStatementinstances to control the atomicity of the batch operation.New in version 2.0.0.
-
LOGGED= BatchType.LOGGED¶ Atomic batch operation.
-
UNLOGGED= BatchType.UNLOGGED¶ Non-atomic batch operation.
-
COUNTER= BatchType.COUNTER¶ Batches of counter operations.
-
-
class
cassandra.query.ValueSequence[source]¶ A wrapper class that is used to specify that a sequence of values should be treated as a CQL list of values instead of a single column collection when used as part of the parameters argument for
Session.execute().This is typically needed when supplying a list of keys to select. For example:
>>> my_user_ids = ('alice', 'bob', 'charles') >>> query = "SELECT * FROM users WHERE user_id IN %s" >>> session.execute(query, parameters=[ValueSequence(my_user_ids)])
-
class
cassandra.query.QueryTrace[source]¶ A trace of the duration and events that occurred when executing an operation.
-
request_type= None¶ A string that very generally describes the traced operation.
-
duration= None¶ A
datetime.timedeltameasure of the duration of the query.
-
client= None¶ The IP address of the client that issued this request
This is only available when using Cassandra 2.2+
-
coordinator= None¶ The IP address of the host that acted as coordinator for this request.
-
parameters= None¶ A
dictof parameters for the traced operation, such as the specific query string.
-
started_at= None¶ A UTC
datetime.datetimeobject describing when the operation was started.
-
events= None¶ A chronologically sorted list of
TraceEventinstances representing the steps the traced operation went through. This corresponds to the rows insystem_traces.eventsfor this tracing session.
-
trace_id= None¶ uuid.UUIDunique identifier for this tracing session. Matches thesession_idcolumn insystem_traces.sessionsandsystem_traces.events.
-
populate(max_wait=2.0)[source]¶ Retrieves the actual tracing details from Cassandra and populates the attributes of this instance. Because tracing details are stored asynchronously by Cassandra, this may need to retry the session detail fetch. If the trace is still not available after max_wait seconds,
TraceUnavailablewill be raised; if max_wait isNone, this will retry forever.
-
-
class
cassandra.query.TraceEvent[source]¶ Representation of a single event within a query trace.
-
description= None¶ A brief description of the event.
-
datetime= None¶ A UTC
datetime.datetimemarking when the event occurred.
-
source= None¶ The IP address of the node this event occurred on.
-
source_elapsed= None¶ A
datetime.timedeltameasuring the amount of time until this event occurred starting from whensourcefirst received the query.
-
thread_name= None¶ The name of the thread that this event occurred on.
-
Raised when complete trace details cannot be fetched from Cassandra.