将 Athena 中嵌套的 json 转换为在 Quicksight 中显示

transform nested json in Athena to show in Quicksight

我在下面写了 Athena 查询到 Quicksight 中的 show/visualize 数据。只要我在 table 中有一条记录,这项工作就很好。如果您看到我已硬编码为索引 1 的查询。我如何修改下面的查询以包含来自 table 的所有数据。如果删除索引 1 然后它给我错误 as

SYNTAX_ERROR: line 8:19: Expression result.extensions.response is not of type ROW

查询:

select user_id, assessment_id, created_by,result.extensions.response[1].assessmentid AS AssesmentId,
result.extensions.response[1].assessmentname AS AssesmentName,
response_json.questionid AS QuestionId,
response_json.questionText as Questiontext,
transform(response_json.answers,answer-> answer.answerId) AS AnswerID,
transform(response_json.answers,answer-> answer.answerText) AS AnswerText
FROM focalbucket
CROSS JOIN UNNEST(result.extensions.response[1].responseData) AS t(response_json)

输出:

雅典娜Table DDL:

CREATE EXTERNAL TABLE `focalbucket`(
  `assessment_id` int COMMENT 'from deserializer', 
  `id` string COMMENT 'from deserializer', 
  `user_id` string COMMENT 'from deserializer', 
  `project_id` int COMMENT 'from deserializer', 
  `created_by` string COMMENT 'from deserializer', 
  `team_id` int COMMENT 'from deserializer', 
  `result`     struct<extensions:struct<response:array<struct<assessmentid:int,assessmentname:string,assessmentcreateddate:string,   responsedata:array<struct<questionid:int,answers:array<struct<answerid:int,answertext:string>>,
   questiontext:string,questiontype:string>>,userfullname:string>>>,
   completion:boolean,platform:string,app_version:string> COMMENT 'from deserializer', 
  `verb` struct<id:string> COMMENT 'from deserializer', 
  `actor` struct<mbox:string,name:string> COMMENT 'from deserializer', 
  `timestamp` bigint COMMENT 'from deserializer', 
  `version` string COMMENT 'from deserializer')
ROW FORMAT SERDE 
  'org.openx.data.jsonserde.JsonSerDe'

result.extensions.response 也是一个数组,因此您也可以取消嵌套它。像这样的东西(注意 - 没有测试,因为没有提供示例数据):

select 
   user_id, 
   assessment_id, 
   created_by,
   t.response.assessmentid AS AssesmentId,
   t.response.assessmentname AS AssesmentName,
   response_json.questionid AS QuestionId,
   response_json.questionText as Questiontext,
   transform(response_json.answers,answer-> answer.answerId) AS AnswerID,
   transform(response_json.answers,answer-> answer.answerText) AS AnswerText
FROM focalbucket
CROSS JOIN UNNEST(result.extensions.response) AS t(response)
CROSS JOIN UNNEST(t.response.responseData) AS tt(response_json)