使用python将值存储到cassandra中

olhwl3o2  于 2021-06-14  发布在  Cassandra
关注(0)|答案(1)|浏览(457)

我将从文件中获取的json存储到cassandra中。但是每次我得到新数据时我都想增加id,现在它正在覆盖现有的数据。我希望结果如下所示。

PacketID    PacketValue

1              Json
2              Json
3              Json

这是我的密码。

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()
uqcuzwp8

uqcuzwp81#

我通过使用uuid.uuid(1)修复了它

相关问题