lab 4: interfacing with cassandra
DESCRIPTION
Cloud Computing Workshop 2013, ITUTRANSCRIPT
4: Interfacing with Cassandra
Zubair Nabi
April 20, 2013
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 1 / 16
Introduction1
Project website: http://cassandra.apache.org/
Column based key value store (multi-level dictionary)Consists of keyspaces and column families
I Keyspace: Namespace for column families (typically one perapplication)
I Column family: Contains multiple columns, each of which has a name,value, and timestamp, and which are referenced by row keys
1Disclaimer: Based on the wonderful talk given by Jeremiah Jordan at PyCon 2012Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 2 / 16
Introduction1
Project website: http://cassandra.apache.org/
Column based key value store (multi-level dictionary)
Consists of keyspaces and column familiesI Keyspace: Namespace for column families (typically one per
application)I Column family: Contains multiple columns, each of which has a name,
value, and timestamp, and which are referenced by row keys
1Disclaimer: Based on the wonderful talk given by Jeremiah Jordan at PyCon 2012Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 2 / 16
Introduction1
Project website: http://cassandra.apache.org/
Column based key value store (multi-level dictionary)Consists of keyspaces and column families
I Keyspace: Namespace for column families (typically one perapplication)
I Column family: Contains multiple columns, each of which has a name,value, and timestamp, and which are referenced by row keys
1Disclaimer: Based on the wonderful talk given by Jeremiah Jordan at PyCon 2012Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 2 / 16
Introduction1
Project website: http://cassandra.apache.org/
Column based key value store (multi-level dictionary)Consists of keyspaces and column families
I Keyspace: Namespace for column families (typically one perapplication)
I Column family: Contains multiple columns, each of which has a name,value, and timestamp, and which are referenced by row keys
1Disclaimer: Based on the wonderful talk given by Jeremiah Jordan at PyCon 2012Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 2 / 16
Introduction1
Project website: http://cassandra.apache.org/
Column based key value store (multi-level dictionary)Consists of keyspaces and column families
I Keyspace: Namespace for column families (typically one perapplication)
I Column family: Contains multiple columns, each of which has a name,value, and timestamp, and which are referenced by row keys
1Disclaimer: Based on the wonderful talk given by Jeremiah Jordan at PyCon 2012Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 2 / 16
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 3 / 16
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 4 / 16
Starting up
Running it: sudo cassandra -f
Running the CLI: cassandra-cli
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 5 / 16
Starting up
Running it: sudo cassandra -f
Running the CLI: cassandra-cli
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 5 / 16
Creating a keyspace
Connecting to the store: connect localhost/9160;
Creating a keyspace: create keyspace ApplicationDatawith placement_strategy =’org.apache.cassandra.locator.SimpleStrategy’and strategy_options =[{replication_factor:1}];
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 6 / 16
Creating a keyspace
Connecting to the store: connect localhost/9160;
Creating a keyspace: create keyspace ApplicationDatawith placement_strategy =’org.apache.cassandra.locator.SimpleStrategy’and strategy_options =[{replication_factor:1}];
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 6 / 16
Creating a column family
Referencing a keyspace: use ApplicationData;
Creating a column family: create column family UserInfoand comparator = ’AsciiType’;
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 7 / 16
Creating a column family
Referencing a keyspace: use ApplicationData;
Creating a column family: create column family UserInfoand comparator = ’AsciiType’;
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 7 / 16
Interfacing with Cassandra in Python
Package: pycassa
Webpage: https://github.com/pycassa/pycassa
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 8 / 16
Interfacing with Cassandra in Python
Package: pycassa
Webpage: https://github.com/pycassa/pycassa
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 8 / 16
Pycassa Basics
1 import pycassa2 from pycassa.pool import ConnectionPool3 from pycassa.columnfamily import ColumnFamily45 pool = ConnectionPool(’ApplicationData’,
6 [’localhost:9160’])
7 col_fam = ColumnFamily(pool, ’UserInfo’)
8 col_fam.insert(’John’, {’email’: ’[email protected]’})
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 9 / 16
Read
1 readData = col_fam.get(’John’,
2 columns=[’email’])
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 10 / 16
Delete
1 col_fam.remove(’John’,
2 columns=[’email’])
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 11 / 16
Batch
1 col_fam.batch_insert(
2 {’John’: {’email’: ’[email protected]’,
3 ’state’: ’IL’,
4 ’gender’: ’M’},
5 ’Jane’: {’email’: ’[email protected]’,
6 ’state’: ’CA’
7 ’gender’: ’M’}})
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 12 / 16
Batch Stream
1 b = col_fam.batch(queue_size=10)
23 b.insert(’John’,
4 {’email’: ’[email protected]’,
5 ’state’: ’IL’,
6 ’gender’: ’M’})
78 b.insert(’Jane’,
9 {’email’: ’[email protected]’,
10 ’state’: ’CA’})
1112 b.remove(’John’, [’gender’])
13 b.remove(’Jane’)
14 b.send()
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 13 / 16
Batch Read
1 readData = col_fam.multiget([’John’, ’Jane’, ’Bill’])
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 14 / 16
Column Slice
1 d = col_fam.get(’Jane’,
2 column_start=’email’,
3 column_finish=’state’)
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 15 / 16
Reference(s)
Apache Cassandra and Python:https://pycon-2012-notes.readthedocs.org/en/latest/apache_cassandra_and_python.html
Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 16 / 16