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();
        }
    }
  • 相关阅读:
    怎么过滤JSON数组中反斜杠“”,反序列化
    ibatis教学实例
    jQuery给input CheckBox的值查询的一致就选中
    jQuery给CheckBox全选与不全选
    ThinkPHP5.1完全开发手册.CHM离线版下载
    4.2 执行环境及作用域【JavaScript高级程序设计第三版】
    21.1 XMLHttpRequest 对象【JavaScript高级程序设计第三版】
    13.4.3 鼠标与滚轮事件【JavaScript高级程序设计第三版】
    13.6 模拟事件【JavaScript高级程序设计第三版】
    14.5 富文本编辑【JavaScript高级程序设计第三版】
  • 原文地址:https://www.cnblogs.com/zyanrong/p/10924227.html
Copyright © 2011-2022 走看看