当前位置: 首页 > 工具软件 > Python-Cactus > 使用案例 >

Pycassa 起步——python 与 cassandra

胥康安
2023-12-01

系统 fedora


一、首先安装pip

        先安装setuptools

http://pypi.python.org/pypi/setuptools#cygwin-mac-os-x-linux-other下载


 sh  setuptools-0.6c9-py2.4.egg

  

http://pypi.python.org/pypi/pip#downloads下载

解压好之后

$ cd pip-1.0

$ python setup.py install # mayneed to be root

 

至此 pip安装完毕

 二、安装Thrift

先安装gcc-c++

Yum –y installgcc-c++

 

下载thrift-0.8.0.tar.gz

解压到thrift-0.8.0

进入thrift-0.8.0

./configure

Make

Make install

至此,thrift安装完毕


 三、安装pycassa

输入命令pip  install pycassa  就可以安装pycassa了

 

到pycassa-1.5.1目录下

运行pycassaShell(pycassaShell is an interactive Python shell that isincluded with pycassa.)

即 $ ./pycassaShell

>>> SYSTEM_MANAGER.create_keyspace('Keyspace1', strategy_options={"replication_factor": "1"})

>>> SYSTEM_MANAGER.create_column_family('Keyspace1', 'ColumnFamily1')

 

>>> frompycassa.poolimport ConnectionPool

>>> pool = ConnectionPool('Keyspace1')

 

>>> frompycassa.poolimport ConnectionPool

>>> frompycassa.columnfamilyimport ColumnFamily

>>> 

>>> pool = ConnectionPool('Keyspace1')

>>> col_fam = pycassa.ColumnFamily(pool, 'ColumnFamily1')

 

>>> col_fam.insert('row_key', {'col_name': 'col_val'})

1354459123410932

 

>>> col_fam.insert('row_key', {'col_name':'col_val', 'col_name2':'col_val2'})

1354459123410932

 

>>> col_fam.batch_insert({'row1': {'name1': 'val1', 'name2': 'val2'},

...                       'row2': {'foo': 'bar'}})

1354491238721387

 

>>> col_fam.get('row_key')

{'col_name': 'col_val','col_name2': 'col_val2'}

 

>>> for i inrange(1, 10):

...     col_fam.insert('row_key', {str(i): 'val'})

...

1302542571215334

1302542571218485

1302542571220599

1302542571221991

1302542571223388

1302542571224629

1302542571225859

1302542571227029

1302542571228472

>>> col_fam.get('row_key', column_start='5', column_finish='7')

{'5': 'val', '6': 'val','7': 'val'}

 

>>> col_fam.get_count('row_key')

3

 

 

Connection Pooling

 

>>> pool = pycassa.ConnectionPool('Keyspace1', pool_size=20)

>>> standard_cf = pycassa.ColumnFamily(pool, 'Standard1')

>>> standard_cf.insert('key', {'col': 'val'})

1354491238782746

>>> super_cf = pycassa.ColumnFamily(pool, 'Super1')

>>> super_cf.insert('key2', {'column' : {'col': 'val'}})

1354491239779182

>>> standard_cf.get('key')

{'col': 'val'}

>>> pool.dispose()

 

 类似资料: