首页 > 解决方案 > 如何从 CQL 表中的 JSON 列中选择键作为列?

问题描述

我在 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;就像下面的结果: 在此处输入图像描述

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

太感谢了

标签: jsoncassandracql

解决方案


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

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_keywith 查询选择任一行,例如:

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

或选择餐厅的所有内容:

select * from new_table where idRestaurant = ?

推荐阅读