HiveのUDTF
Refer to:http://blog.csdn.net/wf1982/article/details/7623708
1.紹介
UDTF(User-Defined Table-Generating Functions) 入力1行の出力マルチラインの需要を解決するために使用されます。同時に、一列を複数の列に分割する問題(Hiveは、Listを含む複雑なデータフォーマットをサポートする)を解決してもよい。
2.作成に必要なUDTF
1)org.apache.hadoop.hive.ql.udf.genersic.GeneraicUDTFを継承します。2)initialize、process、closeの3つの方法を実現する3)UDTFはまずinitialize方法を呼び出し、この方法はUDTFの戻り値の情報(戻り値の数、タイプ)を返します。初期化が完了すると、プロcessメソッドを呼び出して、入ってきたパラメータを処理し、結果をforword()法で返すことができます。最後にclose()メソッドを呼び出して、整理する方法を整理します。
これはkey:valueを切り分けるためのものです。key:valueという文字列は、結果としてkey、valueの2つのフィールドを返します。
3.使い方
UDTFには二つの使い方があります。一つは直接selectの後ろに置いて、一つはlateral viewと一緒に使います。
1)直接selectで使用する:select explode_map(properties)as(col 1,col 2)from src;
他のフィールドを追加して使用してはいけません。select a,explode_map(properties)as(col 1,col 2)from src 入れ子できません。コールしてください。select explode_map from src group by/cluster by/distribute by/sort byと一緒に使ってはいけません。select explode_map(properties)as(col 1,col 2)from src group by col 1,col 2
2)lateral viewと一緒に使う: select src.id,mystable.com 1,mystable.com 2 from src.lateral view explode_map(properties)mytable as col 1,col 2 この方法は日常的に使うのに便利です。実行プロセスは単独で2回の抽出を実行した後、unionが1つのテーブルに到達するのに相当します。
4.参照文書 http://wiki.apache.org/hadoop/Hive/LanguageManual/UDF
http://wiki.apache.org/hadoop/Hive/DeveloperGuide/UDTF
http://www.slideshare.net/pauly1/userdefined-table-generating-functions
Apppendix:
一つは文字列を複数のフィールドに分割するためのものです。
使い方は:
1.紹介
UDTF(User-Defined Table-Generating Functions) 入力1行の出力マルチラインの需要を解決するために使用されます。同時に、一列を複数の列に分割する問題(Hiveは、Listを含む複雑なデータフォーマットをサポートする)を解決してもよい。
2.作成に必要なUDTF
1)org.apache.hadoop.hive.ql.udf.genersic.GeneraicUDTFを継承します。2)initialize、process、closeの3つの方法を実現する3)UDTFはまずinitialize方法を呼び出し、この方法はUDTFの戻り値の情報(戻り値の数、タイプ)を返します。初期化が完了すると、プロcessメソッドを呼び出して、入ってきたパラメータを処理し、結果をforword()法で返すことができます。最後にclose()メソッドを呼び出して、整理する方法を整理します。
これはkey:valueを切り分けるためのものです。key:valueという文字列は、結果としてkey、valueの2つのフィールドを返します。
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;
}
}
}
}
3.使い方
UDTFには二つの使い方があります。一つは直接selectの後ろに置いて、一つはlateral viewと一緒に使います。
1)直接selectで使用する:select explode_map(properties)as(col 1,col 2)from src;
他のフィールドを追加して使用してはいけません。select a,explode_map(properties)as(col 1,col 2)from src 入れ子できません。コールしてください。select explode_map from src group by/cluster by/distribute by/sort byと一緒に使ってはいけません。select explode_map(properties)as(col 1,col 2)from src group by col 1,col 2
2)lateral viewと一緒に使う: select src.id,mystable.com 1,mystable.com 2 from src.lateral view explode_map(properties)mytable as col 1,col 2 この方法は日常的に使うのに便利です。実行プロセスは単独で2回の抽出を実行した後、unionが1つのテーブルに到達するのに相当します。
4.参照文書 http://wiki.apache.org/hadoop/Hive/LanguageManual/UDF
http://wiki.apache.org/hadoop/Hive/DeveloperGuide/UDTF
http://www.slideshare.net/pauly1/userdefined-table-generating-functions
Apppendix:
一つは文字列を複数のフィールドに分割するためのものです。
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hive.ql.exec.Description;
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.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector;
/**
* GenericUDTFRD: this
*
*/
@Description(name = "tr_rd", value = "_FUNC_(x)")
public class GenericUDTFRD extends GenericUDTF {
private static Log LOG = LogFactory.getLog(GenericUDTFRD.class.getName());
@Override
public void close() throws HiveException {
}
@Override
public StructObjectInspector initialize(ObjectInspector[] args)
throws UDFArgumentException {
LOG.debug("========================initialize");
LOG.debug("args.length" + args.length);
if (args.length != 2) {
throw new UDFArgumentLengthException(
"ExplodeMap takes only two argument");
}
WritableConstantIntObjectInspector x = (WritableConstantIntObjectInspector) args[1];
int numCols = x.getWritableConstantValue().get();
LOG.debug("numCols:" + numCols);
// construct output object inspector
ArrayList<String> fieldNames = new ArrayList<String>(numCols);
ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>(
numCols);
for (int i = 0; i < numCols; ++i) {
fieldNames.add("c" + i);
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
}
return ObjectInspectorFactory.getStandardStructObjectInspector(
fieldNames, fieldOIs);
}
@Override
public void process(Object[] o) throws HiveException {
LOG.debug("========================process");
String input = o[0].toString();
//
String[] test = input.split("\t");
LOG.debug("input:" + input);
LOG.debug("test:" + test);
for(int i=0;i<test.length;i++){
if("".equals(test[i])||null==test[i]){
test[i]="\\N";
}
}
forward(test);
}
@Override
public String toString() {
return "tr_rd";
}
}
使い方は:
-- , 3 。
select tr_rd(tt.col1,3) as (c1,c2,c3)
from table tt