UDF 函数
val randomNew = (str :String, n: Int) => {
val buffer = ArrayBuffer[Int](-1)
var length:Int = 1
var index:Int = -1
for(a <- str){
index = index + 1
if (a ==':') {
length = length+1
buffer +=index
}
}
val routeKey = length <= n
routeKey match {
case true => str
case _ => {
val index = (new Random).nextInt(length)
if(index+n < length){
str.slice(buffer(index)+1,buffer(index+n)-1)
}else if(index+n ==length){
str.slice(buffer(index)+1,str.length)
}else{
val index2 =n-(length-index)
str.slice(buffer(index)+1,str.length)+":"+str.slice(buffer(0),buffer(index2)-1)
}
}
}
}
hive.udf.register("randomNew", randomNew)
假如有一串以 冒号 : 分割的字符串 val arr:String = “adfa:adfa:adag:asdf:hgfh:fgjgfj:fgjfjk:fjfkfjk”
随机获取三个子串的拼接字符串 randomNew(arr,3) adag:asdf:hgfh