22 KiB
Making Queries
Users of versions < 0.4, please read this post before upgrading: Breaking Changes
cqlengine.connection
cqlengine.query
Retrieving objects
Once you've populated Cassandra with data, you'll probably want to retrieve some of it. This is accomplished with QuerySet objects. This section will describe how to use QuerySet objects to retrieve the data you're looking for.
Retrieving all objects
The simplest query you can make is to return all objects from a table.
This is accomplished with the
.all()
method, which returns a QuerySet of all objects in a tableUsing the Person example model, we would get all Person objects like this:
= Person.objects.all() all_objects
Retrieving objects with filters
Typically, you'll want to query only a subset of the records in your database.
That can be accomplished with the QuerySet's
.filter(\*\*)
method.For example, given the model definition:
class Automobile(Model): = columns.Text(primary_key=True) manufacturer = columns.Integer(primary_key=True) year = columns.Text() model = columns.Decimal() price
...and assuming the Automobile table contains a record of every car model manufactured in the last 20 years or so, we can retrieve only the cars made by a single manufacturer like this:
= Automobile.objects.filter(manufacturer='Tesla') q
You can also use the more convenient syntax:
= Automobile.objects(Automobile.manufacturer == 'Tesla') q
We can then further filter our query with another call to .filter
= q.filter(year=2012) q
Note: all queries involving any filtering MUST define either an '=' or an 'in' relation to either a primary key column, or an indexed column.
Accessing objects in a QuerySet
There are several methods for getting objects out of a queryset
- iterating over the queryset
for car in Automobile.objects.all(): #...do something to the car instance pass
- list index
= Automobile.objects.all() q 0] #returns the first result q[1] #returns the second result q[
- list slicing
= Automobile.objects.all() q 1:] #returns all results except the first q[1:9] #returns a slice of the results q[
Note: CQL does not support specifying a start position in it's queries. Therefore, accessing elements using array indexing / slicing will load every result up to the index value requested
- calling
get() <query.QuerySet.get>
on the queryset= Automobile.objects.filter(manufacturer='Tesla') q = q.filter(year=2012) q = q.get() car
this returns the object matching the queryset
- calling
first() <query.QuerySet.first>
on the queryset= Automobile.objects.filter(manufacturer='Tesla') q = q.filter(year=2012) q = q.first() car
this returns the first value in the queryset
Filtering Operators
Equal To <query.QueryOperator.EqualsOperator>
The default filtering operator.
= Automobile.objects.filter(manufacturer='Tesla') q = q.filter(year=2012) #year == 2012 q
In addition to simple equal to queries, cqlengine also supports querying with other operators by appending a
__<op>
to the field name on the filtering call
in (__in) <query.QueryOperator.InOperator>
= Automobile.objects.filter(manufacturer='Tesla') q = q.filter(year__in=[2011, 2012]) q
> (__gt) <query.QueryOperator.GreaterThanOperator>
= Automobile.objects.filter(manufacturer='Tesla') q = q.filter(year__gt=2010) # year > 2010 q # or the nicer syntax filter(Automobile.year > 2010) q.
>= (__gte) <query.QueryOperator.GreaterThanOrEqualOperator>
= Automobile.objects.filter(manufacturer='Tesla') q = q.filter(year__gte=2010) # year >= 2010 q # or the nicer syntax filter(Automobile.year >= 2010) q.
< (__lt) <query.QueryOperator.LessThanOperator>
= Automobile.objects.filter(manufacturer='Tesla') q = q.filter(year__lt=2012) # year < 2012 q # or... filter(Automobile.year < 2012) q.
<= (__lte) <query.QueryOperator.LessThanOrEqualOperator>
= Automobile.objects.filter(manufacturer='Tesla') q = q.filter(year__lte=2012) # year <= 2012 q filter(Automobile.year <= 2012) q.
TimeUUID Functions
In addition to querying using regular values, there are two functions you can pass in when querying TimeUUID columns to help make filtering by them easier. Note that these functions don't actually return a value, but instruct the cql interpreter to use the functions in it's query.
returns the minimum time uuid value possible for the given datetime
returns the maximum time uuid value possible for the given datetime
Example
class DataStream(Model): = cqlengine.TimeUUID(primary_key=True) time = cqlengine.Bytes() data = datetime(1982, 1, 1) min_time = datetime(1982, 3, 9) max_time filter(time__gt=cqlengine.MinTimeUUID(min_time), time__lt=cqlengine.MaxTimeUUID(max_time)) DataStream.
Token Function
Token functon may be used only on special, virtual column pk__token, representing token of partition key (it also works for composite partition keys). Cassandra orders returned items by value of partition key token, so using cqlengine.Token we can easy paginate through all table rows.
See http://cassandra.apache.org/doc/cql3/CQL.html#tokenFun
Example
class Items(Model): id = cqlengine.Text(primary_key=True) = cqlengine.Bytes() data = Items.objects.all().limit(10) query = list(query); first_page = first_page[-1] last = list(query.filter(pk__token__gt=cqlengine.Token(last.pk))) next_page
QuerySets are immutable ======================
When calling any method that changes a queryset, the method does not actually change the queryset object it's called on, but returns a new queryset object with the attributes of the original queryset, plus the attributes added in the method call.
Example
#this produces 3 different querysets #q does not change after it's initial definition = Automobiles.objects.filter(year=2012) q = q.filter(manufacturer='Tesla') tesla2012 = q.filter(manufacturer='Honda') honda2012
Ordering QuerySets
Since Cassandra is essentially a distributed hash table on steroids, the order you get records back in will not be particularly predictable.
However, you can set a column to order on with the
.order_by(column_name)
method.Example
#sort ascending = Automobiles.objects.all().order_by('year') q #sort descending = Automobiles.objects.all().order_by('-year') q
Note: Cassandra only supports ordering on a clustering key. In other words, to support ordering results, your model must have more than one primary key, and you must order on a primary key, excluding the first one.
For instance, given our Automobile model, year is the only column we can order on.
Values Lists
There is a special QuerySet's method
.values_list()
- when called, QuerySet returns lists of values instead of model instances. It may significantly speedup things with lower memory footprint for large responses. Each tuple contains the value from the respective field passed into thevalues_list()
call — so the first item is the first field, etc. For example:= list(range(20)) items random.shuffle(items)for i in items: id=1, clustering_key=i) TestModel.create( = list(TestModel.objects.values_list('clustering_key', flat=True)) values # [19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 0L]
Batch Queries
cqlengine now supports batch queries using the BatchQuery class. Batch queries can be started and stopped manually, or within a context manager. To add queries to the batch object, you just need to precede the create/save/delete call with a call to batch, and pass in the batch object.
Batch Query General Use Pattern
You can only create, update, and delete rows with a batch query, attempting to read rows out of the database with a batch query will fail.
from cqlengine import BatchQuery #using a context manager with BatchQuery() as b: = datetime.now() now = ExampleModel.batch(b).create(example_type=0, description="1", created_at=now) em1 = ExampleModel.batch(b).create(example_type=0, description="2", created_at=now) em2 = ExampleModel.batch(b).create(example_type=0, description="3", created_at=now) em3 # -- or -- #manually = BatchQuery() b = datetime.now() now = ExampleModel.batch(b).create(example_type=0, description="1", created_at=now) em1 = ExampleModel.batch(b).create(example_type=0, description="2", created_at=now) em2 = ExampleModel.batch(b).create(example_type=0, description="3", created_at=now) em3 b.execute() # updating in a batch = BatchQuery() b = "new description" em1.description em1.batch(b).save()= "another new description" em2.description em2.batch(b).save() b.execute() # deleting in a batch = BatchQuery() b id=some_id).batch(b).delete() ExampleModel.objects(id=some_id2).batch(b).delete() ExampleModel.objects( b.execute()
Typically you will not want the block to execute if an exception occurs inside the with block. However, in the case that this is desirable, it's achievable by using the following syntax:
with BatchQuery(execute_on_exception=True) as b: =1, v=1) LogEntry.batch(b).create(k# exception thrown in here mystery_function() =1, v=2) # this code is never reached due to the exception, but anything leading up to here will execute in the batch. LogEntry.batch(b).create(k
If an exception is thrown somewhere in the block, any statements that have been added to the batch will still be executed. This is useful for some logging situations.
Batch Query Execution Callbacks
In order to allow secondary tasks to be chained to the end of batch, BatchQuery instances allow callbacks to be registered with the batch, to be executed immediately after the batch executes.
Multiple callbacks can be attached to same BatchQuery instance, they are executed in the same order that they are added to the batch.
The callbacks attached to a given batch instance are executed only if the batch executes. If the batch is used as a context manager and an exception is raised, the queued up callbacks will not be run.
def my_callback(*args, **kwargs): pass = BatchQuery() batch batch.add_callback(my_callback)'positional arg', named_arg='named arg value') batch.add_callback(my_callback, # if you need reference to the batch within the callback, # just trap it in the arguments to be passed to the callback: =batch) batch.add_callback(my_callback, cqlengine_batch # once the batch executes... batch.execute() # the effect of the above scheduled callbacks will be similar to my_callback()'positional arg', named_arg='named arg value') my_callback(=batch) my_callback(cqlengine_batch
Failure in any of the callbacks does not affect the batch's execution, as the callbacks are started after the execution of the batch is complete.
Logged vs Unlogged Batches
By default, queries in cqlengine are LOGGED, which carries additional overhead from UNLOGGED. To explicitly state which batch type to use, simply:
from cqlengine.query import BatchType with BatchQuery(batch_type=BatchType.Unlogged) as b: =1, v=1) LogEntry.batch(b).create(k=1, v=2) LogEntry.batch(b).create(k
QuerySet method reference
all()
Returns a queryset matching all rows
for user in User.objects().all():
print(user)
batch(batch_object)
Sets the batch object to run the query on. Note that running a select query with a batch object will raise an exception
consistency(consistency_setting)
Sets the consistency level for the operation. Options may be imported
from the top level cqlengine
package.
for user in User.objects(id=3).consistency(ONE):
print(user)
count()
Returns the number of matching rows in your QuerySet
print(User.objects().count())
filter(**values)
- param values
-
See
retrieving-objects-with-filters
Returns a QuerySet filtered on the keyword arguments
get(**values)
- param values
-
See
retrieving-objects-with-filters
Returns a single object matching the QuerySet. If no objects are
matched, a ~models.Model.DoesNotExist
exception is raised. If
more than one object is found, a ~models.Model.MultipleObjectsReturned
exception is
raised.
= User.get(id=1) user
limit(num)
Limits the number of results returned by Cassandra.
Note that CQL's default limit is 10,000, so all queries without a limit set explicitly will have an implicit limit of 10,000
for user in User.objects().limit(100):
print(user)
order_by(field_name)
- param field_name
-
the name of the field to order on. Note: the field_name must be a clustering key
- type field_name
-
string
Sets the field to order on.
from uuid import uuid1,uuid4
class Comment(Model):
= UUID(primary_key=True)
photo_id = TimeUUID(primary_key=True, default=uuid1) # auto becomes clustering key
comment_id = Text()
comment
sync_table(Comment)
= uuid4()
u for x in range(5):
=u, comment="test %d" % x)
Comment.create(photo_id
print("Normal")
for comment in Comment.objects(photo_id=u):
print comment.comment_id
print("Reversed")
for comment in Comment.objects(photo_id=u).order_by("-comment_id"):
print comment.comment_id
allow_filtering()
Enables the (usually) unwise practive of querying on a clustering key without also defining a partition key
timestamp(timestamp_or_long_or_datetime)
Allows for custom timestamps to be saved with the record.
ttl(ttl_in_seconds)
- param ttl_in_seconds
-
time in seconds in which the saved values should expire
- type ttl_in_seconds
-
int
Sets the ttl to run the query query with. Note that running a select query with a ttl value will raise an exception
update(**values)
Performs an update on the row selected by the queryset. Include values to update in the update like so:
=n).update(value='x') Model.objects(key
Passing in updates for columns which are not part of the model will raise a ValidationError. Per column validation will be performed, but instance level validation will not (Model.validate is not called). This is sometimes referred to as a blind update.
For example:
class User(Model):
id = Integer(primary_key=True)
= Text()
name
"localhost"], "test")
setup([
sync_table(User)
= User.create(id=1, name="jon")
u
id=1).update(name="Steve")
User.objects(
# sets name to null
id=1).update(name=None) User.objects(
The queryset update method also supports blindly adding and removing elements from container columns, without loading a model instance from Cassandra.
Using the syntax .update(column_name={x, y, z}) will overwrite the contents of the container, like updating a non container column. However, adding __<operation> to the end of the keyword arg, makes the update call add or remove items from the collection, without overwriting then entire column.
Given the model below, here are the operations that can be performed on the different container columns:
class Row(Model):
= columns.Integer(primary_key=True)
row_id = columns.Set(Integer)
set_column = columns.Set(Integer)
list_column = columns.Set(Integer, Integer) map_column
~cqlengine.columns.Set
- `add`: adds the elements of the given set to the column
- `remove`: removes the elements of the given set to the column
# add elements to a set
=5).update(set_column__add={6})
Row.objects(row_id
# remove elements to a set
=5).update(set_column__remove={4}) Row.objects(row_id
~cqlengine.columns.List
- `append`: appends the elements of the given list to the end of the column
- `prepend`: prepends the elements of the given list to the beginning of the column
# append items to a list
=5).update(list_column__append=[6, 7])
Row.objects(row_id
# prepend items to a list
=5).update(list_column__prepend=[1, 2]) Row.objects(row_id
~cqlengine.columns.Map
- `update`: adds the given keys/values to the columns, creating new entries if they didn't exist, and overwriting old ones if they did
# add items to a map
=5).update(map_column__update={1: 2, 3: 4}) Row.objects(row_id
Per Query Timeouts
By default all queries are executed with the timeout defined in ~cqlengine.connection.setup() The examples below show how to specify a per-query timeout. A timeout is specified in seconds and can be an int, float or None. None means no timeout.
class Row(Model): id = columns.Integer(primary_key=True) = columns.Text() name
Fetch all objects with a timeout of 5 seconds
5).all() Row.objects().timeout(
Create a single row with a 50ms timeout
id=1, name='Jon').timeout(0.05).create() Row(
Delete a single row with no timeout
id=1).timeout(None).delete() Row(
Update a single row with no timeout
id=1).timeout(None).update(name='Blake') Row(
Batch query timeouts
with BatchQuery(timeout=10) as b: id=1, name='Jon').create() Row(
NOTE: You cannot set both timeout and batch at the same time, batch will use the timeout defined in it's constructor. Setting the timeout on the model is meaningless and will raise an AssertionError.
Named Tables
Named tables are a way of querying a table without creating an class. They're useful for querying system tables or exploring an unfamiliar database.
from cqlengine.connection import setup "127.0.0.1", "cqlengine_test") setup( from cqlengine.named import NamedTable = NamedTable("cqlengine_test", "user") user user.objects()0] user.objects()[ # {u'pk': 1, u't': datetime.datetime(2014, 6, 26, 17, 10, 31, 774000)}