1.问题描述

使用put进行添加数据并且指定时间戳时,为什么会失效

1.1初始数据

  1. get 'test:xixi','01'

image.png

1.2指定版本号添加数据

指定比之前版本小的时间戳进行添加数据

  1. public static void putData() throws IOException {
  2. Table table = connection.getTable(TableName.valueOf("test", "xixi"));
  3. Put put = new Put(Bytes.toBytes("01"),1000L);
  4. put.addColumn(Bytes.toBytes("desc"),
  5. Bytes.toBytes("name"),
  6. Bytes.toBytes("w5"))
  7. .setTimestamp(100L);
  8. table.put(put1);
  9. table.close();
  10. connection.close();
  11. }

查询的结果居然是最新的,为什么指定时间戳会失效?
image.png

2.解决

在用Java Api对单个字段设定时间戳时,会存在一个问题
在这里addCloumn()之后设置时间戳,会给下一个字段设置时间戳版本而不是给当前字段设置

  1. Put put = new Put(Bytes.toBytes("01"));
  2. put.addColumn(Bytes.toBytes("desc"),
  3. Bytes.toBytes("name"),
  4. Bytes.toBytes("ls"))
  5. .setTimestamp(1000L)

Debug
image.png
image.png
每次都会创建一个KeyValue,会把Put的成员变量ts传入


所以需要更改成这样

  1. put.setTimestamp(1000L)
  2. .addColumn(Bytes.toBytes("desc"),
  3. Bytes.toBytes("name"),
  4. Bytes.toBytes("ls"))

或者想对本行都生效,可以在构造器中设置

  1. Put put = new Put(Bytes.toBytes("01"),1000L);