我尝试在 RedShift 中解析 JSON。
“输入”列中的字符串是:
[{"desc": "How many people does the video contain?", "name": "Number of People", "type": "dropdown", "values": ["", "Only 1", "2-3", "3+"]}, {"desc": "What is the camera position?", "name": "Movement", "type": "dropdown", "values": ["", "Fixed position", "Moving"]}, {"desc": "From which angle did you shoot the video?", "name": "Shoot Angle", "type": "dropdown", "values": ["", "Frontal recording", "Tight angle: 10-40 degree", "Wide angle: 40-70 degree"]}, {"desc": "From which distance did you shoot the video?", "name": "Distance", "type": "dropdown", "values": ["", "Near/Selfie", "Mid (3-6 ft)", "Far (>6 ft)"]}, {"desc": "What is the video lighting direction?", "name": "Lighting Direction", "type": "dropdown", "values": ["", "Front lit", "Side lit", "Back lit"]}, {"desc": "What is the video background?", "name": "Background", "type": "dropdown", "values": ["", "Outdoors", "In office", "At home", "Plain background"]}, {"desc": "What is the topic in your speech?", "name": "Topic", "type": "dropdown", "values": ["", "Arts and Media", "Business", "Education", "Entertainment", "Food/Eating", "Nutrition", "Healthcare ", "High School Life", "Mental Health", "News", "Technology", "Morals and Ethics", "Phones and Apps", "Sports", "Science"]}]
我的任务是:“JSON 中的每个值、名称、描述都需要作为一行存储在表中”。
示例: id: 1, desc: "视频中有多少人?" id: 2, desc: "相机位置是多少?" 等等
我使用查询:
SELECT c.*, d.desc, d.name, d.values FROM source.table AS c, c.inputs AS d;
出现错误:不允许在“输入”列上导航,因为它不是 super 类型
并查询:
从 source.table AS c、JSON_PARSE(c.inputs) AS d 中选择 c.*、d.desc、d.name、d.values;
给了我另一个错误:“FROM 中的函数表达式可能不会引用相同查询级别的其他关系”
但是当我将测试 JSON 创建为:
CREATE TABLE test_parse_json_super
(
id smallint,
details super
);
INSERT INTO test_parse_json_super VALUES(1, JSON_PARSE('[{"desc": "How many people does the video contain?", "name": "Number of People", "type": "dropdown", "values": ["", "Only 1", "2-3", "3+"]}, {"desc": "What is the camera position?", "name": "Movement", "type": "dropdown", "values": ["", "Fixed position", "Moving"]}, {"desc": "From which angle did you shoot the video?", "name": "Shoot Angle", "type": "dropdown", "values": ["", "Frontal recording", "Tight angle: 10-40 degree", "Wide angle: 40-70 degree"]}, {"desc": "From which distance did you shoot the video?", "name": "Distance", "type": "dropdown", "values": ["", "Near/Selfie", "Mid (3-6 ft)", "Far (>6 ft)"]}, {"desc": "What is the video lighting direction?", "name": "Lighting Direction", "type": "dropdown", "values": ["", "Front lit", "Side lit", "Back lit"]}, {"desc": "What is the video background?", "name": "Background", "type": "dropdown", "values": ["", "Outdoors", "In office", "At home", "Plain background"]}, {"desc": "What is the topic in your speech?", "name": "Topic", "type": "dropdown", "values": ["", "Arts and Media", "Business", "Education", "Entertainment", "Food/Eating", "Nutrition", "Healthcare ", "High School Life", "Mental Health", "News", "Technology", "Morals and Ethics", "Phones and Apps", "Sports", "Science"]}]'));
并使用官方 RedShift 文档中的查询“SELECT c.*, d.desc, d.name, d.values FROM test_parse_json_super AS c, c.details AS d;”
- 工作正常来自 JSON 的所有数据都解析到每一行,并且 JSON 是正确的。
如何修复查询以使用我的真实数据?
谢谢。
最佳答案
我遇到了类似的问题。 json必须在创建的表中解析后才能使用,并且不能通过CTE或子查询引用然后使用。因此我认为答案是:
CREATE TABLE my_table_with_super
AS
(
SELECT c.*,
JSON_PARSE(c.inputs) AS inputs_json_super
FROM source.table c
);
SELECT c.*,
json_row.desc,
json_row.name,
json_row.values
FROM my_table_with_super as c,
c.inputs_json_super as json_row;
关于amazon-redshift - Redshift : Unnest subquery's result on leader is not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69130677/