1. 安装
python使用cassandra_driver库操作cassandra:
b.1、下载https://pypi.org/project/cassandra-driver/#files: cassandra-driver-3.16.0.tar.gz,解压后安装:python setup.py install
备注:3.16版本的cassandra要求Cython的版本大于0.20,小于0.29
b.2、安装cassandra-driver过程中提示缺少Cython,官网下载:https://pypi.python.org/pypi/Cython/并安装:0.28版本(https://pypi.org/project/Cython/0.28/#files)
pip install Cython-0.28-cp27-cp27mu-manylinux1_x86_64.whl
备注:安装whl文件时,请注意pip版本,更新到最新
安装Cython之后,安装cassandra成功。
2. 使用样例
# -*- encoding=utf-8 -*-
import os
import time
import datetime
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
node_ips = ["host1", "host2", "host3"]
auth_provider = PlainTextAuthProvider(username='usr', password='pwd')
cluster = Cluster(node_ips, port=9042, auth_provider = auth_provider, connect_timeout=6000)
session = cluster.connect()
session.set_keyspace('keyspace')
cluster.connect()
rows = session.execute("select * from table_name")
count = len(rows.current_rows)
print("count:", count)