python-3.x - 尝试从 python 写入 cassandra 时 CQL 查询中出现语法错误

标签 python-3.x cassandra kafka-consumer-api cassandra-driver

所以,我正在用 python 构建一个应用程序,它从 twitter 获取数据,然后将其保存到 cassandra。我当前的问题在于一个从kafka读取数据并尝试将其写入cassandra的脚本,如下所示:

import threading, logging, time
import multiprocessing
from cassandra.cluster import Cluster

from kafka import KafkaConsumer, KafkaProducer




class Consumer(multiprocessing.Process):
   def __init__(self):
        multiprocessing.Process.__init__(self)
        self.stop_event = multiprocessing.Event()

    def stop(self):
         self.stop_event.set()

    def run(self):
       consumer = KafkaConsumer(bootstrap_servers='localhost:9092',
                                 auto_offset_reset='earliest',
                                 consumer_timeout_ms=1000)
        consumer.subscribe(['twitter'])



    while not self.stop_event.is_set():
        for message in consumer:
            # session.execute(
            #     """
            #     INSERT INTO mensaje_73 (tweet)
            #     VALUES (message)
            #     """
            # )
            print(message)
            cluster = Cluster()
            session = cluster.connect('twitter')
            session.execute(
                    """
                    INSERT INTO mensaje_73 (tweet)
                    VALUES (message)
                    """
                )

            # if self.stop_event.is_set():
            #     break

    consumer.close()


   def main():

    tasks = [
        Consumer()
    ]

    for t in tasks:
        t.start()

    time.sleep(10)

    for task in tasks:
        task.stop()



if __name__ == "__main__":
     logging.basicConfig(
        format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:% 
   (levelname)s:%(process)d:%(message)s',
        level=logging.INFO
    )
    main()

我尝试将测试消息插入表 twitter.mensaje_73 并且它运行良好,如下所示:

import threading, logging, time
import multiprocessing
from cassandra.cluster import Cluster

from kafka import KafkaConsumer, KafkaProducer


cluster = Cluster()
session = cluster.connect('twitter')
session.execute(
    """
    INSERT INTO mensaje_73 (tweet)
    VALUES ('helooo')
    """
)

任何帮助将不胜感激:)

最佳答案

所以这里的问题是,您的 message 变量在 CQL 中被视为文字,如果没有单引号,它将无法工作。因此,出现错误。

为了解决这个问题,我将使用准备好的语句,然后将 message 绑定(bind)到它:

session = cluster.connect('twitter')
preparedTweetInsert = session.prepare(
        """
        INSERT INTO mensaje_73 (tweet)
        VALUES (?)
        """
    )
session.execute(preparedTweetInsert,[message])

尝试一下,看看是否有帮助。

此外,这似乎是一个简单的数据模型。但要问自己的一件事是,您将如何查询这些数据?除非 tweet 是您唯一的主键,否则这将不起作用。这也意味着查询单个推文的唯一方法是通过消息的确切文本。需要考虑一些事情,但按天分区可能是一个更好的选择,因为它可以很好地分布并提供更好的查询模型。

关于python-3.x - 尝试从 python 写入 cassandra 时 CQL 查询中出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52805432/

相关文章:

python - Pycharm 显示解释器版本为 2.7 但我已经下载了 3.6?

python-3.x - 如何使用 Pandas 删除重复值并保留任何一个

Cassandra LOCAL_QUORUM

apache-kafka - ConsumerOffsets 分区分布不均

c++ - kafka消费者在zookeeper中的注册列表

apache-kafka - 为多个分区使用kafka批处理

python - 使用 python 3 使用 PyQt4 QWebView 查看 map

python - 在python套接字编程中建立连接后如何获取客户端的IP地址?

Cassandra数据库问题

java - 当 Cassandra 中特定数据中心的所有节点都关闭时执行读写操作