简体   繁体   English

使用python将值存储到cassandra中

[英]Storing Values into cassandra using python

I am storing the json i get from a file into cassandra. 我将json从文件存入cassandra。 But i would like to increment the id everytime i get new data and now it is overwriting the existing data. 但我希望每次获得新数据时都会增加id,现在它会覆盖现有数据。 I expect result to be as shown below. 我希望结果如下所示。

PacketID    PacketValue

1              Json
2              Json
3              Json

Here is my code. 这是我的代码。

import json
doc= (json.dumps(sw.get(),indent=4))
KEYSPACE = "mykeyspace"

def createKeySpace():
    cluster = Cluster(contact_points=['172.17.0.2'])
    session = cluster.connect()
    session.execute(""" CREATE KEYSPACE IF NOT EXISTS %s WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '2' } """ % KEYSPACE)
    session.set_keyspace(KEYSPACE)
    session.execute("CREATE TABLE IF NOT EXISTS wireless (PacketID int , PacketValue text, PRIMARY KEY(PacketID) );")

def insert_wireless():
    cluster = Cluster(contact_points=['172.17.0.2'])
    session = cluster.connect()
    PacketID=0
    PacketID=PacketID+1
    session.execute("""INSERT INTO mykeyspace.wireless(PacketID, PacketValue)
    VALUES(%s,%s)""",
    (PacketID,doc))


if __name__=="__main__":
    #start_node()
    th1=threading.Thread(target=createKeySpace())
    th1.start()
    threads=threading.Thread(target=insert_wireless())
    threads.start()
    threads.join()

我用Uuid.uuid(1)修复它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM