繁体   English   中英

如何在 BigQuery 上扩展数组以向现有表添加列

[英]How do you expand an array on BigQuery to add columns to the existing table

目前我在扩展 BigQuery 表中提供的数组时遇到问题。

是否可以创建另一个表或视图,将msgs列中的所有项目扩展到另一个表中。 我提供了下面的屏幕截图

我该怎么做?

这是从我的表中获取所有表和列的查询

SELECT * FROM `aftership.shipments`

这是数据类型的列表

字段名称、类型、模式

_id,STRING,NULLABLE 
_index,INTEGER,NULLABLE 
_created,TIMESTAMP,NULLABLE 
_fivetran_synced,TIMESTAMP,NULLABLE 
_ip,STRING,NULLABLE 
event,STRING,NULLABLE   
event_id,STRING,NULLABLE    
is_tracking_first_tag,BOOLEAN,NULLABLE  
msg,STRING,NULLABLE 
ts,INTEGER,NULLABLE 

来自 msg 列的示例数据将是

{
    "id": "gynv1fsa8m7amkoct4zvx00w",
    "tracking_number": "KEX99999999",
    "title": "#xx589",
    "note": null,
    "origin_country_iso3": "THA",
    "destination_country_iso3": "THA",
    "courier_destination_country_iso3": "THA",
    "shipment_package_count": null,
    "active": false,
    "order_id": "3753237577781",
    "order_id_path": null,
    "order_date": "2021-05-06T01:09:01Z",
    "customer_name": "คุณx xxx",
    "source": "shopify",
    "emails": ["jonappleseed@gmail.com"],
    "smses": ["+669999999"],
    "subscribed_smses": [],
    "subscribed_emails": [],
    "android": [],
    "ios": [],
    "return_to_sender": false,
    "custom_fields": {
        "item_names": "เซต x & x x x x 1"
    },
    "tag": "Delivered",
    "subtag": "Delivered_001",
    "subtag_message": "Delivered",
    "tracked_count": 26,
    "expected_delivery": null,
    "signed_by": "คุณxxx xx #32589",
    "shipment_type": null,
    "created_at": "2021-05-06T11:29:54+00:00",
    "updated_at": "2021-05-07T09:00:32+00:00",
    "slug": "kerry-logistics",
    "unique_token": "deprecated",
    "path": "deprecated",
    "shipment_weight": null,
    "shipment_weight_unit": null,
    "delivery_time": 2,
    "last_m…

后船。 出货量

你可以做的是改变你的JSON_string成STRUCT包含你感兴趣的字段( STRUCT是没有必要的,但保持组织的事情):

WITH sample AS (
    SELECT "1" AS id, "{\"id\":\"gynv1fsa8m7amkoct4zvx00w\",\"tracking_number\":\"KEX99999999\",\"title\":\"#xx589\",\"note\":null,\"origin_country_iso3\":\"THA\",\"destination_country_iso3\":\"THA\",\"courier_destination_country_iso3\":\"THA\",\"shipment_package_count\":null,\"active\":false,\"order_id\":\"3753237577781\",\"order_id_path\":null,\"order_date\":\"2021-05-06T01:09:01Z\",\"customer_name\":\"\u0e04\u0e38\u0e13x xxx\",\"source\":\"shopify\",\"emails\":[\"jonappleseed@gmail.com\", \"jonappleseed@yahoo.com\",\"jonappleseed@outlook.com\"],\"smses\":[\"+669999999\"],\"subscribed_smses\":[],\"subscribed_emails\":[],\"android\":[],\"ios\":[],\"return_to_sender\":false,\"custom_fields\":{\"item_names\":\"\u0e40\u0e0b\u0e15 x & x x x x 1\"},\"tag\":\"Delivered\",\"subtag\":\"Delivered_001\",\"subtag_message\":\"Delivered\",\"tracked_count\":26,\"expected_delivery\":null,\"signed_by\":\"\u0e04\u0e38\u0e13xxx xx #32589\",\"shipment_type\":null,\"created_at\":\"2021-05-06T11:29:54+00:00\",\"updated_at\":\"2021-05-07T09:00:32+00:00\",\"slug\":\"kerry-logistics\",\"unique_token\":\"deprecated\",\"path\":\"deprecated\",\"shipment_weight\":null,\"shipment_weight_unit\":null,\"delivery_time\":2}" AS msg
)

SELECT id,
    STRUCT(JSON_VALUE(msg, '$.id') AS mid,
    JSON_VALUE(msg, '$.title') AS title,
    JSON_VALUE(msg, '$.tag') AS tag,
    JSON_VALUE(msg, '$.active') AS is_active,
    JSON_EXTRACT_ARRAY(JSON_QUERY(msg, '$.emails')) AS emails,
    JSON_QUERY(msg, '$.custom_fields') AS custom_fields) AS msg
FROM sample

这给

在此处输入图片说明

(额外的邮件不在原始邮件中,我添加它们是为了表明您可以将 JSON 字符串中的数组提取到结构体中的数组中)。

您只需按照模板为您感兴趣的每个字段添加一行:

JSON_VALUE(msg, '$.<FIELD>') AS field_column

注意: JSON_VALUE 或 JSON_QUERY 取决于你想要什么

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM