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包如下: