繁体   English   中英

用一列任意长度的元组在Cassandra中创建表

[英]Create table in Cassandra with a column of tuples of arbitrary length

我正在尝试创建一个适合以下数据的表:

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

我已经创建了下表:

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只会正确存储长度为三的元组。 我可以存储任何长度的元组吗? 我不确定该怎么写。

这是我当前表的图片: 在此处输入图片说明

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"},
           ]
    }}

可选地,您也可以将contry和date放在json中。

您在这里有多种选择:(存储为文本,可以,但是我认为您并不是真的想要它)

使用地图(可能是最好的,但不知道是否要打扰解码)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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