IT技術互(hu)動交(jiao)流(liu)平台

运发彩票官网

來源︰IT165收集  發布日期︰2020-02-26 14:42:19

Hbase中提供了(liao)許多(duo)的過(guo)濾器(qi)接(jie)口,以此si)炊允萁jin)行過(guo)濾,使得查詢出(chu)想要的數據。

行過(guo)濾器(qi)

針對行信息進(jin)行過(guo)濾,參數中可以采用前綴匹配(pei)、按位與、或、異或以及(ji)子串匹配(pei)等匹配(pei)的方式。同時可以控制EQUAL、NOT_EQUAL選項(xiang)進(jin)行控制篩選數據的條件(jian)。

/** * 行過(guo)濾器(qi) BinaryComparator  NullComparator:是不是qiang)罩* BitComparator:通過(guo)BitwiseOp類提供的按位與、或、異或操作(zuo)進(jin)行位級別比較 RegexStringComparator:正則匹配(pei) * SubStringComparator:子串是不是包含(han)進(jin)行匹配(pei) */ private static void testRowFilter() { try {  HTable table = new HTable(config, 'testtable');  Scan scan = new Scan();  scan.addColumn('col1'.getBytes(), 'name'.getBytes());  // 行過(guo)濾器(qi)  Filter filter = new RowFilter(CompareOp.EQUAL,   new BinaryComparator('row2'.getBytes()));  scan.setFilter(filter);  ResultScanner result = table.getScanner(scan);  for (Result res : result) {  log.info('行過(guo)濾器(qi)>' + res);  }  // 正則的行過(guo)濾器(qi)  Filter filter2 = new RowFilter(CompareOp.EQUAL,   new RegexStringComparator('.*.2'));  scan.setFilter(filter2);  ResultScanner resultRegx = table.getScanner(scan);  for (Result res : resultRegx) {  log.info('正則>' + res);  }  Filter filterSubString = new RowFilter(CompareOp.EQUAL,   new SubstringComparator('w2'));  scan.setFilter(filterSubString);  ResultScanner resultSubString = table.getScanner(scan);  for (Result res : resultSubString) {  log.info('子串>' + res);  }  table.close(); } catch (IOException e) {  log.error(e); } }

列族過(guo)濾器(qi)

根據列族的數據進(jin)行篩選,形式和上面的行過(guo)濾器(qi)類似,通過(guo)控制相(xiang)應的參數中的篩選的條件(jian)進(jin)行相(xiang)應的篩選。

/** * 列族過(guo)濾器(qi) */ 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) {  log.info(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) {   log.info(res);  }  } else {  log.info('null');  }  table.close(); } catch (IOException e) {  log.error(e); } }

列名過(guo)濾器(qi)

和上面幾個過(guo)濾器(qi)類似,這里是根據列進(jin)行篩選,設置相(xiang)應的條件(jian)後就可以進(jin)行相(xiang)應的篩選了(liao)。

/** * 列名過(guo)濾器(qi) */ 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) {  log.info(res);  }  Get get = new Get('row2'.getBytes());  get.setFilter(filter);  Result resultGet = table.get(get);  log.info(resultGet);  table.close(); } catch (IOException e) {  log.info(e); } }

參考列過(guo)濾器(qi)

參考列過(guo)濾器(qi)根據列族和列限定符進(jin)行篩選,返回與參考列相(xiang)同時間(jian)戳的行的所有鍵(jian)值對。

/** * 參考列過(guo)濾器(qi) */ 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) {  log.info(result);  }  Get get = new Get('row2'.getBytes());  get.setFilter(filter);  Result result = table.get(get);  log.info(result);  table.close(); } catch (IOException e) {  log.error(e); } }

單列過(guo)濾器(qi)

通過(guo)一列的值dao)信pan)斷是不是需(xu)要進(jin)行過(guo)濾。

/** * 單列過(guo)濾器(qi) */ 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) {  log.info(res);  }  Get get = new Get('row2'.getBytes());  get.setFilter(filter);  Result resultGet = table.get(get);  log.info(resultGet);  table.close(); } catch (IOException e) {  log.info(e); } }

前綴過(guo)濾器(qi)

根據前綴進(jin)行匹配(pei)行鍵(jian)的數據,本例中給出(chu)的是以row為前綴的行的數據。

/** * 前綴過(guo)濾器(qi) */ 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) {  log.info('res>' + res);  }  Get get = new Get('row2'.getBytes());  Result resultGet = table.get(get);  log.info('get>' + resultGet);  table.close(); } catch (IOException e) {  log.info(e); } }

分頁過(guo)濾器(qi)

通過(guo)pageFilter設置一頁中數據的條數,注意,在重新設置起(qi)始行的時候,要使得新的行和數據庫(ku)中有區(qu)別,否則,會死(si)循(xun)環(huan)無法停止。

/** * 分頁過(guo)濾器(qi) */ 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的內(na)容一樣(yang)造成(cheng)死(si)循(xun)環(huan)   byte[] startRow = Bytes.add(lastRow, POSTFIX);   scan.setStartRow(startRow);  }  ResultScanner resultScan = table.getScanner(scan);  int localRows = 0;  Result result = resultScan.next();  while (result != null) {   log.info(result);   localRows++;   totalRows++;   lastRow = result.getRow();   result = resultScan.next();  }  if (localRows == 0)   break;  }  log.info(totalRows);  table.close(); } catch (IOException e) {  log.info(e); } }
/** * 列分頁過(guo)濾 */ 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) {  log.info(res);  }  table.close(); } catch (IOException e) {  log.info(e); } }

Skip過(guo)濾器(qi)

與ValueFilter結合(he)使用,如果(guo)一行中某一列不huan)he)要求的話直接(jie)被過(guo)濾掉。

/** * 跳過(guo)過(guo)濾器(qi) */ 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) {  log.info('<' + result);  }  Filter filter = new SkipFilter(filt);  Scan scan = new Scan();  scan.setFilter(filter);  ResultScanner result = table.getScanner(scan);  for (Result res : result) {  log.info('>' + res);  }  table.close(); } catch (IOException e) {  log.info(e); } }

全(quan)匹配(pei)過(guo)濾器(qi)

在遇到(dao)某個條件(jian)之前的數據全(quan)部查詢出(chu)來,直到(dao)遇到(dao)滿足該條件(jian)的數據之後結束查詢。

 /** * 全(quan)匹配(pei)過(guo)濾器(qi) */ 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) {  log.info('>' + res);  }  Filter filter = new WhileMatchFilter(filt);  scan.setFilter(filter);  ResultScanner resultScan = table.getScanner(scan);  for (Result res : resultScan) {  log.info('<' + res);  }  table.close(); } catch (IOException e) {  log.info(e); } }

過(guo)濾器(qi)組合(he)

可以將上面的過(guo)個過(guo)濾器(qi)放在一個List中,然(ran)後形成(cheng)多(duo)個過(guo)濾器(qi)的組合(he)xi)男問澆jin)行過(guo)濾。

 /** * 過(guo)濾器(qi)組合(he) */ 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) {  log.info(res);  }  table.close(); } catch (IOException e) {  log.info(e); } }

轉(zhuan)載注明出(chu)處︰http://blog.csdn.net/wangyang1354/article/details/53761559

Tag標(biao)簽︰蔥嶺  過(guo)濾器(qi)  
  • 运发彩票官网

About IT165 - 廣告服務(wu) - 隱私(si)聲明 - 版權申明 - 免責條款 - 河北快3官网網站(zhan)地圖 - 網友投稿 - 聯(lian)系(xi)方式
本站(zhan)內(na)容來自(zi)于(yu)互(hu)聯(lian)網,僅(jin)供用于(yu)網絡技術學習(xi),學習(xi)中請遵循(xun)相(xiang)關(guan)法律法規
运发彩票官网 | 下一页