首页 > 解决方案 > Hive 1.1.0 中的解码功能失败

问题描述

我尝试在配置单元 1.1.0 中使用解码函数执行转换。但它抛出错误。我已经从配置单元函数中引用了语法,但我仍然从解码函数中得到错误。总是我得到

SemanticException Decode() 恰好需要两个参数

hive> select * from tbl_test;
OK
1       aaaa
2       bbbb
3       cccc
4       dddd
Time taken: 0.12 seconds, Fetched: 4 row(s)
hive> select decode(col1,1,'hi','hello') from tbl_test;

FAILED: SemanticException [Error 10015]: Line 1:7 Arguments length mismatch ''hello'': Decode() 需要两个参数

hive> select decode(col1,1,'hi',null) from tbl_test;

失败:语义异常 [错误 10015]:第 1:7 行参数长度不匹配“TOK_NULL”:解码()恰好需要两个参数

hive> select decode(col1,1,'hi') from tbl_test;

FAILED: SemanticException [Error 10015]: Line 1:7 Arguments length mismatch ''hi'': Decode() 需要两个参数

hive> select decode(col1,1,'hi',"hello") from tbl_test;

FAILED: SemanticException [Error 10015]: Line 1:7 Arguments length mismatch '"hello"': Decode() 需要两个参数

标签: hivehiveql

解决方案


Hive中的 decode 函数与 Oracle 中的 decode 函数不同。

使用caseif 条件语句

select case col1 when 1 then 'hi' else 'hello' end from tbl_test;

推荐阅读