![](/img/trans.png)
[英]Getting IndexError: string index out of range while loading file in bigquery
[英]Bigquery converts my string field into integer while loading json file with Python
{"number":"1234123"} 我在 python 中使用 bigquery.LoadJobConfig 將此數據分配給我的 Bigquery 表。我的 bigquery 表中我的數字列的類型是字符串。 當我執行加載操作時,它會將我的 bigquery 表中的數據類型轉換為 integer。我該如何解決這個問題? 我加載的文件類型:json。
job_config = bigquery.LoadJobConfig(
create_disposition=bigquery.CreateDisposition.CREATE_IF_NEEDED,
write_disposition=bigquery.WriteDisposition.WRITE_APPEND,
source_format=bigquery.SourceFormat.NEWLINE_DELIMITED_JSON,autodetect=True
)
另外:當我將autodetect設置為False時,我在讀取數據時收到類似Error的錯誤,錯誤信息:JSON table encountered too many errors
我建議您傳遞一個BigQuery
schema
來防止這種情況,而不是使用autodetect=True
,例如:
from google.cloud import bigquery
# Construct a BigQuery client object.
client = bigquery.Client()
# TODO(developer): Set table_id to the ID of the table to create.
# table_id = "your-project.your_dataset.your_table_name"
job_config = bigquery.LoadJobConfig(
schema=[
bigquery.SchemaField("number", "STRING")
],
create_disposition=bigquery.CreateDisposition.CREATE_IF_NEEDED,
write_disposition=bigquery.WriteDisposition.WRITE_APPEND,
source_format=bigquery.SourceFormat.NEWLINE_DELIMITED_JSON,
autodetect=False
)
uri = "gs://cloud-samples-data/bigquery/us-states/us-states.json"
load_job = client.load_table_from_uri(
uri,
table_id,
location="US", # Must match the destination dataset location.
job_config=job_config,
) # Make an API request.
load_job.result() # Waits for the job to complete.
destination_table = client.get_table(table_id)
print("Loaded {} rows.".format(destination_table.num_rows))
在此示例中,我將BigQuery
表的架構和autodetect
設置為False
。 如果您對True
使用autodetect
,則無法控制您的字段類型。
您可以查看文檔以獲取更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.