最近使用Hive來統(tǒng)計數(shù)據(jù),用了pyhs2來實(shí)現(xiàn)查詢,但是有些復(fù)雜的處理比如,自定義對域名的處理等,不能通過hql來實(shí)現(xiàn),發(fā)現(xiàn)能夠使用udf。
Java來實(shí)現(xiàn)Hive的寫法
package jsl.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
public final class DomainRoot extends UDF {
public Text evaluate(Text s) {
if (s == null) {return null;}
String tmp = s.toString();
tmp = this.getDomainRoot(tmp);
return new Text(tmp);
}
private String getDomainRoot(String domain) {
throw NoneImplementException("xxxx");
}
}
如果Java的UDF需要當(dāng)成常用的,不用每次add可以注冊到Hive中,
ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java中加入
registerUDF("domain_root", UDFParseUrl.class, false);并重新編譯hive即可
下面來說說重點(diǎn),通過Streaming用Python來寫處理。
關(guān)于Streaming的基礎(chǔ)內(nèi)容:


使用Transform來指定列,以及使用AS來指定生成的列以及可以指定轉(zhuǎn)換生成列的類型
hive> select transform(col1, clo2)
> using '/bin/cat' as (new_clo1 int, new_clo2 double) from table;
約束:首先必須add file到hive中(當(dāng)python中引用了其他如自己寫的模塊時,也需要一并add進(jìn)去)
其次非常不幸,在單獨(dú)的一個查詢中,不能夠使用UDAF的函數(shù)如sum()
再次不得為中間結(jié)果數(shù)據(jù)使用cluster by或distribute by
注意:對于優(yōu)化查詢,使用cluster by或distribute by 和sort by一起非常重要