Python - BigQuery 临时表

Fel*_* FB 1 python google-bigquery

是否可以使用 Python 将 Cloud Storage 中已有的数据导入到 bigquery 中的临时表?我可以在 Python 中创建 BigQuery 临时表并将数据插入其中吗?

Ben*_*nos 5

您只能创建临时表作为 bigquery 脚本或存储过程的一部分。

您可以做的是创建具有随机后缀名称和短期到期的表。在我的例子中一小时。示例函数创建临时表,只需要一个数据集作为参数。

from google.cloud import bigquery
import datetime, pytz, random

PROJECT = "myproject"


def get_temp_table(dataset: str, table_name: str = None, project=None) -> bigquery.Table:
    prefix = "temp"
    suffix = random.randint(10000, 99999)
    if not table_name:
        table_name = "noname"

    temp_table_name = f"{dataset}.{prefix}_{table_name}_{suffix}"
    if project:
        temp_table_name = f"{project}.{temp_table_name}"
    tmp_table_def = bigquery.Table(temp_table_name)
    tmp_table_def.expires = datetime.datetime.now(pytz.utc) + datetime.timedelta(
        hours=1
    )

    return tmp_table_def


client = bigquery.Client(project=PROJECT)

tmp_table_def = get_temp_table("mydataset", "new_users", project=PROJECT)
tmp_table_def.schema = [
    bigquery.SchemaField("id", "STRING", mode="REQUIRED"),
    bigquery.SchemaField("full_name", "STRING", mode="REQUIRED"),
    bigquery.SchemaField("age", "INTEGER", mode="REQUIRED"),
]
tmp_table = client.create_table(tmp_table_def)  # type: bigquery.Table

data = [
    {"id": "c-1234", "full_name": "John Smith", "age": 39},
    {"id": "c-1234", "full_name": "Patricia Smith", "age": 41},
]

errors = client.insert_rows(tmp_table, data)

print(f"Loaded {len(data)} rows into {tmp_table.dataset_id}:{tmp_table.table_id} with {len(errors)} errors")
Run Code Online (Sandbox Code Playgroud)