Skip to content

Instantly share code, notes, and snippets.

@jimfulton
Last active June 30, 2021 19:16
Show Gist options
  • Save jimfulton/69cca1687cc7bb043c28382c79c786db to your computer and use it in GitHub Desktop.
Save jimfulton/69cca1687cc7bb043c28382c79c786db to your computer and use it in GitHub Desktop.
Update BigTable with reverse timestamps, https://github.com/googleapis/google-cloud-go/issues/4087
package dev.j1m;
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.nio.ByteBuffer;
public class Main {
public static final long REVERSE_EPOCH = Long.MAX_VALUE / 1000L;
private static long reverseTimestamp(long modTs) {
assert modTs >= 0;
return REVERSE_EPOCH - modTs;
}
private static void doReversedTimestampPut(Table table) throws IOException {
byte[] key = "key".getBytes();
long timestamp = System.currentTimeMillis();
Put put = new Put(key, System.currentTimeMillis());
put = put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("ts"), reverseTimestamp(timestamp), Bytes.toBytes(timestamp));
table.put(put);
}
public static void main(String[] args) throws IOException {
Connection connection = BigtableConfiguration.connect("ppp", "iii");
Table table = connection.getTable(TableName.valueOf("mytable"));
doReversedTimestampPut(table);
String rowKey = "key";
Result result = table.get(new Get(Bytes.toBytes(rowKey)));
Cell cell = result.getColumnLatestCell("f".getBytes(), "ts".getBytes());
System.out.println(ByteBuffer.wrap(cell.getValueArray()).getLong());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment