2

I have next question: is it possible to create column family in cassandra that would look like this below and how to define it's structure:

Users = {   //CF
     AlexS : { //row key
         Address : { //supercolumn
               state: "NJ",
               country: "US",
               phone : { //super column
                    zip: "00283",
                    number : { // supercolumn
                       home: "23756511",
                       mobile : "23756512"
                    } 
               } 
         }
     }   
}
Kitamo
  • 87
  • 1
  • 1
  • 12

2 Answers2

7

Nested super column within super column for four levels? As far as I know, that is not possible. Because you need to specify by referencing which column family of that current insertion you want it to be. Consider the following, you can have define top level column family to be of type column family super. Then you can have multiple super-columns within this super column family and this supercolumns will have multiple column=value pairs those are called as subcolumns. For example,

list Users;    
Using default limit of 100
-------------------
RowKey: AlexS
=> (super_column=Address,
     (column=country, value=US, timestamp=1331614891360000)
     (column=state, value=NJ, timestamp=1331614891355000))
=> (super_column=number,
     (column=home, value=23756511, timestamp=1331614891406000)
     (column=mobile, value=23756512, timestamp=1331614891406001))
=> (super_column=phone,
     (column=zip, value=00283, timestamp=1331614891396000))

1 Row Returned.
Elapsed time: 21 msec(s).
Community
  • 1
  • 1
Jasonw
  • 4,916
  • 7
  • 37
  • 45
  • 1
    Well, as I haven't found any docs or info that this is possible + your answer, I consider that this is really impossible. So, only 2 types of column family are available: standart and super; and column family of type super only gives additional top level column to hold subcolumns. – Kitamo Mar 13 '12 at 08:07
  • Yes, probably not now, in time maybe it will be support. Declare a column family of type super and contain many standard column family. For each of the standard column family, you will store many columnName and columnValue. – Jasonw Mar 13 '12 at 09:04
  • 1
    You might consider using composite types for the Key, and then have 3 separate keys: AlexS:Address, AlexS:number, Alexs:phone, etc. And avoid SuperColumns. One benefit, can add indexes for any column (only available with standard CF) – libjack Mar 13 '12 at 15:15
  • Jack, do you mean the rowkey is the user name (AlexS in this case) and with this comparator (utf8:utf8) ? That is AlexS:Address, AlexS:number and AlexS:phone? – Jasonw Mar 15 '12 at 10:49
2

Yet another option is to store part or all of the user entity as JSON, since that's how you're representing it. Then if you need to do a reverse lookup, simply write your own index to do so. In that case your key is "AlexS", then you'd have an "Address" column that would contain the JSON representation (or whatever serialization you want to use) of the address data.

Then, if you want to lookup by phone number, you'd have a home-grown index with key=phone number, then a column for each user id.

This is a pretty common approach with Cassandra.

rs_atl
  • 8,825
  • 1
  • 20
  • 28