所以,我正在用 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')
"""
)
任何帮助将不胜感激:)