pycassa.columnfamilymap
– Maps Classes to Column Families¶
Provides a way to map an existing class of objects to a column family.
This can help to cut down boilerplate code related to converting objects to a row format and back again. ColumnFamilyMap is primarily useful when you have one “object” per row.
See also
pycassa.types
for selecting data types for object
attributes and infomation about creating custom data
types.
-
class
pycassa.columnfamilymap.
ColumnFamilyMap
(cls, pool, column_family[, raw_columns])¶ Maps an existing class to a column family. Class fields become columns, and instances of that class can be represented as rows in standard column families or super columns in super column families.
Instances of cls are returned from
get()
,multiget()
,get_range()
andget_indexed_slices()
.pool is a
ConnectionPool
that will be used in the same way aColumnFamily
uses one.column_family is the name of a column family to tie to cls.
If raw_columns is
True
, all columns will be fetched into the raw_columns field in requests.-
get
(key[, columns][, column_start][, column_finish][, column_count][, column_reversed][, super_column][, read_consistency_level])¶ Creates one or more instances of cls from the row with key key.
The fields that are retreived may be specified using columns, which should be a list of column names.
If the column family is a super column family, a list of cls instances will be returned, one for each super column. If the super_column parameter is not supplied, then columns specifies which super columns will be used to create instances of cls. If the super_column parameter is supplied, only one instance of cls will be returned; if columns is specified in this case, only those attributes listed in columns will be fetched.
All other parameters behave the same as in
ColumnFamily.get()
.
-
multiget
(keys[, columns][, column_start][, column_finish][, column_count][, column_reversed][, super_column][, read_consistency_level])¶ Like
get()
, but a list of keys may be specified.The result of multiget will be a dictionary where the keys are the keys from the keys argument, minus any missing rows. The value for each key in the dictionary will be the same as if
get()
were called on that individual key.
-
get_range
([start][, finish][, columns][, column_start][, column_finish][, column_reversed][, column_count][, row_count][, super_column][, read_consistency_level][, buffer_size])¶ Get an iterator over instances in a specified key range.
Like
multiget()
, whether a single instance or multiple instances are returned per-row when the column family is a super column family depends on what parameters are passed.For an explanation of how
get_range()
works and a description of the parameters, seeColumnFamily.get_range()
.Example usage with a standard column family:
>>> pool = pycassa.ConnectionPool('Keyspace1') >>> usercf = pycassa.ColumnFamily(pool, 'Users') >>> cfmap = pycassa.ColumnFamilyMap(MyClass, usercf) >>> users = cfmap.get_range(row_count=2, columns=['name', 'age']) >>> for key, user in users: ... print user.name, user.age Miles Davis 84 Winston Smith 42
-
get_indexed_slices
(index_clause[, columns][, column_start][, column_finish][, column_reversed][, column_count][, include_timestamp][, read_consistency_level][, buffer_size])¶ Fetches a list of instances that satisfy an index clause. Similar to
get_range()
, but uses an index clause instead of a key range.See
ColumnFamily.get_indexed_slices()
for an explanation of the parameters.
-
insert
(instance[, columns][, write_consistency_level])¶ Insert or update stored instances.
instance should be an instance of cls to store.
The columns parameter allows to you specify which attributes of instance should be inserted or updated. If left as
None
, all attributes will be inserted.
-
batch_insert
(instances[, timestamp][, ttl][, write_consistency_level])¶ Insert or update stored instances.
instances should be a list containing instances of cls to store.
-
remove
(instance[, columns][, write_consistency_level])¶ Removes a stored instance.
The columns parameter is a list of columns that should be removed. If this is left as the default value of
None
, the entire stored instance will be removed.
-