Reputation: 4023
I have next question:
I use the hset()
procedure for Redis Server in python. This procedure is described here.
ok, now I couldn't understand the parameter value
... From one hand it should be a number 0 or 1, anyway I could use also other numbers for this parameter (and I also find examples in net with other values). For example I define next def:
def setHashKeyValue(key, value, number):
assert (key != None), "Please, get a key"
#assert (number == 0 or number ==1), "Please enter 0 or 1"
redis_server.hset(key, value, number);
And got right output with the next parameters:
setHashKeyValue('5', 'test ok for key 5', 1)
setHashKeyValue('6', 'test ok for key 6', 1)
On the other hand, I would like to define some loop for time measuring, so I define next def:
def loopKeyValues(number):
timeUse = []
for x in range(number):
start = time.time()
setHashKeyValue(x, x**2, 1)
end = time. time()
timeUse.append(end-start)
plt.plot(timeUse)
plt.ylabel("time")
plt.show()
return timeUse;
and
print loopKeyValues(1000)
This function return me an error message for HashValue:
redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
When I use set()
-def instead of hset()
, the program return me a nice time plot. What could be a problem here and what means the parameter value
?
Upvotes: 8
Views: 37276
Reputation: 73276
The value parameter is just the key of your hash object entry.
Hash objects are like Python dictionaries. They provide associative arrays. When you write:
redis_server.hset(key, value, number)
it means the hash object "key" will be added/set with an entry "value"/"number". In Python, you would write:
key[value] = number
You got an error because in Redis the objects are typed. Once you have stored a string at a given key, Redis cannot consider anymore this object is a hash object: so the operations associated to a hash object will not work for this key.
In other words, if you have:
SET 10 100
then you cannot do:
HSET 10 100 1
However you could do:
DEL 10
HSET 10 100 1
I would suggest to use the redis-cli program to experiment with Redis before using Python scripts.
Upvotes: 17