使用字符串函数进行Hive查询

时间:2014-10-21 09:43:06

标签: java hadoop hive

我需要根据字符串长度来操作字符串。我已尝试过以下但获得例外。

要求是这样的。

我的列号为123456,结果应为12-34-56。如果是1234则为12-34-00。如果只有2位数,那么12-00-00。

IF(LENGTH(numbers) = 2,concat(numbers,'-00-00') OR LENGTH(numbers) = 4,concat(SUBSTR(numbers,1,2),'-',SUBSTR(numbers,3,2),'-00') OR LENGTH(numbers) = 6,concat(SUBSTR(numbers,1,2),'-',SUBSTR(numbers,3,2),SUBSTR(msg.numbers,5,2))) as finalnumbers

例外是:

FAILED: ClassCastException org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableStringObjectInspector cannot be cast to org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector

任何帮助?

1 个答案:

答案 0 :(得分:0)

这可以使用Hive查询中的CASE和WHEN来实现。这个论坛精确地解释了conditional functions

CASE 
WHEN (LENGTH(numbers) = "6") THEN concat(substring(numbers,1,2),'-',substring(numbers,3,2),'-',substring(numbers,5,2)) 
WHEN (LENGTH(numbers) = "4") THEN concat(substring(numbers,1,2),'-',substring(numbers,3,2),'-00')
WHEN (LENGTH(numbers) = "2") THEN concat(numbers,'-00-00') as finalnumbers