在不使用 Hive 中现有的哈希函数的情况下计算哈希 [英] Calculate hash without using exisiting hash fuction in Hive

查看:22
本文介绍了在不使用 Hive 中现有的哈希函数的情况下计算哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算 hive 中字符串的哈希值,而无需仅使用现有函数编写任何 UDF.这样我就可以使用类似的方法在其他语言中获得一致的哈希值.例如:是否有任何函数可以用来做一些事情,比如添加字符或取 Xor.

I want to calculate hash for strings in hive without writing any UDF only using exisiting functions . So that I can use similar approach to get consistent hash in other languages. for ex : are there any functions using which I can do something like adding characters or taking Xor.

推荐答案

这取决于 Hive 的版本,参见.https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-Misc.Functions

It depends on the version of Hive, cf. https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-Misc.Functions

从ABC中选择XYZ,hash(XYZ)
已经可用多年并应用普通的 java.lang.String.hashCode(),返回一个 INT(32 位哈希)

select XYZ, hash(XYZ) from ABC
has been available for years and applies plain old java.lang.String.hashCode(), returning an INT (32 bit hash)

[Edit 2] 实际上它有点复杂,因为 hash() 接受任何类型的参数列表(包括没有内置散列方法的原始类型),因此使用自定义方法——检查源代码中的 ObjectInspectorUtils.hashCode()ObjectInspectorUtils.getBucketHashCode() 这里(对于 V2.1)

Actually it's a bit more complex since hash() accepts a list of arguments of any type (incl. primitive types that have no built-in hashing method), so a custom approach is used -- check ObjectInspectorUtils.hashCode() and ObjectInspectorUtils.getBucketHashCode() in the source code here (for V2.1)

从ABC中选择XYZ, crc32(XYZ)
需要 Hive 1.3 并应用普通的旧循环冗余检查(可能通过 java.util.zip.CRC32),返回 BIGINT(32 位哈希)

select XYZ, crc32(XYZ) from ABC
requires Hive 1.3 and applies plain old Cyclic Redundancy Check (probably via java.util.zip.CRC32), returning a BIGINT (32 bit hash)

从ABC中选择XYZ, md5(XYZ), sha1(XYZ), sha2(XYZ,256), sha2(XYZ,512)
需要 Hive 1.3 并应用强大的加密散列函数,返回带有二进制十六进制表示的字符串(128、160、256 和 512 位散列)

select XYZ, md5(XYZ), sha1(XYZ), sha2(XYZ,256), sha2(XYZ,512) from ABC
requires Hive 1.3 and applies strong, cryptographic hash functions, returning a STRING with the hexadecimal representation of the binary (128, 160, 256 and 512 bit hashes)


该帖子 也有一个很好的解决方法,可以使用 Apache Commons 静态方法和 reflect()<在旧版本的 Hive 中应用加密哈希函数/代码>.


the answer to that post has also a very good workaround for applying crypto hash functions with older versions of Hive, using Apache Commons static methods and reflect().

这篇关于在不使用 Hive 中现有的哈希函数的情况下计算哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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