zoukankan      html  css  js  c++  java
  • hbase的API并且使用多个rowkey分段直接读取数据

    import com.hopechart.dataquery.Decoder;
    import com.hopechart.dataquery.RowKeyRuler;
    import com.hopechart.type.TGPSV2;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.CellUtil;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.HConstants;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Result;
    import org.apache.hadoop.hbase.client.ResultScanner;
    import org.apache.hadoop.hbase.client.Scan;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    public class Demo {
        public static void main(String[] args) throws Exception {
            Configuration config = HBaseConfiguration.create();
            config.set("hbase.client.ipc.pool.size", "3");
            //config.set("hbase.zookeeper.quorum", "mine10");  //服务地址 zk
            //config.set("hbase.zookeeper.property.clientPort", "2181"); //端口号
            config.setLong(HConstants.HBASE_REGIONSERVER_LEASE_PERIOD_KEY, 120000); //设置连接时长
            String recordDate ="20190205";
            String sDt = recordDate+" 00:00:00";
            String eDt = recordDate+" 23:59:59";
            SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd HH:mm:ss");
            Date dt1 = sdf.parse(sDt);
            Date dt2 = sdf.parse(eDt);
            long lTime = dt1.getTime() / 1000;
            long bTime = dt2.getTime() / 1000;
            HTable ht = new HTable(config, "t_gps_sq_"+recordDate);
            //扫描表
            List<Long> list = new ArrayList();
            list.add(1708040960L);
            list.add(1710050744L);
            Scan scan = new Scan();
            for (long v:list){
                long mdataId =v;
                byte[] startRowKey = RowKeyRuler.getRowKey(mdataId, lTime);
                byte[] endRowKey = RowKeyRuler.getRowKey(mdataId, bTime);
                scan.setStartRow(startRowKey);
                scan.setStopRow(endRowKey);
                scan.setBatch(10);
                ResultScanner rs = ht.getScanner(scan);
                for(Result result : rs) {
                    for (Cell cell : result.rawCells()) {
                        byte[] cloneRow = CellUtil.cloneRow(cell);
                        long dataid = Decoder.byte4ToLongBig(cloneRow, 2);
                        byte[] cloneValue = CellUtil.cloneValue(cell);
                        TGPSV2 decode = (TGPSV2) Decoder.decode(1, cloneRow, cloneValue, 0);
                        long dataTime = decode.getDataTime();
                        System.out.println(dataid + " " + dataTime);
                    }
                }
            }
            ht.close();
        }
    }
  • 相关阅读:
    Ranorex发布2.3版本支持Flex4
    TestComplete基础教程
    2009年缺陷跟踪和测试管理工具使用情况调查报告
    软件自动化测试资源列表
    TestComplete资源列表
    分治算法
    画表格蓝桥杯
    分红酒蓝桥杯
    “硬币方案”蓝桥杯
    微生物增值蓝桥杯
  • 原文地址:https://www.cnblogs.com/zyanrong/p/10924227.html
Copyright © 2011-2022 走看看