我有不到 100M 的数据记录,我希望通过对字段进行非规范化来转换它们,然后输入到日期分区 GBQ 表中。日期可以追溯到 2001 年。
我曾希望我可以用 Python 对其进行转换,然后直接从脚本中使用 GBQ 来完成此操作,但在阅读了这篇文章之后,尤其是 this document创建日期分区表似乎并不直接。我正在寻找正确的方向。
有没有可以执行此操作的 python 脚本的工作示例?或者不可能通过 Python 来做?或者有人可以指出我的方向的另一种方法吗?
更新
我不确定我是否遗漏了什么,但创建的表似乎是根据我创建表时的插入日期进行分区的,我想按现有数据集中设置的日期进行分区。无论如何我都看不到改变这一点。
这是我正在尝试的:
import uuid
import os
import csv
from google.cloud import bigquery
from google.cloud.bigquery import SchemaField
from google.cloud.bigquery import Client
from google.cloud.bigquery import Table
import logging #logging.warning(data_store+file)
import json
import pprint
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path to service account credentials'
client = bigquery.Client()
dataset = client.dataset('test_dataset')
dataset.create()
SCHEMA = [
SchemaField('full_name', 'STRING', mode='required'),
SchemaField('age', 'INTEGER', mode='required'),
]
table = dataset.table('table_name', SCHEMA)
table.partitioning_type = "DAY"
table.create()
rows = [
('bob', 30),
('bill', 31)
]
table.insert_data(rows)
是否可以修改它以在我创建表和插入数据时控制分区?
更新 2
原来我不是在寻找表分区,对于我的用例来说,只需将日期序列附加到我的表名的末尾就足够了,然后按照以下行进行查询:
SELECT * FROM `dataset.test_dataset.table_name_*`
WHERE _TABLE_SUFFIX BETWEEN '20170701' AND '20170702'
我不知道这在技术上是否仍然是分区,但据我所知它具有相同的好处。
最佳答案
更新到最新版本(google-cloud-biquery==1.4.0
)
from google.cloud import bigquery
client = bigquery.Client()
dataset_ref = client.dataset('test_dataset')
table_ref = dataset_ref.table('test_table')
SCHEMA = [
SchemaField('full_name', 'STRING', mode='required'),
SchemaField('age', 'INTEGER', mode='required'),
]
table = bigquery.Table(table_ref, schema=SCHEMA)
if partition not in ('DAY', ):
raise NotImplementedError(f"BigQuery partition type unknown: {partition}")
table.time_partitioning = bigquery.table.TimePartitioning(type_=partition)
table = client.create_table(table) # API request
关于python - 如何在 GBQ 中创建日期分区表?你会用 python 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46425733/