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

问题描述 投票:0回答:2

我在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)
r google-bigquery
2个回答
1
投票

从R上传查看列的类,以确定哪个是BigQuery的最佳模式。尝试将数据框列的类更改为字符串,以避免将其更改为浮动,因为看起来像是通过类似的东西发生

as.character(df$column)

1
投票

现在我不能完全确定我的答案,因为我还是初学者,但它可能对你有所帮助。我会将此添加为评论,但我还没有足够的声誉。

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

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

在你的位置,根据你需要做的表 - 我会:

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

要么

  1. 尝试使用更新查询更新已创建的表 - 并修改每个字符串值末尾的“.0”
© www.soinside.com 2019 - 2024. All rights reserved.