Phoenix不能正确显示负整数值 [英] Phoenix doesn't display negative integer values correctly

查看:195
本文介绍了Phoenix不能正确显示负整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个值为整数-17678的HBASE表。但是当我从pheonix检索它时,它给了我不同的正面价值。 RowKey是一个复合rowkey,rowkey没有问题。



Hbase插入:

  public class test 
{
public static void main(String args [])
{
Configuration config = HBaseConfiguration.create();

连接连接= ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf(TEST));
Integer i = -17678;

尝试
{
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();
}

}
}

Phoenix提取:

从TEST中选择CAST(ValueAS INTEGER);

  + ------------------------------------------ + 
| TO_INTEGER(test。Value)|
+ ------------------------------------------ +
| 2147465970 |
+ ------------------------------------------ +

这里有什么不对吗?或凤凰问题?

解决方案

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


二进制表示是一个4字节的整数,符号位翻转(因此负值在正值之前排序)。

因此,要从HBase Phoenix格式的序列化格式:

$ $ p $ code>( - 17678)10 =(11111111111111111011101011110010)2
=> (01111111111111111011101011110010)2 =(2147465970)10

因此输出如预期。使用HBase插入数据时,您需要了解二进制表示。

只有使用CHAR和UNSIGNED_ *数据类型才能将HBase toByte直接读取到Phoenix读取。您必须正确地序列化其他数据类型。即。当你的意思是插入 -17678 时,设置 i = 2147465970

我推荐使用Phoenix来插入数据。如果您担心让应用程序依赖于其他应用程序,Phoenix提供了一个瘦的jdbc驱动程序(4mb而不是86mb)。 $ b

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






如果您绝对必须使用HBase,则可以使用按位XOR序列化带符号数字。



对于整数,您希望使用位掩码对 i 进行XOR运算以翻转符号位。

将掩码应用于一个4字节的整数是:

 (10000000000000000000000000000000)2 =( -  214746848)10 

http://ideone.com/anhgs5 ,我们得到 2147465970 。如果你使用HBase插入,那么当你使用Phoenix阅读时,你会看到 -17678)

需要为Bigint(与日期 - 时间类型共享位掩码),Smallint,Float和Double不同的位掩码。


I am creating a HBASE table with a value of integer -17678. But when i retrieve it from pheonix it gives me a different positive value. RowKey is a composite rowkey and there is no problem with rowkey.

Hbase insertion:

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();
          }

    }
}

Phoenix retrieval:

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

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

Anything wrong here? or a phoenix issue?

解决方案

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

The binary representation is a 4 byte integer with the sign bit flipped (so that negative values sorts before positive values).

So to convert from HBase serialization format to Phoenix format:

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

Thus the output is as expected. You need to be aware of the binary representation when inserting data using HBase.

Direct HBase toByte to Phoenix reads is only possible with CHAR and UNSIGNED_* data types. You'd have to serialize appropriately for other data types. ie. setting i = 2147465970 when you mean to insert -17678.

I recommend using Phoenix to insert data. If you are worried about keeping your application light 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 by using a bitwise XOR.

For integers, you would want to XOR your i with a bitmask to flip the sign bit.

The bitmask to apply to a 4-byte Integer is:

(10000000000000000000000000000000)2 = (-2147483648)10

From http://ideone.com/anhgs5 , we get 2147465970. If you insert that using HBase, when you read using Phoenix, you will read -17678).

You will need a different bitmask for Bigint (shared bitmask with the date-time types), Smallint, Float, and Double.

这篇关于Phoenix不能正确显示负整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆