【发布时间】:2015-11-01 12:05:18
【问题描述】:
我编写了解码 cookie 并返回字符串列表的 UDF。 不幸的是,我在处理时遇到了 Hive 运行时错误
这是我的代码:
@Override
public ObjectInspector initialize(ObjectInspector[] input) throws UDFArgumentException {
ObjectInspector cookieContent = input[0];
if (!(isStringOI(cookieContent))){
throw new UDFArgumentException("only string");
}
this.cookieValue = (StringObjectInspector) cookieContent;
return ObjectInspectorFactory.getStandardListObjectInspector
(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
}
public Object evaluate(DeferredObject[] input) throws HiveException {
String encoded = cookieValue.getPrimitiveJavaObject(input[0].get());
try {
result = decode(encoded);
} catch (CodeException e) {
throw new UDFArgumentException();
}
return result;
}
public List<String> decode(String encoded) throws CodeException {
decodedBase64 = Base64.decodeBase64(encoded);
String decompressedArray = new String(getKadrs(decodedBase64));
String kadr= decompressedArray.substring(decompressedArray.indexOf("|") + 1);
List<String> kadrsList= new ArrayList(Arrays.asList(kadr.split(",")));
return kadrsList;
}
private byte[] getKadrs(byte[] compressed) throws CodeException {
Inflater decompressor = new Inflater();
decompressor.setInput(compressed);
ByteArrayOutputStream outPutStream = new ByteArrayOutputStream(compressed.length);
byte temp [] = new byte[1024];
while (!decompressor.finished()) {
try {
int count = decompressor.inflate(temp);
outPutStream.write(temp, 0, count);
}
catch (DataFormatException e) {
throw new CodeException ("Wrong data format", e);
}
}
try {
outPutStream.close();
} catch (IOException e) {
throw new CodeException ("Cant close outPutStream ", e);
}
return outPutStream.toByteArray();
}
结果是:
“kadr1、kadr20、kadr35、kadr12”。单元测试工作正常,但是当我尝试在 hive 中使用此功能时,我得到了这个:
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.hadoop.io.Text
at org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableStringObjectInspector.getPrimitiveWritableObject(WritableStringObjectInspector.java:41)
我很难调试,因为其他人必须实现我的 jar 才能看到结果,所以所有的建议都将不胜感激。
【问题讨论】:
-
我怀疑 input[0].get() 返回 Text 并且您正在尝试转换为 String
-
@ravindra - 我认为你已经倒退了,因为错误说不能转换为文本。
-
是的。你说的对。这是我的错。