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

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

问题描述

当我以 JSON 格式导出 bigquery 表时,INTEGER 字段将转换为字符串.导出时有没有办法保持整数数据类型?

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. 运行查询 SELECT INTEGER(1) AS myInt 并将结果保存到表中.请注意,输出表架构将类型显示为 INTEGER.
  2. 以 JSON 格式导出表.输出将是:{"myInt":"1"}
  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"}

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

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

推荐答案

目前无法实现;原因在于 Javascript 规范、IEEE 浮点精度、JSON 和 BigQuery 整数大小的不幸组合.

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.

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

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.

问题出现是因为并非所有 64 位整数值都可以表示为 IEEE 754 双精度浮点值.(很容易理解为什么:IEEE 754 双精度浮点使用 64 位,但可以表示很多不是整数的东西;因此,必须有它不能表示的 64 位整数).

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).

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

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

也就是说……在 API 请求中将整数表示为字符串的决定是有道理的,因为 API 的许多调用者都将使用 javascript.在导出数据时不将整数表示为数字的论点似乎没有那么令人信服.(除了现在改变它会是一个突破性的改变).

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).

您能否在 BigQuery 问题跟踪器 上提交错误以解决此问题?(这可能会涉及导出配置中的另一个标志).

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天全站免登陆