multi dimensional array

371 views
Skip to first unread message

grendo

unread,
Jun 15, 2011, 2:44:18 AM6/15/11
to Redis DB
Hi,
I am wondering what is the best way to store a data set of rows and
columns returned from a sql query. Currently I have two hashes. One
hash for the field names with column name as the key and column index
as the value, and a second hash for the field values with a key that
is made up of row_column with the value being the value from the sql
dataset. Then in my client code I can get all the keys/value from the
field name hash and use them to access individual values in the values
hash.

Is there an better way to store a 2 dimensional array in redis ?

Josiah Carlson

unread,
Jun 15, 2011, 3:50:31 AM6/15/11
to redi...@googlegroups.com
On the most part, that is the standard way of doing it. I've not seen
the key/index used before. Usually I've seen a "col1,col2,col3,..."
string in situations where column order matters, but there isn't much
of a difference either way.

If you are looking to store a group of rows at the same time in one
hash, like "store all of the rows returned by query X in a single
hash", then one way I've seen is...

<queryid> -> {
'-1': <column names>,
'0': 'column 1 data,column 2 data,...',
'1': <second row data>,
....
}

So you identify your column rows with some special key, then you
serialize your data as values as indexed entries in the hash. CSV
works pretty well, and overall you may be able to save some space by
not duplicating column names.

If you want to omit the '-1' row, and/or you don't mind duplicate
data, you can use JSON instead of CSV, which is convenient in the fact
that most clients will give you a language-specific hash as a result
instead of you having to write code to create them (you'll still have
to manually pull the rows and call your JSON decoder, but that's not
so bad).

Regards,
- Josiah

> --
> 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.
>
>

Reply all
Reply to author
Forward
0 new messages