码迷,mamicode.com
首页 > 编程语言 > 详细

Cassandra Demo--Python操作cassandra

时间:2019-02-24 23:08:36      阅读:611      评论:0      收藏:0      [点我收藏+]

标签:create   nbsp   level   _id   dex   oca   prepare   ons   and   

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

创建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

标签:create   nbsp   level   _id   dex   oca   prepare   ons   and   

原文地址:https://www.cnblogs.com/gaogao67/p/10428498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!