如何在GBQ中创建日期分区表?你能用python吗?

时间:2017-09-26 11:49:57

标签: python google-bigquery

我只有不到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'

我不知道这在技术上是否仍在分区,但据我所知,它具有相同的好处。

3 个答案:

答案 0 :(得分:3)

您可以使用API​​和Python SDK轻松创建日期分区表。只需在脚本中将timePartitioning字段设置为DAY

https://github.com/GoogleCloudPlatform/google-cloud-python/blob/a14905b6931ba3be94adac4d12d59232077b33d2/bigquery/google/cloud/bigquery/table.py#L219

或使用以下正文滚动您自己的表插入请求:

{
  "tableReference": {
    "projectId": "myProject",
    "tableId": "table1",
    "datasetId": "mydataset"
  },
  "timePartitioning": {
    "type": "DAY"
  }
}

所有内容都由REST api here支持。

答案 1 :(得分:2)

已更新为最新版本(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

答案 2 :(得分:0)

请注意,google-api-core的不同版本对时间分区表的处理方式不同。例如,使用google-cloud-core==0.29.1,必须使用bigquery.Table对象创建按时间划分的表:

from google.cloud import bigquery

MY_SA_PATH = "/path/to/my/service-account-file.json"

MY_DATASET_NAME = "example"
MY_TABLE_NAME = "my_table"

client = bigquery.Client.from_service_account_json(MY_SA_PATH)

dataset_ref = client.dataset(MY_DATASET_NAME)
table_ref = dataset_ref.table(MY_TABLE_NAME)

actual_table = bigquery.Table(table_ref)
actual_table.partitioning_type = "DAY"

client.create_table(actual_table)

我只是通过查看0.20.1 Table source code发现了这一点。我没有在任何文档或示例中看到此信息。如果您在创建按时间划分的表时遇到问题,建议您确定正在使用的每个Google库的版本(例如,使用pip freeze),并对照该库的源代码检查工作。