views:

361

answers:

2

I'm trying to wrap my head around Key-Value stores like CouchDB and Cassandra. I understand why they're useful but as far as how they replace a RDBMS like MySql, I don't get it.

Let's say this is my what I need to store:

{123456: {'model' : 'Ford'
          'color': 'blue'
          'MPG': 23}}

Then I need to find all the cars that are blue.

How does the Key-Value store query the keys using the value? I read some where that map-reduce may be used but looking at the source of a few projects I can't find an example.

Let me know if I'm asking the right question.

+2  A: 

You would want to maintain a separate key/value store that is essentially an index. It would have "blue" as the key, and then a list of all IDs from the "main" cars store that are blue.

Yes, that is duplicating the index functionality of a regular rdbms.

This is a good article about how the FriendFeed team approached this problem and settled on this solution, along with their rationale (I know, a bit strange since they used an RDBMS as a key/value store, but the talking points are sound theory):
http://bret.appspot.com/entry/how-friendfeed-uses-mysql

Joel Martinez
+3  A: 

Essentially, when you are using key-value stores, you are building a database out of the same components that a relational database has internally. The reason for doing this is to have more control and flexibility over scaling and performance, or just for straightforwardness.

In this case, you need to store the equivalent of the table rows and the index as two separate things. So if you want to index on color, then you need to store

{'blue': {123456}}

in the equivalent of an index table.

Of course, some key-value stores provide indexing and searching mechanisms for you so there is no general rule that fits all.

Michael Dillon
Would it need to be:{{'color':'blue'}:[123456]}Otherwise how would it know what blue was referring to.What about text?{'Lorem ipsum dolor sit amet, consectetuer adipiscing elit...':[123456]}Can key-value stores do like searches?
I was assuming that the index was going to only contain color as a key. The details of this depend on the key-value store that you are going to use. You mentioned two different ones in your answer so I assumed the question was more of a general key-value store question. Redis, Hadoop, Tokyo Cabinet, Metakit, Berkeley db, gdbm all provide different capabilities for key-value stores.
Michael Dillon