pycassa – Gettting started with Apache Cassandra using python

Install easy_install and pip


# install easy_install
sudo apt-get install -y python-setuptools

# install python-pip
sudo apt-get install -y python-pip

Install pycassa


sudo pip install pycassa

Test pycassa

cat > cassandra_test.py <<-"EOF"

#-*- coding: utf-8 -*-
import sys, os, cPickle, hashlib, pprint
from datetime import datetime
from collections import defaultdict
import pycassa
from pycassa.system_manager import *
from pycassa.marshal import *
from pycassa.types import *
from pycassa import *
from pycassa.columnfamilymap import ColumnFamilyMap
from time import sleep

"""
pyCassaShell.cmd
SYSTEM_MANAGER.create_keyspace('Keyspace1', strategy_options={"replication_factor": "1"})
SYSTEM_MANAGER.create_column_family('Keyspace1', 'Users', comparator_type=INT_TYPE)
SYSTEM_MANAGER.alter_column_family('Keyspace1', 'Users', key_cache_size=100)
SYSTEM_MANAGER.create_index('Keyspace1', 'Users', 'birthdate', LONG_TYPE, index_name='bday_index')
SYSTEM_MANAGER.drop_keyspace('Keyspace1')

>>> from pycassa.system_manager import *
>>> sys = SystemManager('192.168.10.2:9160')
>>> # Create a SimpleStrategy keyspace
>>> sys.create_keyspace('SimpleKS', SIMPLE_STRATEGY, {'replication_factor': '1'})
>>> # Create a NetworkTopologyStrategy keyspace
>>> sys.create_keyspace('NTS_KS', NETWORK_TOPOLOGY_STRATEGY, {'DC1': '2', 'DC2': '1'})
>>> sys.close()
"""
def main():
    database='mytestdb'
    table ='mytable'

    super_cf=False # consider super columns to be deprecated
    s = SystemManager()
    if database not in s.list_keyspaces():
        s.create_keyspace(database, SIMPLE_STRATEGY, {'replication_factor': '1'})
    if table in s.get_keyspace_column_families(database):
        s.drop_column_family(database, table)
    if table not in s.get_keyspace_column_families(database):
        print("creating new table")
        s.create_column_family(database, table, super=super_cf, comparator_type=ASCII_TYPE)
    s.close()
    pool = pycassa.ConnectionPool(database)
    cf = pycassa.ColumnFamily(pool, table)

    # create data
    mydata = {'key1': {'col1': 'data1'},
              'key2': {'col1': 'data2'},}

    # insert data
    idx = cf.insert("key1", {'col1': 'key1_data1', 'col2': 'key1_data2'})
    print(idx)

    idx = cf.insert("key2", {'col1': 'key2_data1', 'col2': 'key2_data2'})
    print(idx)

    r = cf.get("key1")
    print(r)

    r = cf.get("key2")
    print(r)

if __name__ == "__main__":
    main()
EOF

python cassandra_test.py

References

1. Home: http://pycassa.github.com/pycassa/api/index.html
2. API: http://pycassa.github.com/pycassa/api/index.html
3. Documentation: http://pycassa.github.io/pycassa/
4. Tutorial: http://pycassa.readthedocs.org/en/latest/tutorial.html
5. Super Columns are Deprecated !


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *