Phoenix自定义函数UDF异常解决方案

Phoenix客户端使用自定义函数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命令中创建函数

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

我们在Phoenix客户端查询Function表是有数据的

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

一切看起来都是正常的,那怎么会出现这种错误呢?
原因就出在配置上,默认动态加载的jar包会复制一份到hbase.local.dir目录下,动态加载的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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容