Search code examples
javajythonhbase

Invalid results from HBase Bytes.toString(byte[] b) in Jython


Wondering what I'm doing wrong here. Using Jython 2.2.1 on Java 1.5 with Hbase 0.90. Can't get Bytes.toString(byte[] b) to work. It returns what looks like an address. But when I use the overloaded Bytes.toString(byte[] b, int off, int len), it returns the proper result.

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 '[B@12121212'
print Bytes.toString(t, 0, len(t))   // returns the string properly

Anyone seen this before?


Solution

  • You aren't doing anything wrong. I'm running Jython 2.7 on Java 1.8 and HBase 1.2.4 and am encountering the exact same behavior. Here is what seems to be happening: Java supports multiple methods by the same name that accept different sets of parameters, and thus Jython internally has to disambiguate between multiple methods by the same name that accept different parameters. The Byte[] array that is being converted to a string is represented as a Python array or some subclass thereof, which has a toString( ) method that accepts one parameter (self). The 'Bytes' object, being a subclass of this, has both the Python array.toString(self) method and the Java .toString(final [] byte b) and .toString(final [] byte b, int off, int len) methods.

    It must be that the way the Bytes object is constructed causes Python's .toString(self) to supersede Java's .toString(final [] byte b) but the other method still works because there is no Python .toString( ) method with three parameters to supersede the other Java .toString( ). (See https://docs.python.org/2.7/library/array.html?highlight=tostring#array.array.tostring)

    Solution: A modified import with a few extra lines of code fixes this.

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