无法从具有数组和结构的BigQuery表中提取键值对

时间:2017-08-23 09:44:27

标签: arrays firebase google-bigquery firebase-analytics unnest

我已将Firebase分析数据与Google BigQuery集成,每天都会使用DATE标记创建新表。

一个示例表是“projectID.com_dev_sambhav_ANDROID.app_events_20170821”

下面显示了一个样本表 sample table

我的要求是以event_dim.name =“notification_received”获得以下所需格式的结果 desired output

要获得所需的输出,我执行以下查询(standardSQL)

SELECT event.name
 (SELECT param.value.string_value FROM UNNEST(event_dim.params) AS param WHERE param.key="notification_title") as notification_title,
 (SELECT param.value.string_value FROM UNNEST(event_dim.params) AS param WHERE param.key="item_id") as item_id 
FROM `projectID.com_dev_sambhav_ANDROID.app_events_20*`, UNNEST(event_dim) as event  
WHERE event.name = "notification_received"

但是我收到了这个错误

  

错误:每个函数参数都是表达式,而不是查询。用一个   查询作为表达式,查询必须附加包装   括号使其成为标量子查询表达式。

任何人都可以帮助我摆脱这个问题

1 个答案:

答案 0 :(得分:1)

问题是逗号和别名。此查询有效:

#standardSQL
WITH `projectID.com_dev_sambhav_ANDROID.app_events_2017` AS(
  SELECT ARRAY< STRUCT<date STRING, name STRING, params ARRAY< STRUCT<key STRING, value STRUCT<string_value STRING> > > > > [STRUCT('20170814' AS date, 'notification_received' AS name, [STRUCT('notification_title' AS key, STRUCT('Amazing Offers two' AS string_value) AS value ), 
                                                                                                                STRUCT('firebase_screen_class' AS key, STRUCT('RetailerHomeActivity' AS string_value) AS value),
                                                                                                                STRUCT('notification_id' AS key, STRUCT('12345' AS string_value) AS value),
                                                                                                                STRUCT('firebase_screen_id' AS key, STRUCT('app' AS string_value) AS value),
                                                                                                                STRUCT('item_id' AS key, STRUCT('DEMO-02' AS string_value) AS value),
                                                                                                                STRUCT('firebase_screen' AS key, STRUCT('My Order' AS string_value) AS value)] AS params)] event_dim
)

SELECT
 event.name,
 (SELECT param.value.string_value FROM UNNEST(event.params) AS param WHERE param.key="notification_title") as notification_title,
 (SELECT param.value.string_value FROM UNNEST(event.params) AS param WHERE param.key="item_id") as item_id 
FROM `projectID.com_dev_sambhav_ANDROID.app_events_20*`, UNNEST(event_dim) as event  
WHERE event.name = "notification_received"

如果您UNNEST字段event_dim并将其称为event,那么您应该在查询中使用此别名作为参考。

作为补充,这是解决问题的另一种方式(它只是另一种可能性,因此在使用BigQuery时你有更多的技巧):

#standardSQL
SELECT
  (SELECT date FROM UNNEST(event_dim)) date,
  (SELECT params.value.string_value FROM UNNEST(event_dim) event, UNNEST(event.params) params WHERE event.name = 'notification_received' AND params.key = 'notification_title') AS notification_title,
  (SELECT params.value.string_value FROM UNNEST(event_dim) event, UNNEST(event.params) params WHERE event.name = 'notification_received' AND params.key = 'item_id') AS item_id
FROM `projectID.com_dev_sambhav_ANDROID.app_events_2017`
WHERE EXISTS(SELECT 1 FROM UNNEST(event_dim) WHERE name = 'notification_received')

当处理高达TB的数据时,您可能会发现此查询仍能很好地执行。