在 HIVE 中查找函数
Find function in HIVE
我想检查一个字段是否包含字符串。
我想要一个如下所示的函数:
FIND("string_to_find",field_to_search)
我的数据是这样的:
field_to_search
---------------
"no match in this string"
"record 2 has no matches"
"ahh finally xxxstring_to_findxxx is here"
我正在寻找一个函数来识别包含指定的字符串以及该字符串从哪个位置开始。
return
------
-1
-1
15
这可以通过在 hive 中使用 instr 和 if 来实现。
select if(instr(line,"xxxstring_to_findxxx")==0,-1,instr(line,"xxxstring_to_findxxx")) as position from find_tbl;
其中 line 是您的列名称
内置的 locate
函数几乎可以完全满足您的需求,只是对于您的输入,它会 return
return
------
0
0
16
因为它是从1开始索引的。所以你需要做的就是:
Select locate("string_to_find","ahh finally xxxstring_to_findxxx is here") -1; --returns 15
Select locate("string_to_find","foo") -1; --returns -1
我想检查一个字段是否包含字符串。
我想要一个如下所示的函数:
FIND("string_to_find",field_to_search)
我的数据是这样的:
field_to_search
---------------
"no match in this string"
"record 2 has no matches"
"ahh finally xxxstring_to_findxxx is here"
我正在寻找一个函数来识别包含指定的字符串以及该字符串从哪个位置开始。
return
------
-1
-1
15
这可以通过在 hive 中使用 instr 和 if 来实现。
select if(instr(line,"xxxstring_to_findxxx")==0,-1,instr(line,"xxxstring_to_findxxx")) as position from find_tbl;
其中 line 是您的列名称
内置的 locate
函数几乎可以完全满足您的需求,只是对于您的输入,它会 return
return
------
0
0
16
因为它是从1开始索引的。所以你需要做的就是:
Select locate("string_to_find","ahh finally xxxstring_to_findxxx is here") -1; --returns 15
Select locate("string_to_find","foo") -1; --returns -1