24
4: Interfacing with Cassandra Zubair Nabi [email protected] April 20, 2013 Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 1 / 16

Lab 4: Interfacing with Cassandra

Embed Size (px)

DESCRIPTION

Cloud Computing Workshop 2013, ITU

Citation preview

Page 1: Lab 4: Interfacing with Cassandra

4: Interfacing with Cassandra

Zubair Nabi

[email protected]

April 20, 2013

Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 1 / 16

Page 2: Lab 4: Interfacing with Cassandra

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

Page 3: Lab 4: Interfacing with Cassandra

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

Page 4: Lab 4: Interfacing with Cassandra

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

Page 5: Lab 4: Interfacing with Cassandra

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

Page 6: Lab 4: Interfacing with Cassandra

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

Page 7: Lab 4: Interfacing with Cassandra

Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 3 / 16

Page 8: Lab 4: Interfacing with Cassandra

Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 4 / 16

Page 9: Lab 4: Interfacing with Cassandra

Starting up

Running it: sudo cassandra -f

Running the CLI: cassandra-cli

Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 5 / 16

Page 10: Lab 4: Interfacing with Cassandra

Starting up

Running it: sudo cassandra -f

Running the CLI: cassandra-cli

Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 5 / 16

Page 11: Lab 4: Interfacing with Cassandra

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

Page 12: Lab 4: Interfacing with Cassandra

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

Page 13: Lab 4: Interfacing with Cassandra

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

Page 14: Lab 4: Interfacing with Cassandra

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

Page 15: Lab 4: Interfacing with Cassandra

Interfacing with Cassandra in Python

Package: pycassa

Webpage: https://github.com/pycassa/pycassa

Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 8 / 16

Page 16: Lab 4: Interfacing with Cassandra

Interfacing with Cassandra in Python

Package: pycassa

Webpage: https://github.com/pycassa/pycassa

Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 8 / 16

Page 17: Lab 4: Interfacing with Cassandra

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

Page 18: Lab 4: Interfacing with Cassandra

Read

1 readData = col_fam.get(’John’,

2 columns=[’email’])

Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 10 / 16

Page 19: Lab 4: Interfacing with Cassandra

Delete

1 col_fam.remove(’John’,

2 columns=[’email’])

Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 11 / 16

Page 20: Lab 4: Interfacing with Cassandra

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

Page 21: Lab 4: Interfacing with Cassandra

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

Page 22: Lab 4: Interfacing with Cassandra

Batch Read

1 readData = col_fam.multiget([’John’, ’Jane’, ’Bill’])

Zubair Nabi 4: Interfacing with Cassandra April 20, 2013 14 / 16

Page 23: Lab 4: Interfacing with Cassandra

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

Page 24: Lab 4: Interfacing with Cassandra

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