UDTF(User-Defined Table-Generating Functions)用来解决输入一行输出多行(one-to-many maping)的需求。
编写自己的UDTF:
1.继承org.apache.hadoop.hive.ql.udf.generic.GenericUDTF。
2.实现initialize(),process(),close()三个方法。
3.UDTF首先会调用initialize()方法,此方法返回UDTF的返回行的信息(返回个数,类型)。
4.初始化完成后会调用process()方法,对传入的参数进行处理,可以通过forward()方法把结果返回。
5.最后调用close()对需要清理的方法进行清理。
示例:使用UDTF对"Key:Value"这种字符串进行切分,返回结果为Key,Value两个字段。
import java.util.ArrayList; import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF; import org.apache.hadoop.hive.ql.exec.UDFArgumentException; import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException; import org.apache.hadoop.hive.ql.metadata.HiveException; 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; public class ExplodeMap extends GenericUDTF{ @Override public void close() throws HiveException { // TODO Auto-generated method stub } @Override public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException { if (args.length != 1) { throw new UDFArgumentLengthException("ExplodeMap takes only one argument"); } if (args[0].getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new UDFArgumentException("ExplodeMap takes string as a parameter"); } ArrayList<String> fieldNames = new ArrayList<String>(); ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>(); fieldNames.add("col1"); fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector); fieldNames.add("col2"); fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector); return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames,fieldOIs); } @Override public void process(Object[] args) throws HiveException { String input = args[0].toString(); String[] test = input.split(";"); for(int i=0; i<test.length; i++) { try { String[] result = test[i].split(":"); forward(result); } catch (Exception e) { continue; } } } }
1.打包发送到服务器。
2.添加到Hive环境中:
hive (hive)> add jar /usr/local/src/udtf.jar<span style="font-family: Arial, Helvetica, sans-serif;">;</span> Added /usr/local/src/udtf.jar to class path Added resource: /usr/local/src/udtf.jar
3.创建临时函数:
hive (hive)> create temporary function explode_map as 'com.lixue.udtf.ExplodeMap'; OK Time taken: 0.0080 seconds
4.查询(UDTF有两种使用方式,一种是直接放到select后面,另外一种是和lateral view一起使用):
hive (hive)> select explode_map('name:lavimer;age:23') as (col1,col2) from employees; //MapReduce OK col1 col2 name lavimer age 23 name lavimer age 23 name lavimer age 23
注:不可以添加其他字段使用,如下:
select a, explode_map(properties) as (col1,col2) from src
不可以嵌套调用:
select explode_map(explode_map(properties)) from src
不可以和group by/cluster by/distribute by/sort by一起使用:
select explode_map(properties) as (col1,col2) from src group by col1, col2
可以和lateral view一起使用:
hive (hive)> select user.id,employees.col1,employees.col2 from user lateral view explode_map('name:lavimer,age:23') employees as col1,col2; //MapReduce... OK id col1 col2 1 name lavimer 1 age 23 2 name lavimer 2 age 23 3 name lavimer 3 age 23
注:此方法更为方便使用。执行过程相当于单独执行了两次抽取,然后union到一个表里。