【问题标题】:Inserting data into user defined type in Cassandra在 Cassandra 中将数据插入用户定义的类型
【发布时间】:2017-09-23 00:18:03
【问题描述】:

我在向我的 Cassandra 表插入 JSON 和更新数据时遇到问题。

我有一个结构如下的表:

CREATE TYPE keyspace.connected_hw (
    connected_element text,
    support_list frozen<list<text>>
);

CREATE TABLE keyspace.sample_table (
    id text,
    name text,
    connected_items frozen<keyspace.connected_hw>
)

我的插入脚本如下所示:

INSERT INTO keyspace.sample_table JSON '
  {
    "id": "12345",
    "name": "object 1",
    "connected_items": [
      {
        "connected_element": "56789",
        "support_list": ""
      }
    ]
  }

'

运行此程序后,出现以下错误:

Error decoding JSON value for connected_items: Expected a map, but got a ArrayList :

我尝试先插入没有“connected_items”列表的其他字段,然后再更新它:

INSERT INTO keyspace.sample_table JSON '
  {
    "id": "12345",
    "name": "object 1",
  }

'

在我尝试使用

更新“connected_items”之前,此方法有效
UPDATE keyspace.sample_table
SET connected_items = [{connected_element:'56789',support_list:['type1','type2']}] 
where id='12345'

运行后出现以下错误:

InvalidQueryException: Invalid list literal for connected_items of type frozen<connected_hw>

【问题讨论】:

  • sample_table 的主键是什么?

标签: database cassandra cql user-defined-types


【解决方案1】:

您将connected_items 声明为connected_hw 类型,但您将值插入为“connected_hw”列表,并且support_list 是text 的列表,而不仅仅是text

您可以更改插入/更新格式,如下所示:

INSERT INTO sample_table JSON '{"id": "12345", "name": "object 1", "connected_items": { "connected_element": "56789", "support_list": ["a","b"] } }' ;

还有

UPDATE sample_table SET connected_items = {connected_element:'56789',support_list:['type1','type2']}  where id='12345' ;

或将 connected_items 的类型更改为 connected_hw 列表

ALTER TABLE sample_table DROP connected_items ;
ALTER TABLE sample_table ADD connected_items list<frozen <connected_hw>>;

【讨论】:

  • 谢谢,我更新了我的表结构,效果很好。
猜你喜欢
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 2014-12-31
  • 2018-07-02
  • 2021-08-25
  • 2018-12-15
  • 2019-11-09
  • 1970-01-01
相关资源
最近更新 更多