Phoenix does not display negative integer values ​​correctly

I am creating an HBASE table with an integer value of -17678. But when I get it from pheonix, it gives me another positive value. RowKey is a composite rowkey row, and there is no problem with rowkey.

Base insert:

public class test { public static void main(String args[]) { Configuration config = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf("TEST")); Integer i=-17678; try { Put p = new Put(Bytes.toBytes("rowkey")); p.addColumn(Bytes.toBytes("test"),Bytes.toBytes("test"),Bytes.toBytes(i)); table.put(p); } finally { table.close(); connection.close(); } } } 

Search Phoenix:

select CAST ("Value" AS INTEGER) from TEST;

 +------------------------------------------+ | TO_INTEGER(test."Value") | +------------------------------------------+ | 2147465970 | +------------------------------------------+ 

Something is wrong here? or phoenix problem?

+6
source share
1 answer

http://phoenix.apache.org/language/datatypes.html

The binary representation is a 4-byte integer with an inverted sign bit (so that negative values ​​are sorted to positive values).

So, to convert from HBase serialization format to Phoenix format:

 (-17678)10 = (11111111111111111011101011110010)2 => (01111111111111111011101011110010)2 = (2147465970)10 

Thus, the output will be as expected. You need to know the binary representation when inserting data using HBase.

Direct HBase toByte to Phoenix readings are only possible with the CHAR and UNSIGNED_ * data types. You will have to serialize accordingly for other data types. i.e. setting i = 2147465970 when you want to insert -17678 .

I recommend using Phoenix to insert data. If you are worried about supporting the application depending on dependencies, Phoenix offers a thin jdbc driver (4mb instead of 86mb).

https://phoenix.apache.org/server.html


If you absolutely must use HBase, you can serialize signed numbers using bitwise XOR.

For integers, you would like to XOR your i using a bitmask to invert the sign of the character.

Bitmask to apply to a 4-byte integer:

 (10000000000000000000000000000000)2 = (-2147483648)10 

From http://ideone.com/anhgs5 we get 2147465970 . If you paste this with HBase when you read with Phoenix, you will read -17678) .

You will need another bitmask for Bigint (common bitmask with date types), Smallint, Float and Double.

+7
source

All Articles