python 操作cassandra

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)
   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值