我正在尝试将重复记录的 col 类型从STRING更改为TIMESTAMP。这里有一些来自 BQ 文档的建议(手动更改架构)。但是,我遇到了每个推荐建议的问题。
这是一个示例架构:
{
'name' => 'id',
'type' => 'STRING',
'mode' => 'REQUIRED'
},
{
'name' => 'name',
'type' => 'STRING',
'mode' => 'REQUIRED'
},
// many more fields including nested records and repeated records
{
'name' => 'locations',
'type' => 'RECORD',
'mode' => 'REPEATED',
'fields' => [
{
'name' => 'city',
'type' => 'STRING',
'mode' => 'REQUIRED'
},
{
'name' => 'updated_at',
'type' => 'STRING', // ** want this as TIMESTAMP **
'mode' => 'REQUIRED'
},
]
}
Run Code Online (Sandbox Code Playgroud)
使用查询的问题:
我认为我们必须取消重复记录,将字段转换为每个重复记录的时间戳,然后以某种方式重新创建行以插入新表。
将表导出为 JSON 的问题:
当以 JSON 格式导出表时,它会导出数据的原始 json 表示(带有地图和字典,正如我们所期望的那样)。
但是,我们无法将原始数据重新导入 BQ:
BigQuery 不支持 JSON 格式的地图或字典。例如, "product_categories": {"my_product": 40.0} 无效,但 "product_categories": {"column1": "my_product" , "column2": 40.0} 有效。
https://cloud.google.com/bigquery/docs/loading-data-cloud-storage-json#limitations
我们欢迎所有的建议!
以下答案基于此:REPEATED RECORDBigQuery StandardSQL 中的 type 表示为 type ARRAY<STRUCT<f1 f1_type, f2 f2_type ... >>。
这不是我最喜欢的,因为您必须指定完整的列列表。也许有更好的方法。
#standardSQL
-- Build sample data, try to mimic what's in question.
CREATE OR REPLACE TABLE
<your_dataset>.sample_table AS
SELECT name,
array<struct<city string, update_at string>>[("SFO", "2011-1-1"), ("SEA", "2022-2-2")]
as locations
FROM UNNEST(['Name1', "Name2", "Name3"]) as name;
Run Code Online (Sandbox Code Playgroud)
然后下面的 SQL 会将update_at列转换为DATE并保存到新表(如果您愿意,也可以使用相同的表)。
#standardSQL
CREATE OR REPLACE TABLE
<your_dataset>.output_table AS
SELECT * REPLACE (
ARRAY(SELECT AS STRUCT * REPLACE(CAST(update_at AS DATE) AS update_at)
FROM UNNEST(locations))
AS locations
)
FROM
<your_dataset>.sample_table;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1780 次 |
| 最近记录: |