BigQuery向定义的STRING添加不必要的小数

时间:2019-04-05 18:04:12

标签: r google-bigquery

我已经在BigQuery中定义了一个架构,

+------------------+----------+----------+
|       name       |   type   |   mode   |
+------------------+----------+----------+
| warehouse        | INTEGER  | NULLABLE |
| transaction_date | DATETIME | NULLABLE |
| style            | STRING   | NULLABLE |
| piece            | STRING   | NULLABLE |
| fabric_1         | STRING   | NULLABLE |
| fabric_2         | STRING   | NULLABLE |
| serial           | STRING   | NULLABLE |
| customer_po      | STRING   | NULLABLE |
| order_number     | STRING   | NULLABLE |
+------------------+----------+----------+

我关注的两个字段是serial和order_number,在R中预览时,它们看起来像这样:

+-----------+------------------+--------+-------+-----------+----------+------------+--------------+--------------+
| warehouse | transaction_date | style  | piece | fabric_1  | fabric_2 |   serial   | customer_po  | order_number |
+-----------+------------------+--------+-------+-----------+----------+------------+--------------+--------------+
|        80 | 4/3/19           | K28300 | ARMH  | ALL CHAR  | NA       | 8040418253 | 1486838165   |       464374 |
|        80 | 4/3/19           | K28300 | ARMH  | ALL CHAR  | NA       | 9040542252 | 1485798731-P |       464069 |
|        80 | 4/3/19           | K28300 | ARMH  | ELEG NAVY | NA       | 8040355550 | 1486826068   |       464369 |
|        80 | 4/3/19           | K28300 | ARMH  | ELEG NAVY | NA       | 8040532364 | 1485366411-R |       464071 |
+-----------+------------------+--------+-------+-----------+----------+------------+--------------+--------------+

在R内,这两个字段在我正在寻找的数据框中似乎被读为字符。但是,当我将数据推送到BigQuery时,这两个字段最终像这样:

   +-----------+------------------+--------+-------+-----------+----------+------------+--------------+--------------+
    | warehouse | transaction_date | style  | piece | fabric_1  | fabric_2 |   serial   | customer_po  | order_number |
    +-----------+------------------+--------+-------+-----------+----------+------------+--------------+--------------+
    |        80 | 4/3/19           | K28300 | ARMH  | ALL CHAR  | NA       | 8040418253.0 | 1486838165   |       464374.0 |
    |        80 | 4/3/19           | K28300 | ARMH  | ALL CHAR  | NA       | 9040542252.0 | 1485798731-P |       464069.0 |
    |        80 | 4/3/19           | K28300 | ARMH  | ELEG NAVY | NA       | 8040355550.0 | 1486826068   |       464369.0 |
    |        80 | 4/3/19           | K28300 | ARMH  | ELEG NAVY | NA       | 8040532364.0 | 1485366411-R |       464071.0 |
    +-----------+------------------+--------+-------+-----------+----------+------------+--------------+--------------+

为什么会发生这种情况,我该如何更改?供参考,我的代码将其上传:

bqr_upload_data(projectId = "project-test", 
                datasetId = "orders", 
                tableId = "daily_orders", 
                upload_data = df_daily_orders,
                maxBadRecords = 1000,
                overwrite = TRUE)

2 个答案:

答案 0 :(得分:1)

由于我仍然是初学者,现在我不确定我的答案,但这可能会对您有所帮助。我将其添加为评论,但我的声誉还不够。

如果我正确理解,您实际上是在进行隐式转换-从数字值到字符串值,BigQuery捕获小数点以确保它正确捕获了整个值

检查here BigQuery的转换规则-第二张表,FLOAT64到String。

在您的位置,并根据您需要对表格进行的操作-我会

  1. 重新创建表,但是将serial和order_number列的架构更改为整数类型

  1. 尝试使用更新查询更新已创建的表-并在每个字符串值的末尾修改“ .0”

答案 1 :(得分:1)

从R上传的内容会查看列的类别,以确定哪种是BigQuery的最佳架构。尝试将数据框列的类更改为字符串,以避免通过类似的方式将其更改为浮点型

as.character(df$column)