无法从具有数组和结构的 BigQuery table 中提取键值对
Unable to extract key-value pairs from BigQuery table which has arrays and structs
我已将 Firebase 分析数据与 google BigQuery 集成,并且每天都会创建一个带有日期戳的新 table。
一个样本 table 是 "projectID.com_dev_sambhav_ANDROID.app_events_20170821"
一个示例table如下所示
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"
但是我遇到了这个错误
Error: Each function argument is an expression, not a query. To use a
query as an expression, the query must be wrapped with additional
parentheses to make it a scalar subquery expression.
谁能帮我解决这个问题
问题是逗号和别名。此查询有效:
#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 时,您可能会发现此查询仍然执行得很好。
我已将 Firebase 分析数据与 google BigQuery 集成,并且每天都会创建一个带有日期戳的新 table。
一个样本 table 是 "projectID.com_dev_sambhav_ANDROID.app_events_20170821"
一个示例table如下所示 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"
但是我遇到了这个错误
Error: Each function argument is an expression, not a query. To use a query as an expression, the query must be wrapped with additional parentheses to make it a scalar subquery expression.
谁能帮我解决这个问题
问题是逗号和别名。此查询有效:
#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 时,您可能会发现此查询仍然执行得很好。