通过SQL和C#为分区密钥创建密钥 [英] Create key via SQL and C# for partition key
问题描述
我有一组数据,这些数据具有3级层次结构.每个级别都有一个名称.
I have a set of data which has a hierarchy of 3 levels. Each level has a name.
我正在考虑将所有这些名称组合成一个字符串,然后创建一个可用作服务结构状态服务的哈希键的数字哈希.
I am looking at combining all of these names into a single string then creating a numeric hash that can be used as a hash key for a service fabric stateful service.
我在网上看到了很多有关使用键查找数据的信息,但是我不确定如何有效地实际创建它们.
I have seen lots online about finding data with keys but I am not sure how to actually create them in an efficient way.
理想情况下,我想在SQL Server 2017和C#中快速且轻松地生成哈希.
Ideally I would like a hash that is quick and easy to generate in SQL Server 2017 and C#.
有人可以指出我的正确方向吗?
Can anyone point me in the right direction, please?
保罗
推荐答案
The SF team advice is to use the FNV-1 hashing algorithm for this.
选择哈希算法哈希的重要部分是选择您的 哈希算法.考虑的是目标是否是分组 相似的键彼此靠近(对位置敏感的哈希)-或 活动应广泛分布在所有分区中 (分布哈希),这种情况比较常见.
Select a hash algorithm An important part of hashing is selecting your hash algorithm. A consideration is whether the goal is to group similar keys near each other (locality sensitive hashing)--or if activity should be distributed broadly across all partitions (distribution hashing), which is more common.
一个好的分布哈希算法的特点是 它易于计算,几乎没有碰撞,并且可以分布 均匀地键.有效的哈希算法的一个很好的例子是 FNV-1哈希算法.
The characteristics of a good distribution hashing algorithm are that it is easy to compute, it has few collisions, and it distributes the keys evenly. A good example of an efficient hash algorithm is the FNV-1 hash algorithm.
对于一般的哈希码算法选择,一个很好的资源是 Wikipedia页面上的散列函数.
A good resource for general hash code algorithm choices is the Wikipedia page on hash functions.
此示例中的
AC#实现这里:
public long HashString(string input)
{
input = input.ToUpperInvariant();
var value = Encoding.UTF8.GetBytes(input);
ulong hash = 14695981039346656037;
unchecked
{
for (int i = 0; i < value.Length; ++i)
{
hash ^= value[i];
hash *= 1099511628211;
}
return (long)hash;
}
}
删除ToUpperInvariant
使其区分大小写.
这篇关于通过SQL和C#为分区密钥创建密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!