在使用 BigQuery 插入数据时,如果出现数据没有插入的情况,可能有以下几个原因和解决方法:
from google.cloud import bigquery
client = bigquery.Client()
table_id = "your-project.your_dataset.your_table"
job_config = bigquery.LoadJobConfig(
schema=[
bigquery.SchemaField("name", "STRING"),
bigquery.SchemaField("age", "INTEGER"),
],
skip_leading_rows=1,
source_format=bigquery.SourceFormat.CSV,
)
uri = "gs://your-bucket/your-file.csv"
load_job = client.load_table_from_uri(
uri, table_id, job_config=job_config
) # Make an API request.
load_job.result() # Waits for the job to complete.
table = client.get_table(table_id) # Make an API request.
print("Loaded {} rows to table {}".format(table.num_rows, table_id))
from google.cloud import bigquery
client = bigquery.Client()
table_id = "your-project.your_dataset.your_table"
job_config = bigquery.LoadJobConfig(
schema=[
bigquery.SchemaField("name", "STRING"),
bigquery.SchemaField("age", "INTEGER"),
],
skip_leading_rows=1,
source_format=bigquery.SourceFormat.CSV,
)
uri = "gs://your-bucket/your-file.csv"
load_job = client.load_table_from_uri(
uri, table_id, job_config=job_config
) # Make an API request.
load_job.result() # Waits for the job to complete.
table = client.get_table(table_id) # Make an API request.
print("Loaded {} rows to table {}".format(table.num_rows, table_id))
WriteDisposition
参数来解决问题。例如,使用 WriteDisposition.WRITE_TRUNCATE
可以清空表中的数据再插入新数据。from google.cloud import bigquery
client = bigquery.Client()
table_id = "your-project.your_dataset.your_table"
job_config = bigquery.LoadJobConfig(
schema=[
bigquery.SchemaField("name", "STRING"),
bigquery.SchemaField("age", "INTEGER"),
],
skip_leading_rows=1,
source_format=bigquery.SourceFormat.CSV,
write_disposition=bigquery.WriteDisposition.WRITE_TRUNCATE,
)
uri = "gs://your-bucket/your-file.csv"
load_job = client.load_table_from_uri(
uri, table_id, job_config=job_config
) # Make an API request.
load_job.result() # Waits for the job to complete.
table = client.get_table(table_id) # Make an API request.
print("Loaded {} rows to table {}".format(table.num_rows, table_id))
以上是一些可能导致 BigQuery 数据没有插入的常见问题和解决方法。根据具体情况,选择适当的解决方法来解决问题。