To understand how to use the hash data type, take the following steps:
- Open a Terminal and connect to Redis with redis-cli.
- Now, let's set the information properties of the "Kyoto Ramen" restaurant using the HMSET command:
127.0.0.1:6379> HMSET "Kyoto Ramen" "address" "801 Mission St, San Jose, CA" "phone" "555-123-6543" "rating" "5.0" OK
- Use the HMGET command to retrieve values in a hash:
127.0.0.1:6379> HMGET "Kyoto Ramen" "address" "phone" "rating" 1) "801 Mission St, San Jose, CA" 2) "555-123-6543" 3) "5.0"
- To retrieve the value for a single field, use HGET:
127.0.0.1:6379> HGET "Kyoto Ramen" "rating" "5.0"
- To test if a field exists in the hash, use HEXISTS:
127.0.0.1:6379> HEXISTS "Kyoto Ramen" "phone" (integer) ...