使用任意长度的元组列在 Cassandra 中创建 table

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

我正在尝试创建适合以下数据的 table:

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

我创建了以下 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 ) );

我的问题是 Cassandra 只能正确存储长度为 3 的元组。我可以存储任意长度的元组吗?不知道怎么写。

这是我现在的照片table:

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

将元组值保存为Json字符串,这样你就不用担心元组的数量了。

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

您也可以选择在 json 中输入国家和日期。

这里有多种选择: (存储为文本,即 o.k。但我不认为你真的想要它)

使用地图(可能是最好的,但不知道你是否想麻烦解码)

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}) ;

使用元组列表(只需稍微修改一下 [] 而不是 () )

  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)]) ;