简体   繁体   中英

Cast a hexadecimal string to an array of bigint in hive

I have a column that contains a length 16 hexademical string. I would like to convert it to a bigint. Is there any way to accomplish that? The usual approach returns null since the input string could represent a number > 2^63-1.

select 
cast(conv(hash_col, 16, 10) as bigint) as p0, 
conv(hash_col, 16, 10) as c0 
from mytable limit 10

在此处输入图像描述

I have also tried using unhex(..),

cast(unhex(hash_col) as bigint) as p0 from mytable limit 10

but got the following error

No matching method for class org.apache.hadoop.hive.ql.udf.UDFToLong with (binary). Possible choices: FUNC (bigint) FUNC (boolean) FUNC (decimal(38,18)) FUNC (double) FUNC (float) FUNC (int) FUNC (smallint) FUNC (string) FUNC (timestamp) FUNC (tinyint) FUNC (void)

If I don't do the cast(.. as bigint) part, I get some undisplayable binary value for p0. It seems unhex is not exactly the inverse of hex in hive.

Your values are out of range for BigInt

Ref: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types

Max range for BigInt is 9,223,372,036,854,775,807

Use decimal(20,0) instead.

select cast(conv('85A58F8B014692CA',16,10) as decimal(20,0))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM