【问题标题】:How can I select the key as the column from JSON column in CQL table?如何从 CQL 表中的 JSON 列中选择键作为列?
【发布时间】:2019-09-16 05:13:17
【问题描述】:

我在 CQL 中创建了下表:

CREATE TABLE new_table ( idRestaurant INT, restaurant map<text,varchar>, InspectionDate date, ViolationCode VARCHAR, ViolationDescription VARCHAR, CriticalFlag VARCHAR, Score INT, GRADE VARCHAR, PRIMARY KEY (InspectionDate )) ;

然后,我通过jar插入数据,我得到restaurant column的值就像json/dictionary

select restarutant from new_table; 类似于以下结果:

在普通 SQL 中选择 json 列的键值应该是 select json_col.key from table 但这不适用于 CQL,我如何选择 JSON 的键值作为列或 WHERE 条件过滤?

非常感谢

【问题讨论】:

    标签: json cassandra cql


    【解决方案1】:

    我最好将表的架构更改为以下,而不是使用地图:

    CREATE TABLE new_table (
      idRestaurant INT, 
      restaurant_key text,
      restaurant_value text,
      InspectionDate date, 
      ViolationCode VARCHAR,
      ViolationDescription VARCHAR, 
      CriticalFlag VARCHAR, 
      Score INT, 
      GRADE VARCHAR, 
      PRIMARY KEY (InspectionDate, restaurant_key )) ;
    

    然后您可以根据restaurant_key 选择任一单独的行,查询如下:

    select * from new_table where idRestaurant = ? and restaurant_key = ?
    

    或选择餐厅的所有内容:

    select * from new_table where idRestaurant = ?
    

    【讨论】:

      猜你喜欢
      • 2015-01-08
      • 2012-06-17
      • 1970-01-01
      • 2022-01-07
      • 2021-03-19
      • 2021-12-19
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      相关资源
      最近更新 更多