如何在导出到JSON时保留整数数据类型? [英] How to preserve integer data type when exporting to JSON?

查看:143
本文介绍了如何在导出到JSON时保留整数数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我以JSON格式导出我的bigquery表时, INTEGER 字段会转换为字符串。有什么办法可以在导出时保持整型数据类型?

以下是重现integer-> string转换现象的最小步骤:


  1. 运行查询 SELECT INTEGER(1)AS myInt 并将结果保存到表中。请注意,输出表模式显示类型为 INTEGER

  2. 以JSON格式导出表。输出将是: {myInt:1}

在JSON格式中,1 是一个字符串,而不是一个整数。

解决方案

这目前不可能;理由是由于JavaScript规范,IEEE浮点精度,JSON和BigQuery整数大小的不幸结合。



Javascript ,所有数字都必须表示为IEEE754双精度浮点值。 JavaScript将JSON数字解析为JavaScript数字。 BigQuery使用64位有符号整数值。



问题出现是因为并非所有的64位整数值都可以表示为IEEE 754双精度浮点值。 (很容易看出原因:IEEE 754双精度浮点使用64位,但可以表示很多不是整数的东西;因此,必须有64位整数无法表示)。



因此,为了使BigQuery JSON响应在Javascript中起作用,整数值用引号括起来,这样就不会丢失任何精度。



<这就是说...在API请求中将整数表示为字符串的决定是有道理的,因为API的许多调用者都将使用javascript。在导出数据时,似乎没有像参数那样强制将整数表示为数字。 (除了现在改变它将是一个突破的变化)。



你可以在BigQuery 问题跟踪器来解决这个问题? (它可能会涉及导出配置中的另一个标志)。


When I export my bigquery tables in JSON format, the INTEGER fields are converted to strings. Is there any way to maintain the integer data type when exporting?

Here are the minimum steps to reproduce the integer->string conversion phenomenon:

  1. Run the query SELECT INTEGER(1) AS myInt and save the result to a table. Note that the output table schema shows the type as INTEGER.
  2. Export the table in JSON format. The output will be: {"myInt":"1"}

In the JSON format, "1" is a string, not an integer.

解决方案

This is not currently possible; the reasoning is because of an unfortunate combination of the Javascript spec, IEEE floating point precision, JSON, and BigQuery integer sizes.

In Javascript, all numbers must be representable as IEEE754 double-precision floating point values. Javascript parses JSON numbers into javascript numbers. BigQuery uses 64-bit signed integer values.

The problem comes because not all 64-bit integer values can be represented as IEEE 754 double precision floating point values. (it is easy to see why: IEEE 754 double-precision floating point uses 64 bits but can represent lots of things that aren't integers; therefore, there must be 64 bit integers that it can't represent).

So in order to make BigQuery JSON responses work in Javascript, integer values are wrapped in quotes, so that no precision is lost.

That said ... the decision to represent integers as strings in API request makes sense, since many of the callers of the API will be in javascript. There doesn't seem to be as compelling of an argument to not represent integers as numbers when exporting data. (other than to change it now would be a breaking change).

Can you file a bug at the BigQuery issue tracker to fix this? (it would likely involve another flag in the export configuration).

这篇关于如何在导出到JSON时保留整数数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆