Batch loading data into bigquery 代码示例
时间: 2024-03-25 11:40:10 浏览: 118
以下是使用Python代码将数据批量加载到BigQuery的示例:
```python
from google.cloud import bigquery
# Set up a BigQuery client
client = bigquery.Client()
# Set up the job configuration
job_config = bigquery.LoadJobConfig(
schema=[
bigquery.SchemaField("name", "STRING"),
bigquery.SchemaField("age", "INTEGER"),
bigquery.SchemaField("gender", "STRING"),
],
skip_leading_rows=1,
source_format=bigquery.SourceFormat.CSV,
)
# Set up the data source
uri = "gs://bucket_name/path_to_file.csv"
# Set up the destination table
table_id = "project_id.dataset_name.table_name"
# Create the load job
load_job = client.load_table_from_uri(
uri, table_id, job_config=job_config
)
# Wait for the load job to complete
load_job.result()
# Check the status of the load job
if load_job.errors:
print(load_job.errors)
else:
print("Data loaded successfully")
```
这个示例代码假设您已经在Google Cloud中设置了适当的身份验证,并且已经将数据文件上传到Google Cloud Storage中的存储桶中。在代码中,您需要设置以下参数:
- `schema`:定义要加载数据的表的结构。
- `skip_leading_rows`:指示数据文件中有多少行要跳过,这些行不包含表中的数据。
- `source_format`:指定数据文件的格式。
- `uri`:指定数据文件的位置。
- `table_id`:指定要将数据加载到哪个表中。
一旦设置了这些参数,您就可以运行代码将数据批量加载到BigQuery中。
阅读全文
相关推荐

















