以下是一个使用Python和Google Cloud客户端库向BigQuery表中插入记录并添加摄入时间戳的示例代码:
from google.cloud import bigquery
from google.cloud.bigquery import schema
# 设置BigQuery客户端
client = bigquery.Client()
# 定义表架构
table_schema = [
schema.SchemaField("col1", "STRING"),
schema.SchemaField("col2", "INTEGER"),
schema.SchemaField("ingestion_time", "TIMESTAMP")
]
# 定义要插入的记录
records = [
("value1", 1),
("value2", 2),
("value3", 3)
]
# 获取当前时间戳
ingestion_time = bigquery.ScalarQueryParameter("ingestion_time", "TIMESTAMP", str(datetime.now()))
# 构建插入请求
insert_query = "INSERT INTO `project.dataset.table` (col1, col2, ingestion_time) VALUES (?, ?, ?)"
insert_job_config = bigquery.QueryJobConfig()
insert_job_config.query_parameters = [ingestion_time]
# 执行插入请求
insert_job = client.query(insert_query, job_config=insert_job_config)
# 等待插入作业完成
insert_job.result()
# 输出插入成功的记录数
print(f"Inserted {insert_job.num_dml_affected_rows} row(s).")
请确保替换以下值:
project.dataset.table
:要插入记录的目标表的完全限定名。col1
,col2
:表中的列名。根据需要更改或添加其他列。value1
,value2
,value3
:要插入的实际值。根据需要更改或添加其他记录。此代码假定您已经设置了正确的Google Cloud凭据,并且已经安装了Google Cloud客户端库(google-cloud-bigquery
)。