python 怎么玩cassandra

cassandra for python  的客户端~ 着实不如mongodb 和redis那样成熟。


目前相对靠谱的就是 pycassa  

githup: https://github.com/pycassa/pycassa

不过目测也很久没有更新了


安装:

pip install pycassa

创建一个可操作CF的连接:

import pycassa

    pool = pycassa.ConnectionPool(keyspace=ks_name, server_list=server_list, prefill=False)
    cf = pycassa.ColumnFamily(pool, cf_name)

几个核心的方法:

cf.insert('rowkey', {'column1': 'val'})
cf.get('rowkey')

cf.multiget(['key1', 'key2']) #查询多个rowkey

cf.remove('key')
cf.remove('key', columns=['column1'])

rows={'key1':{'column1': 'val'},'key2':{'column1': 'val2'},'key3':{'column1': 'val3'}}
cf.batch_insert(rows)

cassandra 的cql 是用类似sql的语句来操作 cassandra python也提供了 cql的客户端~  http://code.google.com/a/apache-extras.org/p/cassandra-dbapi2/

源码包下载地址:

http://code.google.com/a/apache-extras.org/p/cassandra-dbapi2/downloads/list


  import cql
  connection = cql.connect(host, port, keyspace)
  cursor = connection.cursor()
  cursor.execute("CQL QUERY", dict(kw='Foo', kw2='Bar', etc...))
  for row in cursor:  # Iteration is equivalent to lots of fetchone() calls
      doRowMagic(row)
 
  cursor.close()
  connection.close()


cql 的详细用法

http://www.datastax.com/docs/1.1/references/cql/index


你可能感兴趣的:(python,数据库)