首页 > 编程语言 > 详细

用java查询HBase中某表的一批数据

时间:2015-12-08 21:57:50      阅读:325      评论:0      收藏:0      [点我收藏+]

java代码如下:

package db.query;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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 org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;


public class HBaseQuery {
    public static void main(String[] args) {
        Configuration conf = HBaseConfiguration.create();
            conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "192.168.1.154");
            conf.set("hbase.zookeeper.property.clientPort", "2181");
            conf.set("hbase.master", "192.168.1.154:6000");
            String tableName = "car_table";
            HTable table;
        try {
            table = new HTable(conf, tableName);
            //设置查询条件
            //使用前缀过滤器
            Filter filter = new PrefixFilter(Bytes.toBytes("144860945858310137-"));
            Scan scan = new Scan();
            scan.setFilter(filter);
//            scan.setStartRow(Bytes.toBytes("144860945858310137-0000000000000"));
//            scan.setStopRow(Bytes.toBytes("144860945858310137-9999999999999"));
            scan.addFamily(Bytes.toBytes("lte"));
            scan.addColumn(Bytes.toBytes("lte"), Bytes.toBytes("cid"));
            scan.addColumn(Bytes.toBytes("lte"), Bytes.toBytes("time"));
            scan.addColumn(Bytes.toBytes("lte"), Bytes.toBytes("pci"));
            scan.addColumn(Bytes.toBytes("lte"), Bytes.toBytes("st"));
            scan.addColumn(Bytes.toBytes("lte"), Bytes.toBytes("ed"));
            scan.addColumn(Bytes.toBytes("lte"), Bytes.toBytes("ta"));
            scan.addColumn(Bytes.toBytes("lte"), Bytes.toBytes("lat"));
            scan.addColumn(Bytes.toBytes("lte"), Bytes.toBytes("lng"));
            ResultScanner results = table.getScanner(scan);
            for(Result result: results){
                String rowkey =  Bytes.toString(result.getRow());
                String cid = Bytes.toString(result.getValue(Bytes.toBytes("lte"), Bytes.toBytes("cid")));
                String time = Bytes.toString(result.getValue(Bytes.toBytes("lte"), Bytes.toBytes("time")));                
                String pci = Bytes.toString(result.getValue(Bytes.toBytes("lte"), Bytes.toBytes("pci")));
                String st = Bytes.toString(result.getValue(Bytes.toBytes("lte"), Bytes.toBytes("st")));    
                String ed = Bytes.toString(result.getValue(Bytes.toBytes("lte"), Bytes.toBytes("ed")));    
                String ta = Bytes.toString(result.getValue(Bytes.toBytes("lte"), Bytes.toBytes("ta")));
                String lat = Bytes.toString(result.getValue(Bytes.toBytes("lte"), Bytes.toBytes("lat")));
                String lng = Bytes.toString(result.getValue(Bytes.toBytes("lte"), Bytes.toBytes("lng")));                    
                System.out.println("rowkey : "+rowkey+" cid : "+cid+", time: "+time+", pci: "+pci+", st: "+st+", ed: "+ed+", ta: "+ta+", lat: "+lat+", lon: "+lng);
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

所需jar包如下:

技术分享

用java查询HBase中某表的一批数据

原文:http://www.cnblogs.com/gaopeng527/p/5030835.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!