Hive的内置HASH()函数使用了什么样的散列算法 [英] What kind of hash algorithm is used for Hive's built-in HASH() Function

查看:9930
本文介绍了Hive的内置HASH()函数使用了什么样的散列算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



理想情况下,我正在寻找SHA512 / SHA256哈希,类似于什么样的哈希算法sha()函数在linkedin中为Pig提供了datafu UDF。

解决方案

HASH 函数(自Hive 0.11开始)使用类似于 $ b

其代码如下所示:

  int hashCode = 0; // Hive HASH使用0作为种子,List#hashCode使用1.我不知道为什么。 
for(Object item:items){
hashCode = hashCode * 31 +(item == null?0:item.hashCode());



$ b基本上它是一本经典的散列算法,正如Effective Java一书中推荐的那样。
引用一个伟大的人(和一个伟大的
):


选择值31是因为它是一个奇素数。如果它甚至是
并且乘法溢出,则信息将会丢失,因为
乘以2相当于移位。使用
作为素数的优点不太清楚,但它是传统的。 31的一个不错的属性是
,为了获得更好的性能,可以用移位和减法
来代替乘法:31 * i ==(i <5) - i。现代虚拟机会自动执行这种
的优化。


我离题了。您可以查看 HASH 来源这里



如果您想在Hive中使用SHAxxx,那么您可以使用Apache DigestUtils 类和Hive内置反映函数(我希望这会起作用):

  SELECT反射('org.apache.commons.codec.digest.DigestUtils','sha256Hex','your_string')


What kind of hashing algorithm is used in the built-in HASH() function?

I'm ideally looking for a SHA512/SHA256 hash, similar to what the SHA() function offers within the linkedin datafu UDFs for Pig.

解决方案

HASH function (as of Hive 0.11) uses algorithm similar to java.util.List#hashCode.

Its code looks like this:

int hashCode = 0; // Hive HASH uses 0 as the seed, List#hashCode uses 1. I don't know why.
for (Object item: items) {
   hashCode = hashCode * 31 + (item == null ? 0 : item.hashCode());
}

Basically it's a classic hash algorithm as recommended in the book Effective Java. To quote a great man (and a great book):

The value 31 was chosen because it is an odd prime. If it were even and the multiplication overflowed, information would be lost, as multiplication by 2 is equivalent to shifting. The advantage of using a prime is less clear, but it is traditional. A nice property of 31 is that the multiplication can be replaced by a shift and a subtraction for better performance: 31 * i == (i << 5) - i. Modern VMs do this sort of optimization automatically.

I digress. You can look at the HASH source here.

If you want to use SHAxxx in Hive then you can use Apache DigestUtils class and Hive built-in reflect function (I hope that'll work):

SELECT reflect('org.apache.commons.codec.digest.DigestUtils', 'sha256Hex', 'your_string')

这篇关于Hive的内置HASH()函数使用了什么样的散列算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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