简体   繁体   中英

Redis get data using python

I have done the following to get json file data into redis using this python script-

import json
import redis
r = redis.StrictRedis(host='127.0.0.1', port=6379, db=1)
with open('products.json') as data_file:
    test_data = json.load(data_file)
r.set('test_json', test_data)

When I use the get commmand from redis-cli (get test_json) I get nil back. I must be using the wrong command? Please help for my understanding on this.

You should use hmset instead of set and hgetall instead of get to store multiple keys, your code should look like:

r.hmset('test_json', test_data) #to set multiple index data
r.hgetall('test_json') #to get multiple index data

I deleted the previous answer. Didn't noticed the problem there is that u specified 'db=1' in the redis constructor. So you are saving the data in the db 1. type 'select 1' in the redis client, or remove that from the constructor (by default, with redis-cli you connect to database 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM