简体   繁体   中英

Create table in Cassandra with a column of tuples of arbitrary length

I am trying to create a table that fits the following data:

[("US",20150914,(("GOV",7),("POL",9))),("PA",20150914,(("EDU",7),("POL",9),("MON",20))),("US",20150914,(("GOV",7)))]

I have create the following table:

CREATE TABLE gdelt.world_patterns (country varchar, date int, mention tuple <tuple<text, int>,tuple<text, int>,tuple<text, int>>, PRIMARY KEY ( country, date ) );

My problem is that Cassandra will only store my tuples of length three correctly. Is it possible for me to store tuples of any length? I'm not sure how to write this.

Here is a picture of my current table: 在此处输入图片说明

CREATE TABLE gdelt.world_patterns (
    country varchar, 
    date int, 
    mention text, ---- Json
PRIMARY KEY ( country, date ) );

Save tuple value as Json string, this way you don't have to worry about number of tuples.

        {"tuple": {
      "country": "us",
      "date": "20150704",
      "mention": 
            [
          {"text": "value"},
          {"text": "value"},
          {"text": "value"},
           ]
    }}

Optionaly you can put contry and date in json as well.

You have multiple options here: (store as text, which is ok but I don't think that you really want it)

Using a map (might be best but don't know if you want to bother with decoding)

CREATE TABLE world_patterns (
    country varchar,
    date int,
    mention map<text, int>,
    PRIMARY KEY ( country, date )
);

INSERT INTO world_patterns(country, date, mention) values ('LA', 20150704, { 'US' : 20150914, 'GOV': 7, 'POL':  9}) ;

Use a list of tuples (just slight modification of your thing [] instead of () )

  CREATE TABLE world_patterns (
    country varchar,
    date int,
    mention mention list<frozen<tuple<text,int>>>,
    PRIMARY KEY (country, date)
  );

  INSERT INTO world_patterns(country, date, mention) values ('LA', 20150704, [('US', 20150914), ('GOV',7), ('POL',9)]) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM