我使用cassandra来存储我从客户端程序获得的数据。我有一个服务器程序,在那里我运行线程插入值,但没有一个队列,它不能像预期的那样工作,但我尝试使用队列,不知道我哪里出错了。有人能告诉我如何多次访问insert语句吗。
客户端程序:
import socket
import threading
import client_to_orc
import json
import pyangbind.lib.pybindJSON as pybindJSON
def con_server(server_host, server_port, thread_num):
server_address = (server_host, server_port)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#print(sock.getsockname())
---
# sw is the packets code comes here
---
packet=(pybindJSON.dumps(sw)) ----> Data in string format
sock.sendto(packet.encode("Utf-8"), server_address)
data = sock.recv(4096)
data = data.decode()
print('Client:' + data)
if __name__ == "__main__":
SERVER_ADDR = ("localhost", 4242)
threads = []
for thread_num in range(10):
thread_args = (SERVER_ADDR[0], SERVER_ADDR[1], thread_num)
t = threading.Thread(target=con_server, name='con_servr', args=thread_args)
t.start()
threads.append(t)
for t in threads:
t.join()
已编辑服务器:
def insert_Vmac(msg):
cluster = Cluster(contact_points=['172.17.0.2'])
session = cluster.connect()
id=1
session.execute("""INSERT INTO mykeyspace.hello(PacketID, PacketValue)
VALUES(%s,%s)""",
(id,msg))
def talkToClient(ip):
logging.info("sending 'clients we received your data' to %s",ip )
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.sendto("ok".encode('utf-8'), ip)
def listen_clients(Host,port):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((Host,port))
client_list=[]
while True:
msgg, client = sock.recvfrom(1024)
message = msgg.decode("utf-8")
msg = json.dumps(message)
print('connected with : ' + client[0]+ ':' + str(client[1]))
for line in msg:
cluster = Cluster(contact_points=['172.17.0.2'])
session = cluster.connect()
id = 0
query = SimpleStatement("""INSERT INTO
mykeyspace.hel(PacketID, PacketValue)
VALUES(%s,%s)""" %(id, msg))
session.execute_async(query)
print('hello inserted')
t = threading.Thread(target=talkToClient, args=(client,))
t.start()
if __name__=="__main__":
t2 = ThreadWithReturnValue(target=listen_clients, args=("localhost", 4242,))
t2.start()
编辑的代码只存储一次数据。但我发送了10次数据,但它并没有存储所有的数据。
1条答案
按热度按时间dgtucam11#
您对所有请求使用相同的id,因此每次写入都将覆盖上一个条目。这就是为什么读取数据时只检索一行。
这是假设“id”是表中的主键。