Number类型的DynamoDB属性中可以存储多少位整数数据? [英] How many bits of integer data can be stored in a DynamoDB attribute of type Number?

查看:216
本文介绍了Number类型的DynamoDB属性中可以存储多少位整数数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DynamoDB的 Number 类型支持38位十进制精度的数字。这不足以存储需要39位数字的128位整数。对于无符号128位整数,最大值为340,282,366,920,938,463,463,374,607,431,768,211,455,对于有符号128位整数为170,141,183,460,469,231,731,687,303,715,884,105,727。这些都是39位数字。

DynamoDB's Number type supports 38 digits of decimal precision. This is not big enough to store a 128-bit integer which would require 39 digits. The max value is 340,282,366,920,938,463,463,374,607,431,768,211,455 for unsigned 128-bit ints or 170,141,183,460,469,231,731,687,303,715,884,105,727 for signed 128-bit ints. These are both 39-digit numbers.

如果我不能存储128位,那么我可以在 Number中存储多少位整数数据

If I can't store 128 bits, then how many bits of integer data can I store in a Number?

推荐答案

类型为 Number 可以存储126位整数(或带有严重警告的127位无符号整数)。

DynamoDB attribute of type Number can store 126-bit integers (or 127-bit unsigned integers, with serious caveats).

根据Amazon的文档


数字的精度最高为38位。超出此范围将导致异常。

Numbers can have up to 38 digits precision. Exceeding this results in an exception.

这意味着(通过AWS控制台进行测试验证)最大正整数和最小负整数DynamoDB可以存储在 Number 属性中的分别是:

This means (verified by testing in the AWS console) that the largest positive integer and smallest negative integers, respectively, that DynamoDB can store in a Number attribute are:


99,999,999,999,999,999,999,999,999,999,999,999,999,999( aka 10 ^ 38-1)
-99,999,999,999,999,999,999,999,999,999,999,999,999(aka -10 ^ 38 + 1)

99,999,999,999,999,999,999,999,999,999,999,999,999 (aka 10^38-1) -99,999,999,999,999,999,999,999,999,999,999,999,999 (aka -10^38+1)

这些数字需要126位

bits = floor (ln(number) / ln (2))
     = floor (87.498 / 0.693)
     = floor (126.259)
     = 126

因此,您可以安全地将126位带符号的int存储在DynamoDB中。

So you can safely store a 126-bit signed int in a DynamoDB.

如果您想过着危险的生活,可以存储127位的 unsigned int也是,但是有一些警告:

If you want to live dangerously, you can store a 127-bit unsigned int too, but there are some caveats:


  • 您需要避免(或至少要非常小心)usi将这样的数字作为排序键,因为最高有效位为1的值将作为负数排序。

  • 您的应用在存储时需要将无符号整数转换为带符号整数它们或在DynamoDB中查询它们,并且还需要在从DynamoDB读取数据之后将它们转换回无符号。

如果是我,没有很好的理由,我不会再冒这些风险。

If it were me, I wouldn't take these risks for one extra bit without a very, very good reason.

一个逻辑问题是126(或上面提到的127)是否足以存储UUID。答案是:这取决于。如果您控制着UUID的生成,那么您始终可以从UUID中剃掉一两个字并进行存储。如果您从4个版本位中刮除(请参见格式此处),那么您可能不会如果您始终生成相同版本的UUID,则将完全失去任何熵。

One logical question is whether 126 (or 127 given the caveats above) is good enough to store a UUID. The answer is: it depends. If you are in control of the UUID generation, then you can always shave a bit or two from the UUID and store it. If you shave from the 4 "version" bits (see format here) then you may not be losing any entropy at all if you are always generating UUIDs with the same version.

但是,如果其他人正在生成这些UUID并期望无损存储,那么您可能无法使用 Number 来存储UUID。但是,如果您将客户端限制为4-8个 UUID版本的白名单,您也许可以存储它一个>。现在最大的版本是0-15范围内的5,并且出于隐私原因不建议使用某些较旧的版本,因此此限制可能是合理的,具体取决于您的客户端以及他们是否遵循 RFC 4122

However, if someone else is generating those UUIDs AND is expecting lossless storage, then you may not be able to use a Number to store the UUID. But you may be able to store it if you restrict clients to a whitelist of 4-8 UUID versions. The largest version now is 5 out of a 0-15 range, and some of the older versions are discouraged for privacy reasons, so this limitation may be reasonable depending on your clients and whether they adhere to the version bits as defined in RFC 4122.

顺便说一句,令我惊讶的是,这个位限制问题还没有在线出现……至少在一个易于使用Google的地方。因此,贡献这个Q& A对,以便将来的搜索者可以找到它。

这篇关于Number类型的DynamoDB属性中可以存储多少位整数数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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