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

Phoenix客戶端使用自定義函數(shù)UDF時候是正常的,但是在本地測試的時候報Function類找不到的異常。

異常演示

例子

@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);
    }
}

導入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'

我們在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

一切看起來都是正常的酸休,那怎么會出現(xiàn)這種錯誤呢?
原因就出在配置上骂租,默認動態(tài)加載的jar包會復制一份到hbase.local.dir目錄下,動態(tài)加載的jar也是默認從這個配置的目錄中加載的,所以只要把這個目錄配置正確即可斑司。

解決方案

修改程序的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
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末渗饮,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子宿刮,更是在濱河造成了極大的恐慌互站,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件僵缺,死亡現(xiàn)場離奇詭異胡桃,居然都是意外死亡,警方通過查閱死者的電腦和手機磕潮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門翠胰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人自脯,你說我怎么就攤上這事之景。” “怎么了冤今?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵闺兢,是天一觀的道長。 經(jīng)常有香客問我戏罢,道長屋谭,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任龟糕,我火速辦了婚禮桐磁,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘讲岁。我一直安慰自己我擂,他們只是感情好衬以,可當我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著校摩,像睡著了一般看峻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上衙吩,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天互妓,我揣著相機與錄音,去河邊找鬼坤塞。 笑死冯勉,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的摹芙。 我是一名探鬼主播灼狰,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼浮禾!你這毒婦竟也來了交胚?” 一聲冷哼從身側(cè)響起串结,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤患膛,失蹤者是張志新(化名)和其女友劉穎呈昔,沒想到半個月后慎冤,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體绊袋,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡抛寝,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年度硝,在試婚紗的時候發(fā)現(xiàn)自己被綠了实蔽。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片卷扮。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡荡澎,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出晤锹,到底是詐尸還是另有隱情摩幔,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布鞭铆,位于F島的核電站或衡,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏车遂。R本人自食惡果不足惜封断,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望舶担。 院中可真熱鬧坡疼,春花似錦、人聲如沸衣陶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至教沾,卻和暖如春蒲跨,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背详囤。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工财骨, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人藏姐。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像该贾,于是被迫代替她去往敵國和親羔杨。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,678評論 2 354

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