通过cron作业将数据存储到Redis中 [英] storing data into redis through cron job
问题描述
我想每15分钟通过一次cron作业将数据从熊猫存储到redis中,下面是我的代码:-
I want to store data into redis from pandas through a cron job every 15 minute and below is my code:-
我每15分钟使用下面的代码将数据收集到大熊猫中,然后通过cron作业将其发送到redis字典mydict2中.
I am taking data into pandas every 15 minutes with below code and sending it to the redis dictionary mydict2 through a cron job.
import sys
import pickle
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
test_dict1 = results_df.set_index('user')['ua'].T.to_dict()
p_mydict = pickle.dumps(test_dict1)
r.set('mydict2', p_mydict)
我在键mydict2中一次又一次得到相同的输出.基本上,我想存储整个月的用户ID,并希望在月底存储唯一的ID.
I am getting the same output again and again in the key mydict2. Basically i want to store user ids for the whole month and at the end of the month i want the unique count of that.
我也正在使用set方法,假设我有大量数据,那可能是最好的方法.
Also i am using set method, what could be the best method assuming i am having a very large amount of data.
有人可以帮我吗?
推荐答案
在下面替换
p_mydict = pickle.dumps(test_dict1)
r.set('mydict2', p_mydict)
使用
for k, v in test_dict1.items():
r.hmset(k, {"ua" : v})
print("Done adding stuff")
,字典中的每个键都将是Redis中的键.
and each key in your dictionary will be a key in Redis.
这篇关于通过cron作业将数据存储到Redis中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!