import org.apache.hadoop.hbase.util.Bytes;
//填入station_id和时间戳,返回逆序时间戳和station_id生成的rowkey
public class RowKeyConverter {
// station_id长度
private static final int STATION_ID_LENGTH = 12;
/**
* @return A row key whose format is: <station_id> <reverse_order_timestamp>
*/
public static byte[] makeObservationRowKey(String stationId,
long observationTime) {
byte[] row = new byte[STATION_ID_LENGTH + Bytes.SIZEOF_LONG];
Bytes.putBytes(row, 0, Bytes.toBytes(stationId), 0, STATION_ID_LENGTH);
long reverseOrderTimestamp = Long.MAX_VALUE - observationTime;
//将reverseOrderTimestamp转换成byte放入row的STATION_ID_LENGTH位置后
Bytes.putLong(row, STATION_ID_LENGTH, reverseOrderTimestamp);
return row;
}
public static void main(String[] args) {
for(int i=100;i>0;i--) {
long time=System.currentTimeMillis();
byte[] rowKey=makeObservationRowKey("123412341234",time);
for (byte b : rowKey) {
System.out.print(b);
}
System.out.println();
}
}
}
rowkey倒置时间戳
最新推荐文章于 2023-05-08 15:23:48 发布