hive gudf自定义解压函数测试用例失败,错误为“b cannot be cast to org.apache.hadoop.io.byteswritable”

bxpogfeg  于 2021-07-15  发布在  Hadoop
关注(0)|答案(1)|浏览(928)

我正在为通用的udf自定义解压求值函数编写测试用例,该函数对zip文件进行解压。此jar用于hivequery。这里是测试用例的代码,

public void testEvaluate() throws HiveException, IOException {
    Unzip unzip = new Unzip();
    File resourcesDirectory = new File("src/test/resources/test.zip");
    byte[] bytes = Files.readAllBytes( resourcesDirectory.toPath() );

    ObjectInspector binaryOI = PrimitiveObjectInspectorFactory.writableBinaryObjectInspector;
    ObjectInspector[] arguments = {binaryOI};
    unzip.initialize(arguments);

    GenericUDF.DeferredObject valueObj0 = new GenericUDF.DeferredJavaObject(bytes);
    GenericUDF.DeferredObject[] args = { valueObj0 };

    unzip.evaluate(args  );}

我得到的错误如下,

java.lang.ClassCastException: [B cannot be cast to org.apache.hadoop.io.BytesWritable

at org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableBinaryObjectInspector.getPrimitiveJavaObject(WritableBinaryObjectInspector.java:49)
at Unzip.evaluate(Unzip.java:32)
at UnzipTest.testEvaluate(UnzipTest.java:96)
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:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
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)

从DeferreObject[]args读取字节时,第行出错,

-  byte[] input = elementOI.getPrimitiveJavaObject( arg[0].get() );

ps:test.zip包含一个压缩到test.zip的文本文件(带有测试字符串)

cnh2zyt3

cnh2zyt31#

你需要包起来 byte[] 有了一个hive可以使用的可写程序, BytesWritable 对你来说。
如你所见 WritableBinaryObjectInspector.getPrimitiveJavaObject 期望 BytesWritable 对象作为输入,而不是字节数组。
尝试而不是

GenericUDF.DeferredObject valueObj0 = new GenericUDF.DeferredJavaObject(bytes);

执行以下操作:

GenericUDF.DeferredObject valueObj0 = new GenericUDF.DeferredJavaObject(new BytesWritable(bytes));

在本地复制你的案子 byte[] 自定义项内部 evaluate 方法成功。

相关问题