Create key via SQL and C# for partition key

╄→尐↘猪︶ㄣ 提交于 2019-12-25 02:16:43

问题


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.

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?

Paul


回答1:


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.

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.

A good resource for general hash code algorithm choices is the Wikipedia page on hash functions.

A C# implementation in this example here:

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;
    }
}

Remove the ToUpperInvariant to make it case sensitive.



来源:https://stackoverflow.com/questions/51226000/create-key-via-sql-and-c-sharp-for-partition-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!