☆☆☆★☆☆☆

唯有努力才能活成自己想要活成的样子

导航

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();
    }
}

 

posted on 2019-05-25 22:23  Yr-Zhang  阅读(1077)  评论(0编辑  收藏  举报