google-bigquery - BigQuery 无法从 CSV 文件中解析 M/D/YYYY 格式的日期

标签 google-bigquery

问题

我正在尝试从 Google Cloud Storage 中的 CSV 文件创建 BigQuery 表。

我明确定义了加载作业的架构(如下所示)并设置了要跳过的标题行 = 1。

数据

$ cat date_formatting_test.csv
id,shipped,name
0,1/10/2019,ryan
1,2/1/2019,blah
2,10/1/2013,asdf

架构

id:INTEGER,
shipped:DATE,
name:STRING

错误

BigQuery 产生以下错误:

Error while reading data, error message: Could not parse '1/10/2019' as date for field shipped (position 1) starting at location 17

问题

我知道这个日期不是 ISO 格式 (2019-01-10),我假设它会起作用。

但是,我正在尝试定义一个更灵活的输入配置,BigQuery 将借此正确加载普通美国人认为有效的任何日期。

  1. 有没有办法指定预期的日期格式?
  2. 是否有单独的配置/设置允许我使用按原样定义的模式成功加载提供的 CSV?

最佳答案

根据listed limitations :

When you load CSV or JSON data, values in DATE columns must use 
the dash (-) separator and the date must be in the following 
format: YYYY-MM-DD (year-month-day).

所以这给我们留下了 2 个选择:

选项 1:ETL

  • 将新的 CSV 文件放入 Google Cloud Storage
  • 这反过来会触发 Google Cloud FunctionGoogle Cloud Composer工作:
    • 编辑所有 CSV 文件中的日期列
    • 将编辑后的文件保存回 Google Cloud Storage
    • 将修改后的 CSV 文件加载到 Google BigQuery 中

选项 2:ELT

  • 按原样将 CSV 文件加载到 BigQuery(即您的架构应修改为 shipped:STRING)
  • 创建一个 BigQuery View ,将 shipped 字段从字符串转换为可识别的日期格式。使用 SELECT id, PARSE_DATE('%m/%d/%Y', shipped) AS shipped, name
  • 使用该 View 进行分析

根据您的描述,我不确定这是一次性工作还是经常性工作。如果它是一次性的,我会选择选项 2,因为它需要最少的努力。选项 1 需要更多的努力,并且只对重复性工作才值得。

关于google-bigquery - BigQuery 无法从 CSV 文件中解析 M/D/YYYY 格式的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59548775/

相关文章:

python - 使用 Google BigQuery Client API 在 BigQuery 中加载 JSON 文件

json - Go 代码,用于从 Cloud Storage 加载 JSON 文件的 BigQuery 表;自动检测架构

google-bigquery - Bigquery 如果字段存在

google-analytics - 将 View ga_realtime_sessions_view_YYYYMMDD 更改为标准 sql

google-bigquery - bigquery 的 time_partitioning_expiration 参数如何工作?

sql - 在 JOIN 的 ON 子句中使用别名

google-bigquery - 大查询 : Cannot read field 'stamp' of type TIMESTAMP_MICROS as DATETIME

google-bigquery - 选择 BigQuery 中没有记录的记录

google-bigquery - 如何支付 Google BigQuery 费用?

google-bigquery - 有什么办法可以绕过 BigQuery 的 1000 个表限制?