Beeline里面执行hive脚本函数nvl2()与replace()报错
写脚本的时候是在impala里面执行的,都正常,但是转换为调度的时候是在beeline里面执行的 就会有问题了. 详情如下:
replace函数: (去掉字符串里面所有空格)
select replace(' hell o wor d ',' ','');
impala执行命令: select replace(' hell o wor d ',' ','');
impala执行结果为: helloworld
beeline执行命令: select replace(' hell o wor d ',' ','');
beeline执行结果为:Error: Error while compiling statement: FAILED: SemanticException Line 0:-1 Invalid function 'replace' (state=42000,code=40000)
beeline解决方案: 使用 translate函数替换replace函数
beeline执行命令: select translate(' hell o wor d ',' ','');
beeline执行结果为: helloworld
nvl2()函数: 判断第一个表达式是否为空,如果为空则取第三表达式的值,如果不为空则取第二个表达式的值
select nvl2('FEIXIA',concat(translate('DU AN',' ',''),' ',translate('FEI XIA ',' ','')),NULL);
impala执行命令: select nvl2('FEIXIA',concat(translate('DU AN',' ',''),' ',translate('FEI XIA ',' ','')),NULL);
impala执行结果: DUAN FEIXIA
beeline执行命令: select nvl2('FEIXIA',concat(translate('DU AN',' ',''),' ',translate('FEI XIA ',' ','')),NULL);
beeline执行结果: Error: Error while compiling statement: FAILED: SemanticException [Error 10011]: Line 1:7 Invalid function 'nvl2' (state=42000,code=10011)
beeline解决方案: 使用case when 处理
beeline执行命令: select case when nvl('FEIXIA','')<>'' then concat(translate('DU AN',' ',''),' ',translate('FEI XIA ',' ','')) else '' end englishName;
beeline执行结果: DUAN FEIXIA