madiha malik
madiha malik

Reputation: 977

cassandra “map” column type, mapping literals

My question is, that if map data type of Cassandra supports 3 literals.. in example below, a mapping between two literals is declared..

CREATE TABLE abc(id text PRIMARY KEY, fans map<text, text>);

Can I use...

CREATE TABLE abc(id text PRIMARY KEY, fans map<text, text, int>);

Upvotes: 2

Views: 1169

Answers (2)

Andy Tolbert
Andy Tolbert

Reputation: 11638

In addition to BryceAtNetwork23's answer, you can also create a map of text -> (text, int) in the following way:

CREATE TABLE abc (
  id text PRIMARY KEY,
  fans map<text,frozen<tuple<text,int>>>
);

You can then make updates like:

update abc set fans = {'a' : ('hello', 1) } where id = 'helloworld';

And retrieve data like:

cqlsh:test> select * from abc;

  id         | fans
 ------------+---------------------
  helloworld | {'a': ('hello', 1)}

Upvotes: 5

Aaron
Aaron

Reputation: 57748

A map isn't going to work that way. Instead, try using a LIST of TUPLE like this:

CREATE TABLE abc(
  id text PRIMARY KEY,
  fans List<FROZEN<TUPLE<text, text, int>>>
);

Note that you'll also need to use the FROZEN keyword, which is required for tuples and user defined types.

Upvotes: 2

Related Questions