Tuukka Mustonen
Tuukka Mustonen

Reputation: 4831

Storing a list of mixed types in Cassandra

In Cassandra, when specifying a table and fields, one has to give each field a type (text, int, boolean, etc.). The same applies for collections, you have to give lock a collection to specific type (set<text> and such).

I need to store a list of mixed types in Cassandra. The list may contain numbers, strings and booleans. So I would need something like list<?>.

Is this possible in Cassandra and if not, What workaround would you suggest for storing a list of mixed type items? I sketched a few, but none of them seem the right way to go...

Upvotes: 2

Views: 1050

Answers (2)

Tuukka Mustonen
Tuukka Mustonen

Reputation: 4831

As suggested at http://www.mail-archive.com/[email protected]/msg37103.html I decided to encode the various values into binary and store them into list<blob>. This allows to still query the collection values (in Cassandra 2.1+), one just needs to encode the values in the query.

On python, simplest way is probably to pickle and hexify when storing data:

pickle.dumps('Hello world').encode('hex')

And to load it:

pickle.loads(item.decode('hex'))

Using pickle ties the implementation to python, but it automatically converts to correct type (int, string, boolean, etc.) when loading, so it's convenient.

Upvotes: 2

medvekoma
medvekoma

Reputation: 1179

Cassandra's CQL interface is strictly typed, so you will not be able to create a table with an untyped collection column.

I basically see two options:

  • Create a list field, and convert everything to text (not too nice, I agree)
  • Use the thift API and store everything as is.

Upvotes: 2

Related Questions