rowkey 如下:
1900001511504
2900001511504
3900001511504
4900001511504
5900001511504
6900001511504
7900001511504
插入代码如下:
public static void asyncBatchPutTest(Connection connection) throws IOException { BufferedMutator bufferedMutator = null; try { bufferedMutator = connection.getBufferedMutator(TableName.valueOf(TableInformation.TABLE_NAME)); for (int i = 0; i < 100; i++) { int machineId = BaseTypeJudge.reverse(i); String a = BaseTypeJudge.fixBack(machineId + "", 4); String rowKey = a + "00" + Instant.now().getEpochSecond() / 1000; System.out.println(rowKey); Put put = new Put(Bytes.toBytes(Long.parseLong(rowKey))); put.addColumn(Bytes.toBytes(TableInformation.FAMILY_NAME_1), Bytes.toBytes("a"), Bytes.toBytes(i)); bufferedMutator.mutate(put); } } catch (IOException e) { e.printStackTrace(); } finally { if (bufferedMutator != null) bufferedMutator.close(); if (connection != null) connection.close(); } }
分区预创建后,rowkey 不能落入预定的分区,最后发现问题在
Put put = new Put(Bytes.toBytes(Long.parseLong(rowKey)));
把 Long.parseLong(rowKey) 去掉即可
原因可能是创建表的时候,设置分区 startkey 和 endkey 时,用的是 String[],如
String[] keys = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9"};
而创建 Put 的时候,我把 rowkey 转成了 Long
同时,String to byte[] 和 Long to byte[] 结果是不一样的,所以,既然 rowkey 就落不进预定分区了
至于当时为什么想把 rowkey 转成 Long 后,在转 byte,是因为,一串数字 Bytes.toBytes(String) 的 length 是 Bytes.toBytes(Long) 的一倍