在多线程中使用python cassandra驱动程序写入db

时间:2015-10-15 14:42:46

标签: python multithreading cassandra

我正在编写一个小的Python脚本来将数据异步到Cassandra,但在调整代码方面需要一些帮助。

现在我有这个有效:

from cassandra.cluster import Cluster
cluster = Cluster(['111.111.111.111'])

session = cluster.connect('testingKeyspace')

with open('dataImport.txt') as f:
    for line in f:
        session.execute (
            """
            INSERT INTO tstTable (id, accts, info)
            VALUES (%s)
            """ $ (line)
        )

但当我像这样在异步中运行时:

from cassandra.cluster import Cluster
cluster = Cluster(['111.111.111.111'])

session = cluster.connect('testingKeyspace')

with open('dataImport.txt') as f:
    for line in f:
        session.execute_async (
            """
            INSERT INTO tstTable (id, accts, info)
            VALUES (%s)
            """ $ (line)
        )

我收到此错误:

", consistency=Not Set>' request_id=187 result=(no result yet) exception=None host=10.205.111.43>
<ResponseFuture: query='<SimpleStatement query="

为什么异步会给我这个错误而不确定单个执行。

公平警告......我第一次在学习模式中使用python ...

1 个答案:

答案 0 :(得分:0)

让它运转起来:

with open('dataImport.txt') as f:
    for line in f:
        query = SimpleStatement (
            "INSERT INTO tstTable (id, accts, info) VALUES (%s) " %(line),
            consistency_level=ConsistencyLevel.ONE)
        session.execute_async (query)