java时间戳正序和倒序

时间戳倒序   Long.toString(Long.MAX_VALUE - System.currentTimeMillis())
时间戳正序  System.currentTimeMillis()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个按照手机号-对方号码-时间戳方式时间倒序查询 HBase 表中话费清单的示例代码: ```java import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.PrefixFilter; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.filter.CompareFilter; import org.apache.hadoop.hbase.filter.BinaryComparator; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.util.Bytes; public class HBaseDemo { public static void main(String[] args) throws IOException { Configuration config = new Configuration(); config.set("hbase.zookeeper.quorum", "localhost"); // HBase ZooKeeper 地址 Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf("phone_bills")); // 话费清单表名 String phoneNum = "13800138000"; // 手机号码 byte[] startRow = Bytes.toBytes(phoneNum); // 起始行键 byte[] stopRow = Bytes.toBytes(phoneNum + "z"); // 终止行键 // 创建过滤器列表 FilterList filters = new FilterList(FilterList.Operator.MUST_PASS_ALL); // 添加前缀过滤器 Filter prefixFilter = new PrefixFilter(startRow); filters.addFilter(prefixFilter); // 添加列值过滤器 byte[] family = Bytes.toBytes("cf"); // 列族 byte[] qualifier1 = Bytes.toBytes("callee"); // 对方号码列名 byte[] qualifier2 = Bytes.toBytes("timestamp"); // 时间戳列名 byte[] value = Bytes.toBytes("139"); // 以 139 开头的号码 SingleColumnValueFilter valueFilter = new SingleColumnValueFilter( family, qualifier1, CompareFilter.CompareOp.EQUAL, value); filters.addFilter(valueFilter); // 创建扫描器 Scan scan = new Scan(); scan.setFilter(filters); scan.setStartRow(startRow); scan.setStopRow(stopRow); scan.addFamily(family); scan.addColumn(family, qualifier1); scan.addColumn(family, qualifier2); scan.setReversed(true); // 按时间倒序排列 // 执行扫描 ResultScanner scanner = table.getScanner(scan); // 遍历结果 List<String> result = new ArrayList<String>(); for (Result r : scanner) { String rowKey = Bytes.toString(r.getRow()); String callee = Bytes.toString(r.getValue(family, qualifier1)); String timestamp = Bytes.toString(r.getValue(family, qualifier2)); result.add(rowKey + "\t" + callee + "\t" + timestamp); } scanner.close(); // 反转结果列表 Collections.reverse(result); // 输出结果 for (String s : result) { System.out.println(s); } table.close(); connection.close(); } } ``` 需要注意的是,以上代码仅供参考,具体实现方式可能需要根据你的表结构和查询需求进行适当调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值