简体   繁体   English

如何将 CSV 数据从 Google Drive/Google Sheets 正确导入 BigQuery

[英]How to import CSV data from Google Drive/Google Sheets into BigQuery correctly

I'm trying to import a CSV file placed in Google Drive into BigQuery and fail.我正在尝试将放置在 Google Drive 中的 CSV 文件导入 BigQuery 并失败。 I get an error Error while reading table, error message: CSV table encountered too many errors, giving up . Error while reading table, error message: CSV table encountered too many errors, giving up错误Error while reading table, error message: CSV table encountered too many errors, giving up

I guess to know, what caused an error.我想知道是什么导致了错误。 The CSV file contains strings in the columns A, B and D, integer in column C, and float in columns E and F. Delimiter is the tab, \\t . CSV 文件在 A、B 和 D 列中包含字符串,在 C 列中包含整数,在 E 和 F 列中包含浮点数。分隔符是制表符\\t

While the CSV file contains data like on the image 1:虽然 CSV 文件包含类似于图像 1 的数据:

所以看起来Excel中的数据

If i look at the file preview in Google Drive, i see broken numbers, like on the image 2: However, if i download file directly from the GDrive and open it with Notepad++, the file exactly as expected: numbers with commas as decimal sign and tab as column delimiter...如果我查看 Google Drive 中的文件预览,我会看到残破的数字,如图 2 所示:但是,如果我直接从 GDrive 下载文件并使用 Notepad++ 打开它,则文件完全符合预期:数字以逗号作为小数点和制表符作为列分隔符...

在此处输入图片说明

On creating the BigQuery table i add columns manually and assign to them field types too.在创建 BigQuery 表时,我手动添加列并为其分配字段类型。 For columns containing numbers i tested any combination of integer , float and numeric - always the same error.对于包含数字的列,我测试了integerfloatnumeric任意组合 - 总是相同的错误。

Q : how should i format the CSV file to make the import possible?:我应该如何格式化 CSV 文件以使导入成为可能?

Here is a sample of the file:这是该文件的示例:

Domain  Keywords    RP  Brand   SI  SiBerechnet
example.de  accura versicherung 1   accura  293,9   0,00244913
example.de  accura versicherung erfahrung   1   accura  63,9    0,00053249
example.de  accura versicherung für wohnmobile  1   accura  43,9    0,00036583
example.de  accura versicherung keine wohnmobile mehr   1   accura  53,9    0,00044916
example.de  accura versicherungsmakler  1   accura  83,9    0,00069916
example.de  accura versicherung test    1   accura  43,9    0,00036583
example.de  accura versicherung wohnmobil   1   accura  73,9    0,00061582
example.de  accura wohnmobilversicherung erfahrungen    1   accura  73,9    0,00061582
example.de  aufgaben innendienst versicherung   75  non brand   0,133333333 0,00000111
example.de  aufgaben versicherung innendienst   59  non brand   0,169491525 0,00000141
example.de  basler versicherung kfz telefonnummer   98  basler  0,102040816 0,00000085
example.de  basler versicherung kundenservice   96  basler  0,104166667 0,00000087
example.de  basler wohnmobilversicherung    8   basler  3,86    0,00003217
example.de  bergungskosten unfallversicherung   37  non brand   0,810810811 0,00000676
example.de  berufsunfähigkeitsversicherung bei bürojob  84  non brand   0,238095238 0,00000198
example.de  berufsunfähigkeitsversicherung bürojob  83  non brand   1,084337349 0,00000904
example.de  betriebshaftpflicht für hausmeisterservice  87  non brand   0,114942529 0,00000096
example.de  betriebshaftpflicht für hausverwalter   29  non brand   1,034482759 0,00000862
example.de  betriebshaftpflicht hausmeister 87  non brand   0,114942529 0,00000096
example.de  betriebshaftpflicht hausverwalter   26  non brand   0,384615385 0,00000321
example.de  betriebsunterbrechungsversicherung freiberufler 46  non brand   0,217391304 0,00000181
example.de  braucht eine krankenschwester eine diensthaftpflichtversicherung    15  non brand   2,706666667 0,00002256
example.de  campingfahrzeug versicherung    39  non brand   1,025641026 0,00000855
example.de  dienst haftpflicht  99  non brand   0,303030303 0,00000253
example.de  diensthaftpflicht öffentlicher dienst   55  non brand   0,545454545 0,00000455
example.de  diensthaftpflichtversicherung   57  non brand   22,80701754 0,00019006
example.de  dienst haftpflichtversicherung  84  non brand   0,238095238 0,00000198
example.de  diensthaftpflichtversicherung beamte    90  non brand   0,555555556 0,00000463
example.de  diensthaftpflichtversicherung für soldaten  28  non brand   0,357142857 0,00000298
example.de  diensthaftpflichtversicherung kosten    80  non brand   0,5 0,00000417
example.de  diensthaftpflichtversicherung öffentlicher dienst   51  non brand   0,980392157 0,00000817
example.de  diensthaftpflichtversicherung öffentlicher dienst angestellte   63  non brand   0,158730159 0,00000132
example.de  diensthaftpflichtversicherung polizei   69  non brand   0,724637681 0,00000604
example.de  diensthaftpflichtversicherung soldaten  26  non brand   0,769230769 0,00000641
example.de  einbauküche hausrat oder gebäude scheidung  31  non brand   0,64516129  0,00000538
example.de  einbauküche hausratversicherung oder gebäudeversicherung    12  non brand   2,643333333 0,00002203

What happens often is that the language settings on the drive sheets are different and are throwing around the "."经常发生的情况是驱动器表上的语言设置不同,并且会出现“。” and ",".和 ”,”。

You can see/change this in File -> spreadsheet settings您可以在文件 -> 电子表格设置中查看/更改此设置

I've copied your data to a Google Sheets, then I exported it with the tab as the field delimiter and I was able to load the data by specifying the field delimiter and I didn't have any issues as the table was created (However the numbers didn't take the comma as the decimal delimiter as it's used as a thousand separator always ).我已将您的数据复制到 Google 表格中,然后我将它与选项卡一起导出为字段分隔符,并且我能够通过指定字段分隔符来加载数据,并且在创建表时我没有遇到任何问题(但是数字没有将逗号作为十进制分隔符,因为它总是用作千位分隔符)。 Thus, I imported all the columns as Strings then I applied a REGEX_REPLACE like the following:因此,我将所有列作为字符串导入,然后应用了如下所示的REGEX_REPLACE

SELECT CAST(REGEXP_REPLACE(siberechnet, ",", ".") as numeric) as new_col FROM `project.dataset.table`

To use the decimal numbers properly正确使用十进制数

Hope it helps for your use-case.希望它对您的用例有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM