Last active
June 30, 2021 19:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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