在 BigQuery 中创建一个目标表,其中包含 AVRO 数据类型中的 DECIMAL 数据类型所使用的字段架构。然后,通过以下方法将 AVRO 数据加载到 BigQuery 中:
from google.cloud import bigquery
from google.cloud.bigquery import LoadJobConfig
from google.cloud.bigquery.enums import SqlTypeNames
# Set the source and destination file paths
source_file_path = "path/to/source/file.avro"
destination_table_id = "project_id.dataset.table_name"
# Create BigQuery client
client = bigquery.Client()
# Set the load configuration options
avro_config = bigquery.AvroOptions(decimal_target_types=[SqlTypeNames.DECIMAL])
load_config = LoadJobConfig(source_format=bigquery.SourceFormat.AVRO, avro_options=avro_config)
# Start the load job
load_job = client.load_table_from_uri(source_file_path, destination_table_id, job_config=load_config)
# Wait for the load job to complete
load_job.result()
在上述代码示例中,decimal_target_types 参数用于指定目标表中 DECIMAL 数据类型的架构。我们在 avro_config 中使用了 SqlTypeNames.DECIMAL 值,表示映射到 BigQuery 中的 DECIMAL 数据类型。
然后,将配置选项传递给 LoadJobConfig 对象,并从源文件路径加载数据到目标表中。
最后,等待加载作业完成。