Cassandra Demo--Python操作cassandra

Posted gaogao67

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cassandra Demo--Python操作cassandra相关的知识,希望对你有一定的参考价值。

================================================================

创建keyspace和table

CREATE KEYSPACE exps  
WITH REPLICATION = { class : NetworkTopologyStrategy, DC1 : 2 };

create table exps_user(user_id int primary key,user_name varchar,user_info text);

create index idx_user_name on exps_user(user_name);

 

================================================================

需要安装python依赖包cassandra-driver:

from cassandra.cluster import Cluster
from cassandra.policies import DCAwareRoundRobinPolicy
from cassandra import ConsistencyLevel

cluster_instances = [192.168.199.171, 192.168.199.172, 192.168.199.173]
cluster = Cluster(
    cluster_instances,
    load_balancing_policy=DCAwareRoundRobinPolicy(local_dc=DC1),
    port=9042
)

session = cluster.connect("exps")
for user_id in range(1, 50000):
    try:
        sql_script = ("insert into exps_user(user_id,user_name,user_info)"
                      "VALUES({0},‘U{0}‘,‘THIS IS TEST‘);").format(user_id)
        user_lookup_stmt = session.prepare(sql_script)
        user_lookup_stmt.consistency_level = ConsistencyLevel.LOCAL_ONE
        user1 = session.execute(user_lookup_stmt, [])
    except Exception as ex:
        print(str(ex))

 

以上是关于Cassandra Demo--Python操作cassandra的主要内容,如果未能解决你的问题,请参考以下文章

cassandra高级操作之JMX操作

检查cassandra中的子集

ansible快速部署cassandra3集群

JAVA操作cassandra数据库

从 c#datastax 驱动程序关闭 cassandra 集群时出错

如何查看长期运行的 Cassandra 操作的进度?