为hbase设计复合行键

l3zydbqr  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(301)

我正在尝试创建具有以下结构的hbase表。


**rowkey**                |**CF1**

(customerid,txtimestamp)|customerid,amount

我想使用customerid查询特定期间范围内的记录。
我的rowkey以相反的顺序使用客户id和事务时间戳。
long customerid=long.valueof(新stringbuilder(customerid).reverse().tostring());
byte[]rowkey=bytes.add(bytes.tobytes(customerid),bytes.tobytes(txtimestamp.gettime());
如何设计行键,以便将其拆分为4个区域服务器?
有没有有效的行键设计方法?

lskq00tm

lskq00tm1#

你不需要撤销客户id,这是没有意义的
如果要在4个区域中拆分所有数据,可以使用值0-3作为所有键的前缀,例如:

int partition = customer_id % 4;
byte[] rowKey = Bytes.add(
                   Bytes.toBytes(String.valueOf(partition)),
                   Bytes.toBytes(String.valueOf(customer_id)),
                   Bytes.toBytes(txTimestamp.getTime())
                );

在这种情况下,您需要使用hbaseadmin方法创建具有拆分键的表

public void createTable(final HTableDescriptor desc, byte [][] splitKeys)

拆分键将是:

byte[][] splitKeys = new byte[3][];
splitKeys[0] = "1".getBytes();
splitKeys[1] = "2".getBytes();
splitKeys[2] = "3".getBytes();

所以所有以0开头的键转到第一个区域,以1开头的键转到第二个区域,依此类推

相关问题