我正在将帐单数据从Google Cloud Platform导出到BigQuery(BQ)。 当前的任务是建立一个将UNNEST相关数据存储到新的“平面”表中的查询 BQ中的数据结构是这样的:
[{
"billing_account_id": "01234-1778EC-123456",
"service": {
"id": "2062-016F-44A2",
"description": "Maps"
},
"sku": {
"id": "5D8F-0D17-AAA2",
"description": "Google Maps"
},
"usage_start_time": "2018-11-05 14:45:00 UTC",
"usage_end_time": "2018-11-05 15:00:00 UTC",
"project": {
"id": null,
"name": null,
"labels": []
},
"labels": [],
"system_labels": [],
"location": null,
"export_time": "2018-11-05 21:54:09.779 UTC",
"cost": "5.0",
"currency": "EUR",
"currency_conversion_rate": "0.87860000000017424",
"usage": {
"amount": "900.0",
"unit": "seconds",
"amount_in_pricing_units": "0.00034674063800277393",
"pricing_unit": "month"
},
"credits": "-1.25",
"invoice": {
"month": "201811"
}
},
我希望安排一个每天仅使用这种模式构建新表的工作
billing_account_id, usage_start_time, usage_end_time, cost, credit_amount
到目前为止,我在这里:
select billing_account_id, usage_start_time, usage_end_time, cost, credits AS CREDITS from clientBilling.gcp_billing_export_v1_XXXX , UNNEST(credits);
但是结果中,信用额仍然嵌套,而不是我需要的“固定”额度。欢迎任何输入,谢谢! :)
答案 0 :(得分:3)
credits
是一个结构数组(每个结构都是“名称,数量”)-BigQuery中的“重复”记录-因此,您必须首先取消嵌套该数组,然后引用所需的结构成员。 / p>
因此:
UNNEST
信用记录
SELECT
billing_account_id,
usage_start_time,
usage_end_time,
cost,
credit.amount as credit_amount
FROM
`optimum-rock-145719.billing_export.gcp_billing_export_v1*`,
UNNEST(credits) as credit
这将返回只包含credits.amount列作为credits_amount的结果表。您正在执行第1步,而不是第2步,并忽略了SELECT
子句中未嵌套的字段。