relation one to many in redis

2,105 views
Skip to first unread message

Aziz Nait Oumghar

unread,
Apr 30, 2011, 7:43:30 AM4/30/11
to redi...@googlegroups.com
I'm working actually on redis and I want to know how to make a relation one to many in it

thnx

Dvir Volk

unread,
Apr 30, 2011, 9:29:29 AM4/30/11
to redi...@googlegroups.com
Hi
your question is too general. there are a few ways to do that, depending on how you build your data.
generally speaking, you use lists, sets or sorted sets that contain ids of other objects, to express that.
if you can be a bit more specific, it will be possible to give you more specific advice.


On Sat, Apr 30, 2011 at 2:43 PM, Aziz Nait Oumghar <aziznai...@gmail.com> wrote:
I'm working actually on redis and I want to know how to make a relation one to many in it

thnx

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.

Aziz Nait Oumghar

unread,
Apr 30, 2011, 10:34:02 AM4/30/11
to redi...@googlegroups.com

I'm working on news with this attributes

unique id
title
url
content
...


and for each article talk about many countryies and many personnes
I hope it is more clear.

Geoffrey Hoffman

unread,
Apr 30, 2011, 12:10:01 PM4/30/11
to redi...@googlegroups.com
As you probably know already, Redis is not a database, much less a relational database. With that said, consider the type of keys you want to store, and carefully naming your keys, then storing sets of specific keys in another key. This is just one way to achieve relationships, though admittedly it requires fairly tight integration with client code (php, java etc). Hashes are most analogous to DB tables. So you might do something like this (php pseudocode $variable)...


while(list($news_id, $title, $url, $content) = mysq_fetch_array($result)){
  HMSET news:$news_id title $title url $url content $content
  SADD news:list news:$uid
}

while( list( $country_id, $iso, $name = mysql_fetch_array($result)){
   HMSET country:$country_id iso $iso name $name 
   SADD country:list country:$country_id
}

// Then you make sets of related keys:

foreach($news_country as $news_id => $country_id){
   SADD news_countries:$news_id country:$country_id
}

Now which countries are represented in news_id 17?

SMEMBERS news_countries:17   

Is news 12 about country 4?

SISMEMBER news_countries:12 4  



Does that help at all?




Aziz Nait Oumghar

unread,
Apr 30, 2011, 2:10:00 PM4/30/11
to redi...@googlegroups.com
Iit is useful but I-m working with java

thnx

Josiah Carlson

unread,
Apr 30, 2011, 3:31:32 PM4/30/11
to redi...@googlegroups.com
Do you want "tags"? Do you want all articles that talk about person X
to be able to be found? What do you want to be able to discover?

- Josiah

Aziz Nait Oumghar

unread,
May 1, 2011, 8:57:13 AM5/1/11
to redi...@googlegroups.com
to be more clear , for example in mongodb (oriented document database)the structure of an articles is like this :

{_id: 1314153 ,

  title:"  "
  url: "   "
 content: "    "
 country : {country 1,
                country 2,
                country 3,
                          }
persons : { person1,
                person 2,
                person 3,
                          }
}

how can I perform this in redis I'm talking about relation one to many

thnx

Pierre Chapuis

unread,
May 1, 2011, 9:51:40 AM5/1/11
to redi...@googlegroups.com
> how can I perform this in redis Im talking about relation one to many

There are several possibilities. An obvious one would be:

user:1314153 -> Hash( title -> " ", url -> " ", content -> " ")
user:1314153:contries -> Set(country 1, country 2, country 3)
user:1314153:persons-> Set(person 1, person 2, person 3)

--
Pierre 'catwell' Chapuis

Didier Spezia

unread,
May 1, 2011, 9:58:10 AM5/1/11
to Redis DB
Hi,

there are several ways to do it - the choice mainly
depends on the access pattern.

For instance you can build a JSON object like with mongodb
and store it directly in a Redis string.

So you would have three kinds of objects:
- documents (as in the mongodb example)
- countries
- persons

Up to your application to serialize/deserialize the documents
to set/get the various fields, including references to countries
and persons. This works well if the number of countries and persons
per document is limited.

If you want to support partial updates, you need to split your
document on various keys. For instance:

doc:ID -> hash for title, url and content
doc_countries:ID -> list of references of countries
doc_persons:ID -> list of references of persons

Now you can update each individual field, and maintain the list
of countries and persons associated to a document without having
to manage serialization/deserialization of the whole document.

With both solutions, you can retrieve the data for a given document
in just 2 roundtrips (if you exploit Redis pipelining):
- one to retrieve the document and its data
- one to retrieve the data associated to countries and persons

However, you cannot get the list of documents associated to a
specific country or person. If you want to support such queries,
you need to add extra sets for this. For instance:

country_doc:FR -> set of document IDs associated to France
person_doc:123 -> set of document IDs associated to person 123

Up to you to maintain these extra sets.

So Redis is quite flexible, but you need to anticipate your access
patterns, structure your data accordingly, and manage all indexing
by yourself.

Regards,
Didier.


On May 1, 2:57 pm, Aziz Nait Oumghar <aziznaitoumg...@gmail.com>
wrote:

Aziz Nait Oumghar

unread,
May 1, 2011, 6:44:03 PM5/1/11
to redi...@googlegroups.com

it is more clear now , thank you.

an other question : how can I do the sharding in redis db ?

Geoffrey Hoffman

unread,
May 1, 2011, 9:02:19 PM5/1/11
to redi...@googlegroups.com
In the current release you have to do it client side (that is, write your own code.)

Here's how one guru does it:


On Sun, May 1, 2011 at 3:44 PM, Aziz Nait Oumghar <aziznai...@gmail.com> wrote:

it is more clear now , thank you.

an other question : how can I do the sharding in redis db ?

--

Jeremy Zawodny

unread,
May 1, 2011, 10:47:11 PM5/1/11
to redi...@googlegroups.com
BTW, I recently got approval to release the underlying Perl code.  I'll post here when it's public--probably in a week or so.

Jeremy

Aziz Nait Oumghar

unread,
May 2, 2011, 5:01:22 AM5/2/11
to redi...@googlegroups.com

thanks.
Reply all
Reply to author
Forward
0 new messages