簡體   English   中英

BigQuery 加載數據因列名錯誤而崩潰

[英]BigQuery Load data crash with bad column names

我有一堆 CSV 列名錯誤的文件,比如我在 GCP Bucket 中復制的“AB/C”,並嘗試從控制台本身將它們加載到 BQ(無法更改源文件中的列名)。 當我通過加載第一個 csv 創建表時,BQ 將列重命名為“A_B_C”,這很好,但是當我嘗試將 append 第二個 CSV 文件添加到表中時,它會拋出錯誤“無法解析‘2019/08/ 14' 作為字段 A_B_C(位置 0)從位置 77" 開始的日期。 A_B_C 是 CSV 中的第一列,這就是它引用日期的原因。 IMO 它與日期無關。 CSV中的日期格式為YYYY-MM-DD,符合BQ要求。 我什至更改了架構以將 A_B_C 修改為 STRING,以防萬一日期列有任何問題得到解決,但它仍然是一樣的。

我還跳過了第二個 CSV 加載中的第一行,因此它不會打擾列標題,但仍然沒有機會。

有什么建議嗎?

PS - 顯然使用 *,? 一次加載多個 CSV 文件總是失敗,原因很明顯,我沒有在問題中提及它以避免進一步混淆。

雖然您沒有共享腳本,但您沒有使用任何一種語言。 我能夠使用 Python 腳本重現您的案例加載多個 .csv 文件並將它們附加到同一個表。 我使用了您在問題中提到的相同的列名和格式類型。

為了實現我上面描述的內容,我在 cloud.storage 中使用了blob cloud.storage ,因此腳本可以 go 遍歷所有具有相同前綴的 .csv 文件。 我還使用job_config.autodetect = Truejob_config.skip_leading_rows = 1來自動檢測模式並跳過第一行,即 header。下面是腳本:

from google.cloud import bigquery
from google.cloud.storage import Blob
from google.cloud import storage

client = bigquery.Client()
dataset_id = 'dataset_name'

client_bucket= storage.Client()
#bucket where the .csv files are stored
bucket = client_bucket.bucket('bucket_name')


dataset_ref = client.dataset(dataset_id)
job_config = bigquery.LoadJobConfig()
job_config.autodetect = True
job_config.skip_leading_rows = 1
# The source format defaults to CSV, so the line below is optional.
job_config.source_format = bigquery.SourceFormat.CSV
#append to the table
job_config.write_disposition = bigquery.WriteDisposition.WRITE_APPEND


#it will loop and load all the .csv files with the same prefix(test_)
#loading them in the created table table_output
for blob in bucket.list_blobs(prefix='test_'):
    file_name = blob.name
    #uri location for the .csv file which will be uploaded
    uri = "gs://bucket_name/"+file_name
    load_job = client.load_table_from_uri(
        uri, dataset_ref.table("table_output"),            job_config=job_config)  # API request

    #checking the uploads 
    print("Starting job {}".format(load_job.job_id))

    load_job.result()  # Waits for table load to complete.
    print("Job finished.")

    destination_table = client.get_table(dataset_ref.table(file_name.split('.')[0]))
    print("Loaded {} rows.".format(destination_table.num_rows))

至於文檔,我使用了兩個 sample.csv 文件:

test_1.csv

A.B/C,other_field
2020-06-09,test

test_2.csv

A.B/C,other_field
2020-06-10,test

因此,腳本會通過go這個bucket,找到所有.csv前綴為text_的文件,上傳到同一個表中。 請注意,我對日期字段以及字段名稱AB/C使用了相同的格式。 Cloud Shell內運行腳本后,加載成功,output:

在此處輸入圖像描述

和模式,

在此處輸入圖像描述

如上圖,字段名AB/C改為A_B_C ,字段格式自動檢測為DATE ,沒有任何錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM