2015-11-01 235 views
0

我已經編寫了解碼cookie並返回字符串列表的UDF。 不幸的是我得到了蜂巢運行時錯誤在處理Hive UDF - Java String castexception

這裏是我的代碼:

@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」。單元測試工作得很好,但是當我試圖在蜂巢使用這個功能我得到這個:

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看到的結果,因此,所有意見將不勝感激。

+0

我懷疑輸入[0]獲得()返回文本和您要轉換爲字符串 –

+0

@ravindra - 我認爲你已經倒退了,因爲錯誤說不能轉換爲文本。 –

+0

是的。你是對的。這是我的錯誤。 –

回答

0

您的evaluate方法當前返回一個String,它不是Hadoop數據類型。您應該用return new Text(result)將字符串包裝在Text對象中。

0

拉溫德拉是正確的

我在初始化
回報ObjectInspectorFactory.getStandardListObjectInspector (PrimitiveObjectInspectorFactory.writableStringObjectInspector);

和WritableStringObjectInspector返回文本

我改成了javaStringObjectInspector,它返回字符串所有的罰款 感謝