selectindexfrom(select10as num
)a
lateral view posexplode(split(space(num),' ')) k asindex,data
index
0
1
2
3
4
5
6
7
8
9
10
2.1 将字符串进行分割(借助subStr方法和辅助自增列切割字符串)
subStr(str, pos[, len])-returns the substring of str that starts at pos andisof length len orsubStr(bin, pos[, len])-returns the slice of byte array that starts at pos andisof length len
## 从位置0截取1个字符select subStr("123",0,1)
_c0
1
2.2 收集对应的下标集合
select
collect_list(index)as index_col
from(select10as num
)a
lateral view posexplode(split(space(num),' ')) k asindex,data
index_col
[0,1,2,3,4,5,6,7,8,9,10]
3 案例
3.1 案列代码
select
w,
str,
collect_list(k_)as w_index
from(select
k_,
str,
substring(str,k_,1)as w
from(select"查找字符串每个字符出现的下标"as str
)a
lateral view posexplode(split(space(length(str)),' ')) k as k_,data)b where k_>0groupby str,w
;