1. 程式人生 > >hive udtf 輸入一列返回多行多列

hive udtf 輸入一列返回多行多列

create json clu spark beeline tin tca org.json 1.0

之前說到了hive udf,見https://blog.csdn.net/liu82327114/article/details/80670415

UDTF(User-Defined Table-Generating Functions) 用來解決 輸入一行輸出多行(On-to-many maping) 的需求。

繼承org.apache.hadoop.hive.ql.udf.generic.GenericUDTF,實現initialize, process, close三個方法。

UDTF首先會調用initialize方法,此方法返回UDTF的返回行的信息(返回個數,類型)。

初始化完成後,會調用process方法,真正的處理過程在process函數中,在process中,每一次forward()調用產生一行;如果產生多列可以將多個列的值放在一個數組中,然後將該數組傳入到forward()函數。

最後close()方法調用,對需要清理的方法進行清理。

1.創建maven工程

file->project structure->modules->點擊+號->new module->選擇maven

技術分享圖片

點擊next,填寫groupid(對應包結構)、artifactid(maven倉庫對應的坐標)

source java 代碼,操作如下圖file->project structure,

技術分享圖片

點擊apply,

2.開始寫java代碼

添加maven依賴

<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>0.13.1</version>
</dependency>

代碼如下

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.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 org.json.JSONArray;


import java.util.ArrayList;




public class helloudtf extends GenericUDTF {


@Override

// 可接收參數數組

public void process(Object[] objects) throws HiveException {
String input = objects[0].toString();
String[] result = new String[2];
result[0] = input;
result[1] = input+input;
String[] result1 = new String[2];
result1[0] = input+"a";
result1[1] = input+"a"+input;
forward(result);//一個forward 代表一行
forward(result1);


}
@Override
public StructObjectInspector initialize(ObjectInspector[] args)
throws UDFArgumentException {
if (args.length != 1)www.leyouzaixian2.com {
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 = www.ruishengks.com new ArrayList<ObjectInspector>();
fieldNames.add("col1");
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
fieldNames.add("col2");
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringO www.baidu620.com bjectInspector);

//定義了行的列數和類型
return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames,fieldOIs);
}
@Override
public void close() throws HiveException {


}

}

3.編包上傳到hdfs

在此項目pom文件的路徑下執行mvn clean install

將target文件中生成的jar文件上傳到hdfs上,路徑自己自定義,我直接上傳到/。

sudo -u hdfs hdfs dfs -put testudf-1.0-SNAPSHOT.jar /

4.使用hivesql或者sparksql加載自定義函數

beeline -u jdbc:hive2://node113.leap.com:10000 -n hive

create function test.iptonum as ‘com.liubl.helloudtf‘ using jar ‘hdfs:///testudf-1.0-SNAPSHOT.jar‘;

(com.liubl.HelloUdf為代碼類的全路徑自己去粘貼一下)

(測試sql見圖)

技術分享圖片

UDTF的使用:

UDTF有兩種使用方法,一種直接放到select後面,一種和lateral view一起使用。

1:直接select中使用:select explode_map(properties) as (col1,col2) from src;

  • 不可以添加其他字段使用:select a, explode_map(properties) as (www.mcyllpt.com/ col1,col2) from src
  • 不可以嵌套調用:select explode_www.yongshiyule178.com map(explode_map(properties)) from src
  • 不可以和group by/cluster by/distribute by/sort www.078881.cn by一起使用:select explode_map(properties) as (col1,col2) from src group by col1, col2

2:和lateral view一起使用:select src.id, mytable.col1, mytable.col2 from src www.yigouylpt2.cn lateral view explode_map(properties) mytable as col1, col2;

    • 此方法更為方便日常使用。執行過程相當於單獨執行了兩次抽取,然後union到一個表裏

hive udtf 輸入一列返回多行多列