在AWS红移中查询JSON字符串
我的AWS红移数据库中有一个字段在AWS红移中查询JSON字符串,json,amazon-web-services,amazon-redshift,Json,Amazon Web Services,Amazon Redshift,我的AWS红移数据库中有一个字段varchar(65000)列,用于存储JSON字符串。JSON键/值对经常更改,我需要能够运行每日报告从列中检索所有键/值数据 例如: create table test.json(json varchar(65000)); insert into test.json select '{"animal_id": 1, "name": "harry", "animal_type": "cat", "age": 2, "location": "oakland"}'
varchar(65000)
列,用于存储JSON字符串。JSON键/值对经常更改,我需要能够运行每日报告从列中检索所有键/值数据
例如:
create table test.json(json varchar(65000));
insert into test.json
select '{"animal_id": 1, "name": "harry", "animal_type": "cat", "age": 2, "location": "oakland"}' union
select '{"animal_id": 2, "name": "louie","animal_type": "dog", "age": 4}' union
select '{"animal_id": 3, "gender": "female"}' union
select '{"animal_id": 4, "size": "large"}' ;
利用上述数据,我可以编写下面的查询,以获取我知道存在的属性。但是,如果明天添加新属性,我的报表查询将不会拾取新的键/值对。有没有办法在此表上执行SELECT*
类型查询
SELECT
json_extract_path_text(JSON,'animal_id') animal_id,
json_extract_path_text(JSON,'name') name,
json_extract_path_text(JSON,'animal_type') animal_type,
json_extract_path_text(JSON,'location') location,
json_extract_path_text(JSON,'age') age,
json_extract_path_text(JSON,'gender') gender,
json_extract_path_text(JSON,'size') size
FROM test.json
ORDER BY animal_id;
使用带有纯SQL的当前模式不可能执行您想要的操作 如果在创建SQL查询时可以使用应用程序逻辑,则可以动态创建
SELECT
语句
方案A
在应用程序中加载整个JSON,解析它并通过这种方式获得所需信息
方案B
在数据库中存储值时,解析JSON对象并将发现的键添加到另一个表中。查询红移集群时,加载此值列表并使用此信息生成适当的SQL语句
希望这些解决方法能适用于您的情况。谢谢您的回答,GuiSim,很高兴知道这一点。选项B将满足我的需要:)