package hive;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import java.util.ArrayList;
import java.util.List;
public class MyUDTF extends GenericUDTF {
List<String> output=new ArrayList<>();
@Override
public StructObjectInspector initialize(StructObjectInspector argOIs)
throws UDFArgumentException {
List<String> fieldNames=new ArrayList<>();
//类型
List<ObjectInspector> fieldOIs=new ArrayList<>();
// 设置别名
fieldNames.add("line2word");
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
}
@Override
public void process(Object[] args) throws HiveException {
String lines=args[0].toString();
String splitKey=args[1].toString();
String[] words=lines.split(splitKey);
for (String word:words){
output.clear();
output.add(word);
forward(output);
}
}
@Override
public void close() throws HiveException {
}
}
代码含义: 字符串列转行
添加jar包
创建函数
?
测试
?
?
|