Phoenix自定義函數(shù)UDF異常解決方案

Phoenix客戶端使用自定義函數(shù)UDF時(shí)候是正常的,但是在本地測(cè)試的時(shí)候報(bào)Function類(lèi)找不到的異常拉宗。

異常演示

例子

@Test
    public void testSql2() throws SQLException {
        String sql = "select CRC32(\"userId\") from TEST_LOG";
        Configuration conf = new Configuration();
        conf.addResource("hbase-site.xml");//BUG修改處
        conf.addResource("hdfs-site.xml");
        PhoenixDriver phoenixDriver = PhoenixDriver.getInstaceWithProperties(conf,PhoenixDriver.loadProperties());
        ResultSet rs = phoenixDriver.query(sql);
        int columns=rs.getMetaData().getColumnCount();
        while(rs.next()) {
            for(int i=1;i<=columns;i++) {
                System.out.print(rs.getString(i));
                System.out.print("\t\t");
            }
            System.out.println();
        }
    }

CRC32Function.java

@BuiltInFunction(name = CRC32Function.NAME, args = {@Argument()})
public class CRC32Function extends ScalarFunction {
    public static final String NAME = "CRC32";
    public static final Integer LENGTH = 19;


    public CRC32Function() throws SQLException {
    }

    public CRC32Function(List<Expression> children) throws SQLException {
        super(children);
    }

    public static void main(String[] args) {
        CRC32 crc32 = new CRC32();
        crc32.update("lake".getBytes());
        System.out.println(crc32.getValue());
    }

    @Override
    public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
        if (!getChildExpression().evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            return true;
        }
        CRC32 crc32 = new CRC32();
        crc32.update(ptr.get(),ptr.getOffset(), ptr.getLength());
        ptr.set(Bytes.toBytes(String.valueOf(crc32.getValue())));
        return true;
    }

    @Override
    public PDataType getDataType() {
        return PVarchar.INSTANCE;
    }

    @Override
    public Integer getMaxLength() {
        return LENGTH;
    }

    @Override
    public boolean isNullable() {
        return getChildExpression().isNullable();
    }

    @Override
    public String getName() {
        return NAME;
    }

    private Expression getChildExpression() {
        return children.get(0);
    }
}

導(dǎo)入phoenix-core 編譯打包上傳至HDFS中

hadoop fs -put phoenix-udfs-1.0-SNAPSHOT.jar /hbase/lib/

Phoenix命令中創(chuàng)建函數(shù)

create function CRC32(varchar) returns varchar as 'com.dounine.phoenixudfs.CRC32Function' using jar 'hdfs:///hbase/lib/phoenix-udfs-1.0-SNAPSHOT.jar'

我們?cè)?code>Phoenix客戶端查詢Function表是有數(shù)據(jù)的

jdbc:phoenix:host1.demo.com:2181> select * from SYSTEM."FUNCTION";
+------------+----------------+-----------+------------------------------------------+--------------------------------------------------+--------+
| TENANT_ID  | FUNCTION_NAME  | NUM_ARGS  |                CLASS_NAME                |                     JAR_PATH                     | RETURN |
+------------+----------------+-----------+------------------------------------------+--------------------------------------------------+--------+
|            | CRC32          | 1         | com.dounine.phoenixudfs.CRC32Function  | hdfs:///hbase/lib/phoenix-udfs-1.0-SNAPSHOT.jar  | varcha |
|            | CRC32          | null      |                                          |                                                  |        |
+------------+----------------+-----------+------------------------------------------+--------------------------------------------------+--------+
4 rows selected (0.068 seconds)

jar包查看

jdbc:phoenix:storm2.starsriver.cn:2181> list jars;
+---------------------------------------------------------------------------+
|                               jar_location                                |
+---------------------------------------------------------------------------+
| hdfs://host5.demo.com:8020/hbase/lib/phoenix-udfs-1.0-SNAPSHOT.jar  |
+---------------------------------------------------------------------------+
1 row selected (0.645 seconds)

程序異常如下

java.sql.SQLException: java.lang.reflect.InvocationTargetException

    at org.apache.phoenix.parse.FunctionParseNode.create(FunctionParseNode.java:280)
    at org.apache.phoenix.compile.ExpressionCompiler.visitLeave(ExpressionCompiler.java:336)
    at org.apache.phoenix.compile.ProjectionCompiler$SelectClauseVisitor.visitLeave(ProjectionCompiler.java:700)
    at org.apache.phoenix.compile.ProjectionCompiler$SelectClauseVisitor.visitLeave(ProjectionCompiler.java:585)
    at org.apache.phoenix.parse.FunctionParseNode.accept(FunctionParseNode.java:86)
    at org.apache.phoenix.compile.ProjectionCompiler.compile(ProjectionCompiler.java:412)
    at org.apache.phoenix.compile.QueryCompiler.compileSingleFlatQuery(QueryCompiler.java:561)
    at org.apache.phoenix.compile.QueryCompiler.compileSingleQuery(QueryCompiler.java:507)
    at org.apache.phoenix.compile.QueryCompiler.compileSelect(QueryCompiler.java:193)
    at org.apache.phoenix.compile.QueryCompiler.compile(QueryCompiler.java:153)
    at org.apache.phoenix.jdbc.PhoenixStatement$ExecutableSelectStatement.compilePlan(PhoenixStatement.java:490)
    at org.apache.phoenix.jdbc.PhoenixStatement$ExecutableSelectStatement.compilePlan(PhoenixStatement.java:456)
    at org.apache.phoenix.jdbc.PhoenixStatement$1.call(PhoenixStatement.java:302)
    at org.apache.phoenix.jdbc.PhoenixStatement$1.call(PhoenixStatement.java:291)
    at org.apache.phoenix.call.CallRunner.run(CallRunner.java:53)
    at org.apache.phoenix.jdbc.PhoenixStatement.executeQuery(PhoenixStatement.java:290)
    at org.apache.phoenix.jdbc.PhoenixStatement.executeQuery(PhoenixStatement.java:283)
    at org.apache.phoenix.jdbc.PhoenixStatement.executeQuery(PhoenixStatement.java:1793)
    at cn.starsriver.flink.phoenix.PhoenixDriver.query(PhoenixDriver.java:96)
    at com.dounine.test.PhoenixTest1.testSql2(PhoenixTest1.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.apache.phoenix.parse.FunctionParseNode.create(FunctionParseNode.java:268)
    ... 41 more
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.dounine.phoenixudfs.CRC32Function
    at org.apache.phoenix.expression.function.UDFExpression.constructUDFFunction(UDFExpression.java:170)
    at org.apache.phoenix.expression.function.UDFExpression.<init>(UDFExpression.java:72)
    ... 46 more
Caused by: java.lang.ClassNotFoundException: cn.starsriver.phoenixudfs.CRC32Function
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at org.apache.hadoop.hbase.util.DynamicClassLoader.tryRefreshClass(DynamicClassLoader.java:173)
    at org.apache.hadoop.hbase.util.DynamicClassLoader.loadClass(DynamicClassLoader.java:140)
    at org.apache.phoenix.expression.function.UDFExpression.constructUDFFunction(UDFExpression.java:164)
    ... 47 more

一切看起來(lái)都是正常的,那怎么會(huì)出現(xiàn)這種錯(cuò)誤呢?
原因就出在配置上踊兜,默認(rèn)動(dòng)態(tài)加載的jar包會(huì)復(fù)制一份到hbase.local.dir目錄下,動(dòng)態(tài)加載的jar也是默認(rèn)從這個(gè)配置的目錄中加載的兽掰,所以只要把這個(gè)目錄配置正確即可芭碍。

解決方案

修改程序的hbase-site.xml中的hbase.local.dirUDFjar包所在目錄即可

<property>
      <name>hbase.local.dir</name>
      <value>/tmp/hbase-hbase/local</value>
    </property>

目錄下面有編譯好的UDF

[root@dounine bin]# ls /tmp/hbase-hbase/local/jars/
phoenix-udfs-1.0-SNAPSHOT.jar
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市孽尽,隨后出現(xiàn)的幾起案子窖壕,更是在濱河造成了極大的恐慌,老刑警劉巖杉女,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瞻讽,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡速勇,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)坎拐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)烦磁,“玉大人,你說(shuō)我怎么就攤上這事哼勇《嘉保” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵积担,是天一觀的道長(zhǎng)陨晶。 經(jīng)常有香客問(wèn)我,道長(zhǎng)帝璧,這世上最難降的妖魔是什么先誉? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任湿刽,我火速辦了婚禮,結(jié)果婚禮上谆膳,老公的妹妹穿的比我還像新娘叭爱。我一直安慰自己撮躁,他們只是感情好漱病,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著把曼,像睡著了一般杨帽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嗤军,一...
    開(kāi)封第一講書(shū)人閱讀 51,737評(píng)論 1 305
  • 那天注盈,我揣著相機(jī)與錄音,去河邊找鬼叙赚。 笑死老客,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的震叮。 我是一名探鬼主播胧砰,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼苇瓣!你這毒婦竟也來(lái)了尉间?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤击罪,失蹤者是張志新(化名)和其女友劉穎哲嘲,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體媳禁,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡眠副,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了竣稽。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片囱怕。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖丧枪,靈堂內(nèi)的尸體忽然破棺而出光涂,到底是詐尸還是另有隱情,我是刑警寧澤拧烦,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布忘闻,位于F島的核電站,受9級(jí)特大地震影響恋博,放射性物質(zhì)發(fā)生泄漏齐佳。R本人自食惡果不足惜私恬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望炼吴。 院中可真熱鬧本鸣,春花似錦、人聲如沸硅蹦。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)童芹。三九已至涮瞻,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間假褪,已是汗流浹背署咽。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留生音,地道東北人宁否。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像缀遍,于是被迫代替她去往敵國(guó)和親慕匠。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容