8

I want to create this type of a structure in redis: (its basically json data)

{
    "id": "0001",
    "name":"widget ABC",
    "model": "model123",
    "service":"standard",
    "admin_password": 82616416,
    "r1":
        {
            "extid":"50000",
            "password":"test123",
        },
    "r2":
        {
            "ext":"30000",
            "password":"test123",
        },
}

What I've tried so far:

I've tried to create a hash without the "sub" hashes, just to make sure I have the basics down pat. So here's what I started with from the redis-cli:

HMSET widget:1 id 0001 name 'widget ABC' model 'model123' service standard admin_password 82616416
HMSET widget:2 id 0002 name 'widget ABC' model 'model123' service standard admin_password 12341234

That seems to work. I can see that I have 2 "records" of widget data in a widget collection.

But my attempt to create a record with the r1 data fails.

This is what I tried:

HMSET widget:3 id 0002 name 'widget ABC' model 'model123' service standard admin_password 12341234 r1{extid 50000} 

This creates a hash key "r1{extid" with a hash value of "50000}"

Any suggestions would be appreciated. I think my problem is my lingo. I just don't know enough about the redis syntax to know what words to use in my google search.

Perhaps I just have to "flatten" the data like so:

HMSET widget:3 id 0002 name 'widget ABC' model 'model123' service standard admin_password 12341234 r1_extid 50000 r1_password test123 r2_extid 30000 r2_password test123

Thanks.

12

Redis doesn't support nested data structures, and specifically it doesn't support a Hash inside a Hash :) You basically have a choice between two options: either serialize the internal Hash and store it in a Hash field or use another Hash key and just keep a reference to it in a field of the outer Hash.

0

I suggest ReJSON. It is built to handle these issues exactly.

You can use the path notation to access nested elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.