2012-02-24 3 views
0

내가 뭘 잘못하고 있는지 궁금합니다. Hbase 0.90과 함께 Java 1.5에서 Jython 2.2.1 사용. Bytes.toString(byte[] b)을 사용할 수 없습니다. 주소처럼 보이는 것을 반환합니다. 하지만 오버로드 된 Bytes.toString(byte[] b, int off, int len)을 사용하면 적절한 결과가 반환됩니다.자이 썬의 HBase Bytes.toString (byte [] b)에서 잘못된 결과가 발생했습니다.

g = Get(Bytes.toBytes(id)) 
res = self.table.get(g) 

t = res.getValue(Bytes.toBytes('stuff'), Bytes.toBytes('t')) 
print Bytes.toString(t)    // returns stuff like '[[email protected]' 
print Bytes.toString(t, 0, len(t)) // returns the string properly 

누구나 전에 본적이 있습니까?

답변

2

: 여기 (HBase와의 0.90.3에서) 소스 코드입니다. Java 1.8 및 HBase 1.2.4에서 Jython 2.7을 실행 중이며 정확히 동일한 문제가 발생합니다. 자바가 다른 매개 변수 집합을 받아들이는 동일한 이름으로 여러 메소드를 지원하므로 자이 썬은 내부적으로 다른 매개 변수를 허용하는 동일한 이름으로 여러 메소드를 모호하게 구분해야한다. 문자열로 변환되는 Byte [] 배열은 Python 배열 또는 그 하위 클래스로 표현됩니다.이 배열에는 하나의 매개 변수 (self)를 허용하는 toString() 메서드가 있습니다. 이것의 하위 클래스 인 'Bytes'객체는 Python의 array.toString (self) 메소드와 Java .toString (final [] byte b) 및 .toString (final [] byte b, int off, int len) 메소드를 호출합니다.

Bytes 객체가 생성되는 방식으로 인해 Python의 .toString (self)가 Java의 .toString (final [] byte b)를 대체하지만 Python .toString() 메서드가 없기 때문에 다른 메서드가 여전히 작동해야합니다. 세 개의 매개 변수로 다른 Java .toString()을 대체합니다. (https://docs.python.org/2.7/library/array.html?highlight=tostring#array.array.tostring)

솔루션 : 수정 된 가져 오기로 몇 줄의 코드를 추가하면 수정되었습니다.

import org.apache.hadoop.hbase.util.Bytes as BrokenBytes 
class Bytes(BrokenBytes): 
    @staticmethod 
    def toString(x): return(BrokenBytes.toString(x, 0, len(x))) 
0

불가능합니다. 당신이 게시 한 것보다 당신이하는 일에 더 많은 것이 있어야합니다. toString의 첫 번째 오버로드가 두 번째 오버로드를 호출합니다. 당신이 잘못 작업을 수행하지 않는

/** 
    * @param b Presumed UTF-8 encoded byte array. 
    * @return String made from <code>b</code> 
    */ 
    public static String toString(final byte [] b) { 
    if (b == null) { 
     return null; 
    } 
    return toString(b, 0, b.length); 
    } 

    /* snip */ 

    /** 
    * This method will convert utf8 encoded bytes into a string. If 
    * an UnsupportedEncodingException occurs, this method will eat it 
    * and return null instead. 
    * 
    * @param b Presumed UTF-8 encoded byte array. 
    * @param off offset into array 
    * @param len length of utf-8 sequence 
    * @return String made from <code>b</code> or null 
    */ 
    public static String toString(final byte [] b, int off, int len) { 
    if (b == null) { 
     return null; 
    } 
    if (len == 0) { 
     return ""; 
    } 
    try { 
     return new String(b, off, len, HConstants.UTF8_ENCODING); 
    } catch (UnsupportedEncodingException e) { 
     LOG.error("UTF-8 not supported?", e); 
     return null; 
    } 
    } 
+0

답장을 보내 주셔서 감사합니다. 아마도 자이 썬이 Bytes.toString (byte [])을 통과하기 전에 byte []로 펑키 한 일을하고있을 가능성이 있는가? 나는 이것을 알아낼 수 없지만이 행동을 일으키기 위해 내가하고있는 이상한 것은 없다. – Ryan

관련 문제