将外部唯一标识映射到类似于Firestore的ID。

huangapple go评论46阅读模式
英文:

Map external unique ID to Firestore like Id

问题

我有一个表中的数据,我想将其同步到一个Firestore集合(1行 -> 1文档),但是Id是递增的数字,所以我不想使用表中的Id作为文档Id,因为可能出现热点问题

我考虑对Id进行哈希处理,寻找一个返回类似Firestore的Id的哈希函数(我认为正则表达式是 ^[a-zA-Z0-9]{20}$)。

有任何想法或替代策略吗?

英文:

I have data in a table that I would like to sync to a Firestore collection (1 row -> 1 document), but the Ids are incrementing numbers, so I don't want to use the Id in the table as the document Id, due to hotspotting.

I'm thinking of hashing the Id and am looking for a hashing function that returns a firestore like id, (I believe the regex is ^[a-zA-Z0-9]{20}$)

Any ideas or alternative strategies?

答案1

得分: 0

哈希是一个很好的起点,可能是你唯一的选择。哈希可能会发生冲突(取决于你选择的算法),你可以通过在哈希后面添加实际数字来解决这个问题,以确保它们绝对是唯一的。所以,类似于 [hash]_[rownum] 这样的形式。

你没有必要让哈希看起来像Firestore生成的,因为Firestore只是在客户端SDK中生成随机ID(不是在服务器上)。只要字符串是唯一的,你可以使用任何你喜欢的UTF-8字符串格式作为ID。

英文:

Hashing is a good place to start, and likely your only option. Hashes could run into collisions (with a likelihood dependent on the algorithm you choose). You can work around that by suffixing the hash with the actual number in order to ensure they are absolutely unique. So, something like [hash]_[rownum].

You have no requirement to make the hash look like what Firestore generates because Firestore just generates random IDs in the client SDK (not on the server). You can use any ID utf-8 string format you like so long as the strings are unique.

huangapple
  • 本文由 发表于 2023年1月9日 08:37:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定