如果你在Hbase表中存在一些特征相同的记录,实际上就可以通过设置Filter的方式进行检索过滤。
比如这个特征是前缀相同。
ROW COLUMN+CELL 003_8618722111005_460014004369494 column=info:sex, timestamp=1318853623294, value=male
Jerry1 column=info:sex, timestamp=1318917602868, value=male 003_8618722111005_460014004569494 column=info:sex, timestamp=1318917789851, value=male
可以发现这三条记录都是以Jerry开头的row key.
那么我们如何检索呢。很简单。
如果是在hbase shell中
scan ‘scores’, {FILTER => org.apache.Hadoop.hbase.filter.PrefixFilter.new(org.apache.hadoop.hbase.util.Bytes.toBytes(‘003_8618722111005_’))}
或者
scan 'MsisdnImsi',{STARTROW=>"003_8618722111005_460014004369494",STOPROW=>"003_8618722111005_9"}
如果是Java 客户端
[java] view plain copy
- <pre name="code" class="java">public void scaneByPrefixFilter(String tablename, String rowPrifix) {
- try {
- HTable table = new HTable(conf, tablename);
- Scan s = new Scan();
- s.setFilter(new PrefixFilter(rowPrifix.getBytes()));
- ResultScanner rs = table.getScanner(s);
- for (Result r : rs) {
- KeyValue[] kv = r.raw();
- for (int i = 0; i < kv.length; i++) {
- System.out.print(new String(kv[i].getRow()) + " ");
- System.out.print(new String(kv[i].getFamily()) + ":");
- System.out.print(new String(kv[i].getQualifier()) + " ");
- System.out.print(kv[i].getTimestamp() + " ");
- System.out.println(new String(kv[i].getValue()));
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
[java] view plain copy
- 或者:
[java] view plain copy
- <pre name="code" class="java">public static void getInfo(String str) {
- /根据分区规则 取模
- long mod = Long.valueOf(str) % DataType.getAsInt(6);
- String tmp = "";
- if (mod > 9)
- {
- tmp = "0" + mod;
- }
- else
- {
- tmp = "00" + mod;
- }
- //参考scan 'MsisdnImsi',{STARTROW=>"003_8618722111005_460014004369494",STOPROW=>"003_8618722111005_9"}</span>
- String startRow = tmp + "_" + str;
- String stopRow = tmp + "_" + str + "_9";
- logger.info("---------------------- startRow : "+startRow+" ---------------------");
- logger.info("---------------------- stopRow : "+stopRow+" ---------------------");
- ResultScanner scanner = null;
- try
- {
- // 从表池中取出HBASE表对象
- HTableInterface table = getTable("testtable");
- byte[] family = Bytes.toBytes("F");
- byte[] column = Bytes.toBytes("A");
- // 获取筛选对象
- Scan scan = new Scan();
- scan.setStartRow(getBytes(startRow));
- scan.setStopRow(getBytes(stopRow));
- scan.addColumn(family, column);
- // 给筛选对象放入过滤器(true标识分页,具体方法在下面)
- scan.setCacheBlocks(false);
- scanner = table.getScanner(scan);
- // 遍历扫描器对象, 并将需要查询出来的数据row key取出
- for (Result result : scanner) {
- for (KeyValue kv : result.list()) {
- String key = Bytes.toStringBinary(kv.getRow());
- String value = HbaseVO.deserialize(kv.getValue()).insertTime;
- //do something
- }
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- logger.error("查询hbase异常! ",e);
- }
- finally
- {
- closeScanner(scanner);
- }
- }