filter ==> SQL 中的Wherejava
filter的執行流程:正則表達式
過濾器在客戶端建立,而後經過RPC發送到服務器上,由服務器執行apache
Comparator 服務器 |
Description session |
LongComparatordom |
Assumes the given value array is a Java Long number and uses Bytes.toLong() to convert it. 函數 |
BinaryComparatoroop |
Uses Bytes.compareTo() to compare 當前值與閥值spa |
BinaryPrefixComparator設計 |
Bytes.compareTo() 進行匹配,可是從左端開始前綴匹配 |
NullComparator |
判斷當前值是否爲null |
BitComparator |
Performs a bitwise comparison, providing a BitwiseOp enumeration with AND, OR, and XOR operators. |
RegexStringComparator |
正則表達式匹配 |
SubstringComparator |
子字符串比對 |
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * 基於行鍵上的過濾器 */ public class FilterInHbase { public static void main(String[] args) throws IOException{ Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); //創建user表的鏈接 Table table =connection.getTable(TableName.valueOf("user")); Scan scan=new Scan(); //掃描列族info 列age scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age")); System.out.println("行過濾器"); //比較過濾器 //這兒是指找出行小於或者等於"510824118261011172"的全部行 Filter filter1 = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("813782218261011172"))); //添加過濾器到掃描器中 scan.setFilter(filter1); ResultScanner scanner1 = table.getScanner(scan); for(Result res:scanner1){ System.out.println(res); } scanner1.close(); System.out.println("正則過濾器"); //正則過濾器 //過濾行鍵以2結束的 Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator(".*2$") ); scan.setFilter(filter2); ResultScanner scanner2 = table.getScanner(scan); for (Result res:scanner2){ System.out.println(res); } scanner2.close(); //子串過濾器 //過濾行鍵中包含了"61826"這個字符串 System.out.println("子串過濾器"); Scan scan3=new Scan(); //掃描列族info 列age scan3.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age")); Filter filter3=new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("61826") ); scan3.setFilter(filter3); ResultScanner scanner3=table.getScanner(scan3); for(Result res:scanner3){ System.out.println(res); } scanner3.close(); table.close(); connection.close(); } } /** Result: 行過濾器 < 813782218261011172 keyvalues={224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0} keyvalues={510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0} keyvalues={524382618264914241/info:age/1472196193913/Put/vlen=2/seqid=0} keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0} keyvalues={813782218261011172/info:age/1472196212550/Put/vlen=2/seqid=0} 正則過濾器 已2結尾的行鍵 keyvalues={510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0} keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0} keyvalues={813782218261011172/info:age/1472196212550/Put/vlen=2/seqid=0} 子串過濾器 包含了"61826"的行鍵 keyvalues={224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0} keyvalues={524382618264914241/info:age/1472196193913/Put/vlen=2/seqid=0} keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0} **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.BinaryComparator; import org.apache.hadoop.hbase.filter.CompareFilter; import org.apache.hadoop.hbase.filter.FamilyFilter; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * 列族過濾器 * 比較列族來返回結果 * 用戶能夠在列族一級篩選所需數據 */ public class FamilyFilterInHbase { public static void main(String[] args) throws IOException { Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); //創建表的鏈接 Table table = connection.getTable(TableName.valueOf("user")); //比較過濾器 如今表有2個列族 info ship 固然實際有可能不少哦 //取info < "kiss" <ship < "wings" Filter filter1 = new FamilyFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("kiss"))); Scan scan = new Scan(); scan.setFilter(filter1); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner){ System.out.println(result); } scanner.close(); Get get1 = new Get(Bytes.toBytes("673782618261019142")); get1.setFilter(filter1); Result result1=table.get(get1); System.out.println("Result of get1(): " + result1); //添加列族過濾器 info Filter filter2= new FamilyFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("info"))); //獲取一行數據 Get get2 = new Get(Bytes.toBytes("673782618261019142")); //可是get列族ship 那麼==>> Result of get():keyvalues=NONE [自己衝突 因此無數據] //若是get列族info 那麼==>> Result of get2():keyvalues={673782618261019142/... get2.addFamily(Bytes.toBytes("ship")); get2.setFilter(filter2); Result result2 =table.get(get2); System.out.println("Result of get2():"+result2); scanner.close(); table.close(); connection.close(); } } /** LESS "kiss" keyvalues={224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, 224382618261914241/info:phone/1472196211427/Put/vlen=11/seqid=0, 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0} keyvalues={510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0, 510824118261011172/info:height/1472196213056/Put/vlen=3/seqid=0, 510824118261011172/info:name/1472196212942/Put/vlen=8/seqid=0, 510824118261011172/info:phone/1472196213237/Put/vlen=11/seqid=0, 510824118261011172/info:weight/1472196213169/Put/vlen=3/seqid=0} keyvalues={524382618264914241/info:age/1472196193913/Put/vlen=2/seqid=0, 524382618264914241/info:height/1472196194783/Put/vlen=3/seqid=0, 524382618264914241/info:name/1472196193255/Put/vlen=8/seqid=0, 524382618264914241/info:phone/1472196195125/Put/vlen=11/seqid=0, 524382618264914241/info:weight/1472196194970/Put/vlen=3/seqid=0} keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0, 673782618261019142/info:height/1472196211761/Put/vlen=3/seqid=0, 673782618261019142/info:name/1472196211678/Put/vlen=7/seqid=0, 673782618261019142/info:phone/1472196211956/Put/vlen=11/seqid=0, 673782618261019142/info:weight/1472196211841/Put/vlen=3/seqid=0} keyvalues={813782218261011172/info:age/1472196212550/Put/vlen=2/seqid=0, 813782218261011172/info:height/1472196212605/Put/vlen=3/seqid=0, 813782218261011172/info:name/1472196212480/Put/vlen=8/seqid=0, 813782218261011172/info:phone/1472196212713/Put/vlen=11/seqid=0, 813782218261011172/info:weight/1472196212651/Put/vlen=3/seqid=0} GREATER "kiss" keyvalues={224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, 224382618261914241/ship:salary/1472196211594/Put/vlen=4/seqid=0} keyvalues={510824118261011172/ship:addr/1472196213328/Put/vlen=8/seqid=0, 510824118261011172/ship:email/1472196213422/Put/vlen=12/seqid=0, 510824118261011172/ship:salary/1472196214963/Put/vlen=5/seqid=0} keyvalues={524382618264914241/ship:addr/1472196195270/Put/vlen=7/seqid=0, 524382618264914241/ship:email/1472196195371/Put/vlen=13/seqid=0, 524382618264914241/ship:salary/1472196195485/Put/vlen=4/seqid=0} keyvalues={673782618261019142/ship:addr/1472196212059/Put/vlen=8/seqid=0, 673782618261019142/ship:email/1472196212176/Put/vlen=12/seqid=0, 673782618261019142/ship:salary/1472196212284/Put/vlen=4/seqid=0} keyvalues={813782218261011172/ship:addr/1472196212762/Put/vlen=4/seqid=0, 813782218261011172/ship:email/1472196212802/Put/vlen=12/seqid=0, 813782218261011172/ship:salary/1472196212840/Put/vlen=5/seqid=0} APPEND(LESS "kiss") Result of get(): keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0, 673782618261019142/info:height/1472196211761/Put/vlen=3/seqid=0, 673782618261019142/info:name/1472196211678/Put/vlen=7/seqid=0, 673782618261019142/info:phone/1472196211956/Put/vlen=11/seqid=0, 673782618261019142/info:weight/1472196211841/Put/vlen=3/seqid=0} APPEND(GREATER "kiss") Result of get1(): keyvalues={673782618261019142/ship:addr/1472196212059/Put/vlen=8/seqid=0, 673782618261019142/ship:email/1472196212176/Put/vlen=12/seqid=0, 673782618261019142/ship:salary/1472196212284/Put/vlen=4/seqid=0} //filter "info" get "ship" Result of get():keyvalues=NONE //filter "info" get "info" Result of get2():keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0, 673782618261019142/info:height/1472196211761/Put/vlen=3/seqid=0, 673782618261019142/info:name/1472196211678/Put/vlen=7/seqid=0, 673782618261019142/info:phone/1472196211956/Put/vlen=11/seqid=0, 673782618261019142/info:weight/1472196211841/Put/vlen=3/seqid=0} **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * 值過濾器 * 根據值進行篩選 能夠聯合RegexStringComparator 進行設計 */ public class FilterOfValue { public static void main(String[] args) throws IOException { Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); //創建表的鏈接 Table table = connection.getTable(TableName.valueOf("user")); //值中包含了177的過濾器 Filter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("1771392142") ); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner){ for (Cell cell:result.rawCells()){ System.out.println("Cell: "+cell+", Value: "+Bytes.toString(cell.getValueArray(),cell.getValueLength())); } } scanner.close(); Get get1 = new Get(Bytes.toBytes("673782618261019142")); get1.setFilter(filter); Result result1=table.get(get1); for (Cell cell : result1.rawCells()) { System.out.println("Get1 Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } Get get2 = new Get(Bytes.toBytes("813782218261011172")); get2.setFilter(filter); Result result2=table.get(get2); for (Cell cell : result2.rawCells()) { System.out.println("Get2 Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } table.close(); connection.close(); } } /** 原數據: 673782618261019142 column=info:phone, timestamp=1472196211956, value=17713921424 813782218261011172 column=info:phone, timestamp=1472196212713, value=12713921424 *輸出結果: Cell: 673782618261019142/info:phone/1472196211956/Put/vlen=11/seqid=0, Value: 73782618261019142infophoneVŻt�17713921424 Get1 Cell: 673782618261019142/info:phone/1472196211956/Put/vlen=11/seqid=0, Value: 17713921424 **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * 參考列過濾器 * 根據列名進行篩選 */ public class FilterOfDependentColumnFilter { private static Table table=null; public static Table getTable() { if(table==null){ try { Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); //創建表的鏈接 return connection.getTable(TableName.valueOf("user")); }catch (IOException e){ return table; } } return table; } public static void filter(boolean drop,CompareFilter.CompareOp oper,ByteArrayComparable comparable) throws IOException { Filter filter; if (comparable != null) { filter = new DependentColumnFilter(Bytes.toBytes("info"), Bytes.toBytes("phone"), drop, oper, comparable); } else { filter = new DependentColumnFilter(Bytes.toBytes("info"), Bytes.toBytes("phone"), drop); } Scan scan = new Scan(); scan.setFilter(filter); ResultScanner scanner = getTable().getScanner(scan); for (Result result : scanner) { for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } scanner.close(); Get get = new Get(Bytes.toBytes("673782618261019142")); get.setFilter(filter); Result result = getTable().get(get); for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } public static void main(String[] args) throws IOException { filter(true, CompareFilter.CompareOp.NO_OP, null); filter(false, CompareFilter.CompareOp.NO_OP, null); filter(true, CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes("17713921424"))); filter(false, CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes("17713921424"))); filter(true, CompareFilter.CompareOp.EQUAL, new RegexStringComparator(".*\\.5")); filter(false, CompareFilter.CompareOp.EQUAL, new RegexStringComparator(".*\\.5")); } } /** **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * 前綴過濾器 * 根據行鍵的前綴進行過濾 */ public class FilterOfPrefixFilter { public static void main(String[] args) throws IOException { Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); //創建表的鏈接 Table table = connection.getTable(TableName.valueOf("user")); Filter filter = new PrefixFilter(Bytes.toBytes("510824")); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } scanner.close(); Get get = new Get(Bytes.toBytes("row-5")); get.setFilter(filter); Result result = table.get(get); for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } scanner.close(); table.close(); connection.close(); } } /** Cell: 510824118261011172/info:height/1472196213056/Put/vlen=3/seqid=0, Value: 188 Cell: 510824118261011172/info:name/1472196212942/Put/vlen=8/seqid=0, Value: yangyang Cell: 510824118261011172/info:phone/1472196213237/Put/vlen=11/seqid=0, Value: 18013921626 Cell: 510824118261011172/info:weight/1472196213169/Put/vlen=3/seqid=0, Value: 138 Cell: 510824118261011172/ship:addr/1472196213328/Put/vlen=8/seqid=0, Value: shanghai Cell: 510824118261011172/ship:email/1472196213422/Put/vlen=12/seqid=0, Value: 199@sina.com Cell: 510824118261011172/ship:salary/1472196214963/Put/vlen=5/seqid=0, Value: 50000 **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.PageFilter; import org.apache.hadoop.hbase.filter.PrefixFilter; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * 分頁過濾器 */ public class FilterOfPage { private static final byte[] POSTFIX = new byte[] { 0x00 }; public static void main(String[] args) throws IOException { Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); //創建表的鏈接 Table table = connection.getTable(TableName.valueOf("user")); //分頁大小 Filter filter = new PageFilter(3); int totalRows = 0; byte[] lastRow = null; while (true) { Scan scan = new Scan(); scan.setFilter(filter); if (lastRow != null) { byte[] startRow = Bytes.add(lastRow, POSTFIX); System.out.println("start row: " + Bytes.toStringBinary(startRow)); scan.setStartRow(startRow); } ResultScanner scanner = table.getScanner(scan); int localRows = 0; Result result; while ((result = scanner.next()) != null) { System.out.println(localRows++ + ": " + result); totalRows++; lastRow = result.getRow(); } scanner.close(); if (localRows == 0) break; } System.out.println("total rows: " + totalRows); table.close(); connection.close(); } } /** 0: keyvalues={224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, 224382618261914241/info:phone/1472196211427/Put/vlen=11/seqid=0, 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0, 224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, 224382618261914241/ship:salary/1472196211594/Put/vlen=4/seqid=0} 1: keyvalues={510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0, 510824118261011172/info:height/1472196213056/Put/vlen=3/seqid=0, 510824118261011172/info:name/1472196212942/Put/vlen=8/seqid=0, 510824118261011172/info:phone/1472196213237/Put/vlen=11/seqid=0, 510824118261011172/info:weight/1472196213169/Put/vlen=3/seqid=0, 510824118261011172/ship:addr/1472196213328/Put/vlen=8/seqid=0, 510824118261011172/ship:email/1472196213422/Put/vlen=12/seqid=0, 510824118261011172/ship:salary/1472196214963/Put/vlen=5/seqid=0} 2: keyvalues={524382618264914241/info:age/1472196193913/Put/vlen=2/seqid=0, 524382618264914241/info:height/1472196194783/Put/vlen=3/seqid=0, 524382618264914241/info:name/1472196193255/Put/vlen=8/seqid=0, 524382618264914241/info:phone/1472196195125/Put/vlen=11/seqid=0, 524382618264914241/info:weight/1472196194970/Put/vlen=3/seqid=0, 524382618264914241/ship:addr/1472196195270/Put/vlen=7/seqid=0, 524382618264914241/ship:email/1472196195371/Put/vlen=13/seqid=0, 524382618264914241/ship:salary/1472196195485/Put/vlen=4/seqid=0} start row: 524382618264914241\x00 0: keyvalues={673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0, 673782618261019142/info:height/1472196211761/Put/vlen=3/seqid=0, 673782618261019142/info:name/1472196211678/Put/vlen=7/seqid=0, 673782618261019142/info:phone/1472196211956/Put/vlen=11/seqid=0, 673782618261019142/info:weight/1472196211841/Put/vlen=3/seqid=0, 673782618261019142/ship:addr/1472196212059/Put/vlen=8/seqid=0, 673782618261019142/ship:email/1472196212176/Put/vlen=12/seqid=0, 673782618261019142/ship:salary/1472196212284/Put/vlen=4/seqid=0} 1: keyvalues={813782218261011172/info:age/1472196212550/Put/vlen=2/seqid=0, 813782218261011172/info:height/1472196212605/Put/vlen=3/seqid=0, 813782218261011172/info:name/1472196212480/Put/vlen=8/seqid=0, 813782218261011172/info:phone/1472196212713/Put/vlen=11/seqid=0, 813782218261011172/info:weight/1472196212651/Put/vlen=3/seqid=0, 813782218261011172/ship:addr/1472196212762/Put/vlen=4/seqid=0, 813782218261011172/ship:email/1472196212802/Put/vlen=12/seqid=0, 813782218261011172/ship:salary/1472196212840/Put/vlen=5/seqid=0} start row: 813782218261011172\x00 2016-08-29 17:17:57,197 INFO [main] client.ConnectionManager$HConnectionImplementation: Closing zookeeper sessionid=0x56c13890bf003a total rows: 5 **/
FirstKeyOnlyFilter 首次行鍵過濾器
FirstKeyValueMatchingQualifiersFilter
FuzzyRowFilter 模糊行匹配過濾器
ColumnCountGetFilter 列計數過濾器
可使用這個過濾器來限制每行最多取回多少列,
當一行的列數到設定的最大值,這個過濾器會中止整個掃描操做。
適合在get方法中使用
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.ColumnCountGetFilter; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * ColumnCountGetFilter 列數過濾器 */ public class FilterOfColumnCountGetFilter { public static void main(String args[]) throws IOException{ Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("user")); //限制返回的列數 ColumnCountGetFilter columnCountGetFilter = new ColumnCountGetFilter(3); // column=info:age,column=info:height,column=info:name,column=info:phone,column=info:weight, Get get = new Get(Bytes.toBytes("224382618261914241")); get.setFilter(columnCountGetFilter); Result result = table.get(get); System.out.println("Result of columnCountGetFilter get: "); for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } Get get1 = new Get(Bytes.toBytes("224382618261914241")); Result result1 = table.get(get1); System.out.println("Result of get: "); for (Cell cell : result1.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } table.close(); connection.close(); } } /** Result of columnCountGetFilter get: Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 Cell: 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, Value: 158 Cell: 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, Value: lisi Result of get: Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 Cell: 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, Value: 158 Cell: 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, Value: lisi Cell: 224382618261914241/info:phone/1472196211427/Put/vlen=11/seqid=0, Value: 13213921424 Cell: 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0, Value: 128 Cell: 224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, Value: chengdu Cell: 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, Value: qq@sina.com Cell: 224382618261914241/ship:salary/1472196211594/Put/vlen=4/seqid=0, Value: 5000 **/
構造函數:limit 限制取回來列數 offset 偏移位就是開始位置 byte[] 字符串/書籤偏移從哪裏開始分頁。
ColumnPaginationFilter(int limit, int offset)
ColumnPaginationFilter(int limit, byte[] columnOffset)
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.ColumnPaginationFilter; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * ColumnPageFilter 列分頁過濾器 */ public class FilterOColumnPageFilter { public static void main(String args[]) throws IOException{ Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("user")); //限制返回的列數 從第4列開始取 取3列數據 ColumnPaginationFilter columnPaginationFilter = new ColumnPaginationFilter(3,4); Get get = new Get(Bytes.toBytes("224382618261914241")); get.setFilter(columnPaginationFilter); Result result = table.get(get); System.out.println("Result of ColumnPageFilter get: "); for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } //限制返回的列數 從第name列開始取 取3列數據 ColumnPaginationFilter columnPaginationFilter2 = new ColumnPaginationFilter(3,Bytes.toBytes("name")); Get get2 = new Get(Bytes.toBytes("224382618261914241")); get2.setFilter(columnPaginationFilter2); Result result2 = table.get(get2); System.out.println("Result of ColumnPageFilter get: "); for (Cell cell : result2.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } Get get1 = new Get(Bytes.toBytes("224382618261914241")); Result result1 = table.get(get1); System.out.println("Result of get: "); for (Cell cell : result1.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } table.close(); connection.close(); } } /** Result of ColumnPageFilter get: Cell: 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0, Value: 128 Cell: 224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, Value: chengdu Cell: 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, Value: qq@sina.com Result of ColumnPageFilter get: Cell: 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, Value: lisi Cell: 224382618261914241/info:phone/1472196211427/Put/vlen=11/seqid=0, Value: 13213921424 Cell: 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0, Value: 128 Result of get: Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 Cell: 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, Value: 158 Cell: 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, Value: lisi Cell: 224382618261914241/info:phone/1472196211427/Put/vlen=11/seqid=0, Value: 13213921424 Cell: 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0, Value: 128 Cell: 224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, Value: chengdu Cell: 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, Value: qq@sina.com Cell: 224382618261914241/ship:salary/1472196211594/Put/vlen=4/seqid=0, Value: 5000 **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.ColumnPrefixFilter; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * ColumnPrefixFilter 列前綴過濾器 */ public class FilterOfColumnPrefixFilter { public static void main(String args[]) throws IOException{ Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("user")); //取列名已ag開頭的 Filter columnPrefixFilter = new ColumnPrefixFilter(Bytes.toBytes("a")); Get get = new Get(Bytes.toBytes("224382618261914241")); get.setFilter(columnPrefixFilter); Result result = table.get(get); System.out.println("Result of columnPrefixFilter get: "); for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } Get get1 = new Get(Bytes.toBytes("224382618261914241")); Result result1 = table.get(get1); System.out.println("Result of get: "); for (Cell cell : result1.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } table.close(); connection.close(); } } /** Result of columnPrefixFilter get: Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 Cell: 224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, Value: chengdu Result of get: Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 Cell: 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, Value: 158 Cell: 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, Value: lisi Cell: 224382618261914241/info:phone/1472196211427/Put/vlen=11/seqid=0, Value: 13213921424 Cell: 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0, Value: 128 Cell: 224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, Value: chengdu Cell: 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, Value: qq@sina.com Cell: 224382618261914241/ship:salary/1472196211594/Put/vlen=4/seqid=0, Value: 5000 **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * MultipleColumnPrefixFilter 多個列前綴過濾器 */ public class FilterOfMultipleColumnPrefixFilter { public static void main(String args[]) throws IOException{ Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("user")); //取列名已a開頭的 還有已h 開頭的 Filter filter = new MultipleColumnPrefixFilter(new byte[][] {Bytes.toBytes("a"),Bytes.toBytes("h")}); Get get = new Get(Bytes.toBytes("224382618261914241")); get.setFilter(filter); Result result = table.get(get); System.out.println("Result of columnPrefixFilter get: "); for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } Get get1 = new Get(Bytes.toBytes("224382618261914241")); Result result1 = table.get(get1); System.out.println("Result of get: "); for (Cell cell : result1.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } table.close(); connection.close(); } } /** Result of columnPrefixFilter get: Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 Cell: 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, Value: 158 Cell: 224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, Value: chengdu Result of get: Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 Cell: 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, Value: 158 Cell: 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, Value: lisi Cell: 224382618261914241/info:phone/1472196211427/Put/vlen=11/seqid=0, Value: 13213921424 Cell: 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0, Value: 128 Cell: 224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, Value: chengdu Cell: 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, Value: qq@sina.com Cell: 224382618261914241/ship:salary/1472196211594/Put/vlen=4/seqid=0, Value: 5000 **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.ColumnRangeFilter; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * ColumnRangeFilter 列範圍過濾器 */ public class FilterOfColumnRangeFilter { public static void main(String args[]) throws IOException{ Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("user")); //minColumn - minimum value for the column range. If if it's null, there is no lower bound. //minColumnInclusive - if true, include minColumn in the range. 若是是true 就要包含minColumn //maxColumn - maximum value for the column range. If it's null, //maxColumnInclusive - if true, include maxColumn in the range. there is no upper bound. //從email到phone範圍內的全部列 第二個參數爲true因此包含了email
Filter filter = new ColumnRangeFilter(Bytes.toBytes("email"), true, Bytes.toBytes("phone"), false); Get get = new Get(Bytes.toBytes("224382618261914241")); get.setFilter(filter); Result result = table.get(get); System.out.println("Result of ColumnRangeFilter get: "); for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } Get get1 = new Get(Bytes.toBytes("224382618261914241")); Result result1 = table.get(get1); System.out.println("Result of get: "); for (Cell cell : result1.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } table.close(); connection.close(); } } /** Result of ColumnRangeFilter get: Cell: 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, Value: 158 Cell: 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, Value: lisi Cell: 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, Value: qq@sina.com Result of get: Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 Cell: 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, Value: 158 Cell: 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, Value: lisi Cell: 224382618261914241/info:phone/1472196211427/Put/vlen=11/seqid=0, Value: 13213921424 Cell: 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0, Value: 128 Cell: 224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, Value: chengdu Cell: 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, Value: qq@sina.com Cell: 224382618261914241/ship:salary/1472196211594/Put/vlen=4/seqid=0, Value: 5000 2016-08-31 17:53:28,394 INFO [main] client.ConnectionManager$HConnectionImplementati **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.CompareFilter; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.filter.SubstringComparator; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * SingleColumnValueFilter 單列過濾器 */ public class FilterOfSingleColumnValueFilter { public static void main(String args[]) throws IOException{ Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("user")); // 510824118261011172 column=ship:email, timestamp=1472196213422, value=199@sina.com SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("ship"),Bytes.toBytes("email"), CompareFilter.CompareOp.EQUAL,new SubstringComparator("199@sina.com")); singleColumnValueFilter.setFilterIfMissing(true); Scan scan = new Scan(); scan.setFilter(singleColumnValueFilter); ResultScanner results = table.getScanner(scan); for (Result result:results){ for (Cell cell :result.rawCells()){ System.out.println("Cell: "+cell+",Value:" + Bytes.toString(cell.getValueArray(),cell.getValueOffset(), cell.getValueLength())); } } results.close(); // 224382618261914241 column=ship:email, timestamp=1472196211530, value=qq@sina.com Get get = new Get(Bytes.toBytes("224382618261914241")); get.setFilter(singleColumnValueFilter); Result result = table.get(get); System.out.println("Result of get: "); for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } Get get1 = new Get(Bytes.toBytes("510824118261011172")); get1.setFilter(singleColumnValueFilter); Result result1 = table.get(get1); System.out.println("Result of get1: "); for (Cell cell : result1.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } table.close(); connection.close(); } } /** Cell: 510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0,Value:18 Cell: 510824118261011172/info:height/1472196213056/Put/vlen=3/seqid=0,Value:188 Cell: 510824118261011172/info:name/1472196212942/Put/vlen=8/seqid=0,Value:yangyang Cell: 510824118261011172/info:phone/1472196213237/Put/vlen=11/seqid=0,Value:18013921626 Cell: 510824118261011172/info:weight/1472196213169/Put/vlen=3/seqid=0,Value:138 Cell: 510824118261011172/ship:addr/1472196213328/Put/vlen=8/seqid=0,Value:shanghai Cell: 510824118261011172/ship:email/1472196213422/Put/vlen=12/seqid=0,Value:199@sina.com Cell: 510824118261011172/ship:salary/1472196214963/Put/vlen=5/seqid=0,Value:50000 Result of get: Result of get1: Cell: 510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0, Value: 18 Cell: 510824118261011172/info:height/1472196213056/Put/vlen=3/seqid=0, Value: 188 Cell: 510824118261011172/info:name/1472196212942/Put/vlen=8/seqid=0, Value: yangyang Cell: 510824118261011172/info:phone/1472196213237/Put/vlen=11/seqid=0, Value: 18013921626 Cell: 510824118261011172/info:weight/1472196213169/Put/vlen=3/seqid=0, Value: 138 Cell: 510824118261011172/ship:addr/1472196213328/Put/vlen=8/seqid=0, Value: shanghai Cell: 510824118261011172/ship:email/1472196213422/Put/vlen=12/seqid=0, Value: 199@sina.com Cell: 510824118261011172/ship:salary/1472196214963/Put/vlen=5/seqid=0, Value: 50000 **/
// 510824118261011172 column=ship:email, timestamp=1472196213422, value=199@sina.com SingleColumnValueExcludeFilter singleColumnValueExcludeFilter = new SingleColumnValueExcludeFilter(Bytes.toBytes("ship"),Bytes.toBytes("email"), CompareFilter.CompareOp.EQUAL,new SubstringComparator("199@sina.com")); singleColumnValueExcludeFilter.setFilterIfMissing(true); Scan scan = new Scan(); scan.setFilter(singleColumnValueExcludeFilter); ResultScanner results = table.getScanner(scan); for (Result result:results){ for (Cell cell :result.rawCells()){ System.out.println("Cell: "+cell+",Value:" + Bytes.toString(cell.getValueArray(),cell.getValueOffset(), cell.getValueLength())); } } results.close(); // 224382618261914241 column=ship:email, timestamp=1472196211530, value=qq@sina.com Get get = new Get(Bytes.toBytes("224382618261914241")); get.setFilter(singleColumnValueExcludeFilter); Result result = table.get(get); System.out.println("Result of get: "); for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } Get get1 = new Get(Bytes.toBytes("510824118261011172")); get1.setFilter(singleColumnValueExcludeFilter); Result result1 = table.get(get1); System.out.println("Result of get1: "); for (Cell cell : result1.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } /** * Cell: 510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0,Value:18 Cell: 510824118261011172/info:height/1472196213056/Put/vlen=3/seqid=0,Value:188 Cell: 510824118261011172/info:name/1472196212942/Put/vlen=8/seqid=0,Value:yangyang Cell: 510824118261011172/info:phone/1472196213237/Put/vlen=11/seqid=0,Value:18013921626 Cell: 510824118261011172/info:weight/1472196213169/Put/vlen=3/seqid=0,Value:138 Cell: 510824118261011172/ship:addr/1472196213328/Put/vlen=8/seqid=0,Value:shanghai Cell: 510824118261011172/ship:salary/1472196214963/Put/vlen=5/seqid=0,Value:50000 Result of get: Result of get1: Cell: 510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0, Value: 18 Cell: 510824118261011172/info:height/1472196213056/Put/vlen=3/seqid=0, Value: 188 Cell: 510824118261011172/info:name/1472196212942/Put/vlen=8/seqid=0, Value: yangyang Cell: 510824118261011172/info:phone/1472196213237/Put/vlen=11/seqid=0, Value: 18013921626 Cell: 510824118261011172/info:weight/1472196213169/Put/vlen=3/seqid=0, Value: 138 Cell: 510824118261011172/ship:addr/1472196213328/Put/vlen=8/seqid=0, Value: shanghai Cell: 510824118261011172/ship:salary/1472196214963/Put/vlen=5/seqid=0, Value: 50000 */
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.TimestampsFilter; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * TimestampsFilter 時間過濾器 */ public class FilterOfTimestampsFilter { public static void main(String args[]) throws IOException{ Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("user")); List<Long> ts = new ArrayList<Long>(); ts.add(new Long("1472196195270")); ts.add(new Long("1472196212480")); ts.add(new Long(15)); Filter filter = new TimestampsFilter(ts); Scan scan1 = new Scan(); scan1.setFilter(filter); ResultScanner scanner1 = table.getScanner(scan1); for(Result result:scanner1){ System.out.println(result); } scanner1.close(); Scan scan2 = new Scan(); scan2.setFilter(filter); //加了時間範圍 故意多加了1s 1472196212480+1=1472196212481 scan2.setTimeRange(1472196195271L, 1472196212481L); ResultScanner scanner2 = table.getScanner(scan2); System.out.println("Add time range:"); for (Result result : scanner2) { System.out.println(result); } scanner2.close(); } } /** keyvalues={524382618264914241/ship:addr/1472196195270/Put/vlen=7/seqid=0} keyvalues={813782218261011172/info:name/1472196212480/Put/vlen=8/seqid=0} Add time range: keyvalues={813782218261011172/info:name/1472196212480/Put/vlen=8/seqid=0} **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.RandomRowFilter; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * RandomRowFilter 隨機行過濾器 */ public class FilterOfRandomRowFilter { public static void main(String args[]) throws IOException{ Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("user")); //該構造參數0-1之間,若是爲負數所有過濾,大於1所有經過 0.2f表的該行數據20%的機率經過 Filter filter = new RandomRowFilter(0.2f); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner results = table.getScanner(scan); for (Result result:results) { for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } table.close(); connection.close(); } } /** Cell: 510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0, Value: 18 Cell: 510824118261011172/info:height/1472196213056/Put/vlen=3/seqid=0, Value: 188 Cell: 510824118261011172/info:name/1472196212942/Put/vlen=8/seqid=0, Value: yangyang Cell: 510824118261011172/info:phone/1472196213237/Put/vlen=11/seqid=0, Value: 18013921626 Cell: 510824118261011172/info:weight/1472196213169/Put/vlen=3/seqid=0, Value: 138 Cell: 510824118261011172/ship:addr/1472196213328/Put/vlen=8/seqid=0, Value: shanghai Cell: 510824118261011172/ship:email/1472196213422/Put/vlen=12/seqid=0, Value: 199@sina.com Cell: 510824118261011172/ship:salary/1472196214963/Put/vlen=5/seqid=0, Value: 50000 **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * SkipFilter 跳過(忽略)過濾器 * similarface * similarface@outlook.com */ public class FilterOfSkipFilter { public static void main(String args[]) throws IOException{ Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("user")); //510824118261011172 column=info:height, timestamp=1472196213056, value=188 //673782618261019142 column=info:weight, timestamp=1472196211841, value=188 //若是列值中含有188這個數值,那麼這列將會跳過 Filter filter = new ValueFilter(CompareFilter.CompareOp.NOT_EQUAL,new BinaryComparator(Bytes.toBytes("188"))); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner results = table.getScanner(scan); for (Result result:results) { for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } Scan scan1 = new Scan(); //skipfilter的構造參數時filter // filter==>若是列值中含有188這個數值,那麼這列將會跳過 filter2==>就是若是列值中含有188這個數值那麼整個行都會被跳過 表示不會出現[510824118261011172,673782618261019142] Filter filter2 = new SkipFilter(filter); scan1.setFilter(filter2); ResultScanner scanner2= table.getScanner(scan1); for(Result result:scanner2){ for (Cell cell : result.rawCells()) { System.out.println("SKIP Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } results.close(); scanner2.close(); table.close(); connection.close(); } } /** Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 Cell: 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, Value: 158 Cell: 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, Value: lisi Cell: 224382618261914241/info:phone/1472196211427/Put/vlen=11/seqid=0, Value: 13213921424 Cell: 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0, Value: 128 Cell: 224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, Value: chengdu Cell: 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, Value: qq@sina.com Cell: 224382618261914241/ship:salary/1472196211594/Put/vlen=4/seqid=0, Value: 5000 Cell: 510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0, Value: 18 Cell: 510824118261011172/info:name/1472196212942/Put/vlen=8/seqid=0, Value: yangyang Cell: 510824118261011172/info:phone/1472196213237/Put/vlen=11/seqid=0, Value: 18013921626 Cell: 510824118261011172/info:weight/1472196213169/Put/vlen=3/seqid=0, Value: 138 Cell: 510824118261011172/ship:addr/1472196213328/Put/vlen=8/seqid=0, Value: shanghai Cell: 510824118261011172/ship:email/1472196213422/Put/vlen=12/seqid=0, Value: 199@sina.com Cell: 510824118261011172/ship:salary/1472196214963/Put/vlen=5/seqid=0, Value: 50000 Cell: 524382618264914241/info:age/1472196193913/Put/vlen=2/seqid=0, Value: 30 Cell: 524382618264914241/info:height/1472196194783/Put/vlen=3/seqid=0, Value: 168 Cell: 524382618264914241/info:name/1472196193255/Put/vlen=8/seqid=0, Value: zhangsan Cell: 524382618264914241/info:phone/1472196195125/Put/vlen=11/seqid=0, Value: 13212321424 Cell: 524382618264914241/info:weight/1472196194970/Put/vlen=3/seqid=0, Value: 168 Cell: 524382618264914241/ship:addr/1472196195270/Put/vlen=7/seqid=0, Value: beijing Cell: 524382618264914241/ship:email/1472196195371/Put/vlen=13/seqid=0, Value: sina@sina.com Cell: 524382618264914241/ship:salary/1472196195485/Put/vlen=4/seqid=0, Value: 3000 Cell: 673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0, Value: 19 Cell: 673782618261019142/info:height/1472196211761/Put/vlen=3/seqid=0, Value: 178 Cell: 673782618261019142/info:name/1472196211678/Put/vlen=7/seqid=0, Value: zhaoliu Cell: 673782618261019142/info:phone/1472196211956/Put/vlen=11/seqid=0, Value: 17713921424 Cell: 673782618261019142/ship:addr/1472196212059/Put/vlen=8/seqid=0, Value: shenzhen Cell: 673782618261019142/ship:email/1472196212176/Put/vlen=12/seqid=0, Value: 126@sina.com Cell: 673782618261019142/ship:salary/1472196212284/Put/vlen=4/seqid=0, Value: 8000 Cell: 813782218261011172/info:age/1472196212550/Put/vlen=2/seqid=0, Value: 19 Cell: 813782218261011172/info:height/1472196212605/Put/vlen=3/seqid=0, Value: 158 Cell: 813782218261011172/info:name/1472196212480/Put/vlen=8/seqid=0, Value: wangmazi Cell: 813782218261011172/info:phone/1472196212713/Put/vlen=11/seqid=0, Value: 12713921424 Cell: 813782218261011172/info:weight/1472196212651/Put/vlen=3/seqid=0, Value: 118 Cell: 813782218261011172/ship:addr/1472196212762/Put/vlen=4/seqid=0, Value: xian Cell: 813782218261011172/ship:email/1472196212802/Put/vlen=12/seqid=0, Value: 139@sina.com Cell: 813782218261011172/ship:salary/1472196212840/Put/vlen=5/seqid=0, Value: 10000 SKIP Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 SKIP Cell: 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, Value: 158 SKIP Cell: 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, Value: lisi SKIP Cell: 224382618261914241/info:phone/1472196211427/Put/vlen=11/seqid=0, Value: 13213921424 SKIP Cell: 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0, Value: 128 SKIP Cell: 224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, Value: chengdu SKIP Cell: 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, Value: qq@sina.com SKIP Cell: 224382618261914241/ship:salary/1472196211594/Put/vlen=4/seqid=0, Value: 5000 SKIP Cell: 524382618264914241/info:age/1472196193913/Put/vlen=2/seqid=0, Value: 30 SKIP Cell: 524382618264914241/info:height/1472196194783/Put/vlen=3/seqid=0, Value: 168 SKIP Cell: 524382618264914241/info:name/1472196193255/Put/vlen=8/seqid=0, Value: zhangsan SKIP Cell: 524382618264914241/info:phone/1472196195125/Put/vlen=11/seqid=0, Value: 13212321424 SKIP Cell: 524382618264914241/info:weight/1472196194970/Put/vlen=3/seqid=0, Value: 168 SKIP Cell: 524382618264914241/ship:addr/1472196195270/Put/vlen=7/seqid=0, Value: beijing SKIP Cell: 524382618264914241/ship:email/1472196195371/Put/vlen=13/seqid=0, Value: sina@sina.com SKIP Cell: 524382618264914241/ship:salary/1472196195485/Put/vlen=4/seqid=0, Value: 3000 SKIP Cell: 813782218261011172/info:age/1472196212550/Put/vlen=2/seqid=0, Value: 19 SKIP Cell: 813782218261011172/info:height/1472196212605/Put/vlen=3/seqid=0, Value: 158 SKIP Cell: 813782218261011172/info:name/1472196212480/Put/vlen=8/seqid=0, Value: wangmazi SKIP Cell: 813782218261011172/info:phone/1472196212713/Put/vlen=11/seqid=0, Value: 12713921424 SKIP Cell: 813782218261011172/info:weight/1472196212651/Put/vlen=3/seqid=0, Value: 118 SKIP Cell: 813782218261011172/ship:addr/1472196212762/Put/vlen=4/seqid=0, Value: xian SKIP Cell: 813782218261011172/ship:email/1472196212802/Put/vlen=12/seqid=0, Value: 139@sina.com SKIP Cell: 813782218261011172/ship:salary/1472196212840/Put/vlen=5/seqid=0, Value: 10000 **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * WhileMatchFilter 全匹配過濾器 [當匹配到就中斷掃描] * similarface * similarface@outlook.com */ public class FilterOfWhileMatchFilter { public static void main(String args[]) throws IOException{ Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("user")); //匹配 510824118261011172 的行 Filter filter1=new RowFilter(CompareFilter.CompareOp.NOT_EQUAL,new BinaryComparator(Bytes.toBytes("510824118261011172"))); Scan scan = new Scan(); scan.setFilter(filter1); ResultScanner results = table.getScanner(scan); for (Result result:results) { for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } // 匹配 510824118261011172 的行立刻中斷本次掃描操做 Filter filter2 = new WhileMatchFilter(filter1); Scan scan1 = new Scan(); scan1.setFilter(filter2); ResultScanner scanner2= table.getScanner(scan1); for(Result result:scanner2){ for (Cell cell : result.rawCells()) { System.out.println("WhileMatchFilter Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } results.close(); scanner2.close(); table.close(); connection.close(); } } /** Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 Cell: 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, Value: 158 ... Cell: 524382618264914241/ship:addr/1472196195270/Put/vlen=7/seqid=0, Value: beijing Cell: 524382618264914241/ship:email/1472196195371/Put/vlen=13/seqid=0, Value: sina@sina.com ... Cell: 813782218261011172/ship:salary/1472196212840/Put/vlen=5/seqid=0, Value: 10000 WhileMatchFilter Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 ... WhileMatchFilter Cell: 224382618261914241/ship:salary/1472196211594/Put/vlen=4/seqid=0, Value: 5000 **/
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * FilterList 過濾器列表[多個過濾器組合在一塊兒] * similarface * similarface@outlook.com */ public class FilterOfFilterList { public static void main(String args[]) throws IOException{ Configuration configuration = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("user")); List<Filter> filters = new ArrayList<Filter>(); Filter filter1=new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("524382618264914241"))); filters.add(filter1); Filter filter2=new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("813782218261011172"))); filters.add(filter2); //列名過濾器 Filter filter3=new QualifierFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("age")); filters.add(filter3); //行鍵 Between "524382618264914241" and "813782218261011172" and column="age" FilterList filterList1 = new FilterList(filters); Scan scan = new Scan(); scan.setFilter(filterList1); ResultScanner results = table.getScanner(scan); for (Result result:results) { for (Cell cell : result.rawCells()) { System.out.println("Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } results.close(); //行鍵 Between "524382618264914241" or "813782218261011172" or column="age" FilterList filterList2 = new FilterList(FilterList.Operator.MUST_PASS_ONE,filters); scan.setFilter(filterList2); ResultScanner scanner2= table.getScanner(scan); for(Result result:scanner2){ for (Cell cell : result.rawCells()) { System.out.println("MUST_PASS_ONE Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } scanner2.close(); table.close(); connection.close(); } } /** Cell: 524382618264914241/info:age/1472196193913/Put/vlen=2/seqid=0, Value: 30 Cell: 673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0, Value: 19 Cell: 813782218261011172/info:age/1472196212550/Put/vlen=2/seqid=0, Value: 19 MUST_PASS_ONE Cell: 224382618261914241/info:age/1472196211169/Put/vlen=2/seqid=0, Value: 24 MUST_PASS_ONE Cell: 224382618261914241/info:height/1472196211234/Put/vlen=3/seqid=0, Value: 158 MUST_PASS_ONE Cell: 224382618261914241/info:name/1472196211088/Put/vlen=4/seqid=0, Value: lisi MUST_PASS_ONE Cell: 224382618261914241/info:phone/1472196211427/Put/vlen=11/seqid=0, Value: 13213921424 MUST_PASS_ONE Cell: 224382618261914241/info:weight/1472196211386/Put/vlen=3/seqid=0, Value: 128 MUST_PASS_ONE Cell: 224382618261914241/ship:addr/1472196211487/Put/vlen=7/seqid=0, Value: chengdu MUST_PASS_ONE Cell: 224382618261914241/ship:email/1472196211530/Put/vlen=11/seqid=0, Value: qq@sina.com MUST_PASS_ONE Cell: 224382618261914241/ship:salary/1472196211594/Put/vlen=4/seqid=0, Value: 5000 MUST_PASS_ONE Cell: 510824118261011172/info:age/1472196213020/Put/vlen=2/seqid=0, Value: 18 MUST_PASS_ONE Cell: 510824118261011172/info:height/1472196213056/Put/vlen=3/seqid=0, Value: 188 MUST_PASS_ONE Cell: 510824118261011172/info:name/1472196212942/Put/vlen=8/seqid=0, Value: yangyang MUST_PASS_ONE Cell: 510824118261011172/info:phone/1472196213237/Put/vlen=11/seqid=0, Value: 18013921626 MUST_PASS_ONE Cell: 510824118261011172/info:weight/1472196213169/Put/vlen=3/seqid=0, Value: 138 MUST_PASS_ONE Cell: 510824118261011172/ship:addr/1472196213328/Put/vlen=8/seqid=0, Value: shanghai MUST_PASS_ONE Cell: 510824118261011172/ship:email/1472196213422/Put/vlen=12/seqid=0, Value: 199@sina.com MUST_PASS_ONE Cell: 510824118261011172/ship:salary/1472196214963/Put/vlen=5/seqid=0, Value: 50000 MUST_PASS_ONE Cell: 524382618264914241/info:age/1472196193913/Put/vlen=2/seqid=0, Value: 30 MUST_PASS_ONE Cell: 524382618264914241/info:height/1472196194783/Put/vlen=3/seqid=0, Value: 168 MUST_PASS_ONE Cell: 524382618264914241/info:name/1472196193255/Put/vlen=8/seqid=0, Value: zhangsan MUST_PASS_ONE Cell: 524382618264914241/info:phone/1472196195125/Put/vlen=11/seqid=0, Value: 13212321424 MUST_PASS_ONE Cell: 524382618264914241/info:weight/1472196194970/Put/vlen=3/seqid=0, Value: 168 MUST_PASS_ONE Cell: 524382618264914241/ship:addr/1472196195270/Put/vlen=7/seqid=0, Value: beijing MUST_PASS_ONE Cell: 524382618264914241/ship:email/1472196195371/Put/vlen=13/seqid=0, Value: sina@sina.com MUST_PASS_ONE Cell: 524382618264914241/ship:salary/1472196195485/Put/vlen=4/seqid=0, Value: 3000 MUST_PASS_ONE Cell: 673782618261019142/info:age/1472196211733/Put/vlen=2/seqid=0, Value: 19 MUST_PASS_ONE Cell: 673782618261019142/info:height/1472196211761/Put/vlen=3/seqid=0, Value: 178 MUST_PASS_ONE Cell: 673782618261019142/info:name/1472196211678/Put/vlen=7/seqid=0, Value: zhaoliu MUST_PASS_ONE Cell: 673782618261019142/info:phone/1472196211956/Put/vlen=11/seqid=0, Value: 17713921424 MUST_PASS_ONE Cell: 673782618261019142/info:weight/1472196211841/Put/vlen=3/seqid=0, Value: 188 MUST_PASS_ONE Cell: 673782618261019142/ship:addr/1472196212059/Put/vlen=8/seqid=0, Value: shenzhen MUST_PASS_ONE Cell: 673782618261019142/ship:email/1472196212176/Put/vlen=12/seqid=0, Value: 126@sina.com MUST_PASS_ONE Cell: 673782618261019142/ship:salary/1472196212284/Put/vlen=4/seqid=0, Value: 8000 MUST_PASS_ONE Cell: 813782218261011172/info:age/1472196212550/Put/vlen=2/seqid=0, Value: 19 MUST_PASS_ONE Cell: 813782218261011172/info:height/1472196212605/Put/vlen=3/seqid=0, Value: 158 MUST_PASS_ONE Cell: 813782218261011172/info:name/1472196212480/Put/vlen=8/seqid=0, Value: wangmazi MUST_PASS_ONE Cell: 813782218261011172/info:phone/1472196212713/Put/vlen=11/seqid=0, Value: 12713921424 MUST_PASS_ONE Cell: 813782218261011172/info:weight/1472196212651/Put/vlen=3/seqid=0, Value: 118 MUST_PASS_ONE Cell: 813782218261011172/ship:addr/1472196212762/Put/vlen=4/seqid=0, Value: xian MUST_PASS_ONE Cell: 813782218261011172/ship:email/1472196212802/Put/vlen=12/seqid=0, Value: 139@sina.com MUST_PASS_ONE Cell: 813782218261011172/ship:salary/1472196212840/Put/vlen=5/seqid=0, Value: 10000 **/