Hbase葱岭探索与发现 过虑器Api
摘要: Hbase中出示了很多的过虑器插口,为此来多数据开展过虑,促使查寻出要想的数据信息。对于行信息内容开展过虑,主要参数中能够选用作为前缀配对、按位与、或、异或及其子串配对...
Hbase中出示了很多的过虑器插口,为此来多数据开展过虑,促使查寻出要想的数据信息。
对于行信息内容开展过虑,主要参数中能够选用作为前缀配对、按位与、或、异或及其子串配对等配对的方法。同时能够操纵EQUAL、NOT_EQUAL选择项开展操纵挑选数据信息的标准。
* 行过虑器 BinaryComparator NullComparator:不是是空值 * BitComparator:根据BitwiseOp类出示的按位与、或、异或实际操作开展位级別较为 RegexStringComparator:正则表达式配对 * SubStringComparator:子串不是是包括开展配对 private static void testRowFilter() { try { HTable table = new HTable(config, testtable Scan scan = new Scan(); scan.addColumn( col1 .getBytes(), name .getBytes()); // 行过虑器 Filter filter = new RowFilter(CompareOp.EQUAL, new BinaryComparator( row2 .getBytes())); scan.setFilter(filter); ResultScanner result = table.getScanner(scan); for (Result res : result) { ( 行过虑器 + res); // 正则表达式的行过虑器 Filter filter2 = new RowFilter(CompareOp.EQUAL, new RegexStringComparator( .*.2 scan.setFilter(filter2); ResultScanner resultRegx = table.getScanner(scan); for (Result res : resultRegx) { ( 正则表达式 + res); Filter filterSubString = new RowFilter(CompareOp.EQUAL, new SubstringComparator( w2 scan.setFilter(filterSubString); ResultScanner resultSubString = table.getScanner(scan); for (Result res : resultSubString) { ( 子串 + res); table.close(); } catch (IOException e) { log.error(e); }列族过虑器
依据列族的数据信息开展挑选,方式和上边的行过虑器相近,根据操纵相对的主要参数中的挑选的标准开展相对的挑选。
* 列族过虑器 private static void testFamlyFilter() { try { HTable table = new HTable(config, testtable Filter filter = new FamilyFilter(CompareOp.EQUAL, new BinaryComparator( col1 .getBytes())); Scan scan = new Scan( row2 .getBytes(), filter); ResultScanner result = table.getScanner(scan); for (Result res : result) { (res); Filter filterNull = new FamilyFilter(CompareOp.EQUAL, new RegexStringComparator( .*.1 Scan scanNull = new Scan( row2 .getBytes(), filterNull); scanNull.addFamily( col1 .getBytes()); ResultScanner resultNull = table.getScanner(scanNull); if (resultNull != null) { for (Result res : resultNull) { (res); } else { ( null table.close(); } catch (IOException e) { log.error(e); }列名过虑器
和上边好多个过虑器相近,这儿是依据列开展挑选,设定相对的标准后便可以开展相对的挑选了。
* 列名过虑器 public static void testColumFilter() { try { HTable table = new HTable(config, testtable Filter filter = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator( name .getBytes())); Scan scan = new Scan( row2 .getBytes(), filter); ResultScanner result = table.getScanner(scan); for (Result res : result) { (res); Get get = new Get( row2 .getBytes()); get.setFilter(filter); Result resultGet = table.get(get); (resultGet); table.close(); } catch (IOException e) { (e);参照列过虑器
参照列过虑器依据列族和列限制符开展挑选,回到与参照列同样時间戳的行的全部键值对。
* 参照列过虑器 public static void testDependentColumnFilter() { try { HTable table = new HTable(config, testtable Filter filter = new DependentColumnFilter( col1 .getBytes(), name .getBytes(), false); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner resu = table.getScanner(scan); for (Result result : resu) { (result); Get get = new Get( row2 .getBytes()); get.setFilter(filter); Result result = table.get(get); (result); table.close(); } catch (IOException e) { log.error(e); }单列过虑器
根据一列的值开展分辨不是是必须开展过虑。
* 单列过虑器 public static void testSingleColumnValueFilter() { try { HTable table = new HTable(config, testtable Filter filter = new SingleColumnValueFilter( col1 .getBytes(), name .getBytes(), CompareOp.EQUAL, wy .getBytes()); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner result = table.getScanner(scan); for (Result res : result) { (res); Get get = new Get( row2 .getBytes()); get.setFilter(filter); Result resultGet = table.get(get); (resultGet); table.close(); } catch (IOException e) { (e); }作为前缀过虑器
依据作为前缀开展配对行键的数据信息,本例中得出的是以row为作为前缀的行的数据信息。
* 作为前缀过虑器 public static void testPrefixFilter() { try { HTable table = new HTable(config, testtable Filter filter = new PrefixFilter( row .getBytes()); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner result = table.getScanner(scan); for (Result res : result) { ( res + res); Get get = new Get( row2 .getBytes()); Result resultGet = table.get(get); ( get + resultGet); table.close(); } catch (IOException e) { (e); }分页查询过虑器
根据pageFilter设定一页中数据信息的总数,留意,在再次设定起止行的情况下,要促使新的行和数据信息库文件有差别,不然,会死循环系统没法终止。
* 分页查询过虑器 public static void testPageFilter() { try { HTable table = new HTable(config, testtable Filter filter = new PageFilter(10); int totalRows = 0; byte[] lastRow = null; Scan scan = new Scan(); while (true) { scan.setFilter(filter); if (lastRow != null) { // 再加0后表明新的刚开始避免row的內容一样导致死循环系统 byte[] startRow = Bytes.add(lastRow, POSTFIX); scan.setStartRow(startRow); ResultScanner resultScan = table.getScanner(scan); int localRows = 0; Result result = resultScan.next(); while (result != null) { (result); localRows++; totalRows++; lastRow = result.getRow(); result = resultScan.next(); if (localRows == 0) break; (totalRows); table.close(); } catch (IOException e) { (e);
* 列分页查询过虑 public static void testColumnPaginationFilter() { try { HTable table = new HTable(config, testtable Filter filter = new ColumnPaginationFilter(5, 10); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner result = table.getScanner(scan); for (Result res : result) { (res); table.close(); } catch (IOException e) { (e); }Skip过虑器
与ValueFilter融合应用,假如一行中某一列不符合合规定得话立即被过虑掉。
* 绕过过虑器 public static void testSkipFilter() { try { HTable table = new HTable(config, testtable Filter filt = new ValueFilter(CompareOp.NOT_EQUAL, new BinaryComparator( v .getBytes())); Scan scanValue = new Scan(); scanValue.setFilter(filt); ResultScanner ress = table.getScanner(scanValue); for (Result result : ress) { ( + result); Filter filter = new SkipFilter(filt); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner result = table.getScanner(scan); for (Result res : result) { ( + res); table.close(); } catch (IOException e) { (e); }全配对过虑器
在碰到某一标准以前的数据信息所有查寻出去,直至碰到考虑此条件的数据信息以后完毕查寻。
* 全配对过虑器 public static void testWhileMatch() { try { HTable table = new HTable(config, testtable Filter filt = new RowFilter(CompareOp.NOT_EQUAL, new BinaryComparator( row6 .getBytes())); Scan scan = new Scan(); scan.setFilter(filt); ResultScanner results = table.getScanner(scan); for (Result res : results) { ( + res); Filter filter = new WhileMatchFilter(filt); scan.setFilter(filter); ResultScanner resultScan = table.getScanner(scan); for (Result res : resultScan) { ( + res); table.close(); } catch (IOException e) { (e);过虑器组成
能够将上边的活才过虑器放到一个List中,随后产生好几个过虑器的组成的方式开展过虑。
* 过虑器组成 public static void testFilterList() { List Filter filterList = new ArrayList Filter Filter filter1 = new SingleColumnValueFilter( col1 .getBytes(), name .getBytes(), CompareOp.EQUAL, x .getBytes()); filterList.add(filter1); Filter filter2 = new RowFilter(CompareOp.NOT_EQUAL, new BinaryComparator( row2 .getBytes())); filterList.add(filter2); FilterList filters = new FilterList(filterList); Scan scan = new Scan(); scan.setFilter(filters); try { HTable table = new HTable(config, testtable ResultScanner result = table.getScanner(scan); for (Result res : result) { (res); table.close(); } catch (IOException e) { (e);
转截标明出處:wangyang1354/article/details/