1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
public class HBaseFilterTest {
@Test public void createTable(){ HBaseUtil.createTable("FileTable", new String[]{"fileInfo", "saveInfo"}); }
@Test public void addFileDetails(){ HBaseUtil.putRow("FileTable", "rowkey1", "fileInfo", "name", "file1.txt"); HBaseUtil.putRow("FileTable", "rowkey1", "fileInfo", "type", "txt"); HBaseUtil.putRow("FileTable", "rowkey1", "fileInfo", "size", "1024"); HBaseUtil.putRow("FileTable", "rowkey1", "saveInfo", "creator", "suiwo1"); HBaseUtil.putRow("FileTable", "rowkey2", "fileInfo", "name", "file2.jpg"); HBaseUtil.putRow("FileTable", "rowkey2", "fileInfo", "type", "jpg"); HBaseUtil.putRow("FileTable", "rowkey2", "fileInfo", "size", "2048"); HBaseUtil.putRow("FileTable", "rowkey2", "saveInfo", "creator", "suiwo3"); HBaseUtil.putRow("FileTable", "rowkey3", "fileInfo", "name", "file3.jpg"); HBaseUtil.putRow("FileTable", "rowkey3", "fileInfo", "type", "jpg"); HBaseUtil.putRow("FileTable", "rowkey3", "fileInfo", "size", "2048"); HBaseUtil.putRow("FileTable", "rowkey3", "saveInfo", "creator", "suiwo3"); }
@Test public void rowFilterTest(){ Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("rowkey1")));
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL, Arrays.asList(filter));
ResultScanner scanner = HBaseUtil.getScanner("FileTable","rowkey1","rowkey3", filterList);
if(scanner != null){ scanner.forEach(result -> { System.out.println("rowkey = " + Bytes.toString(result.getRow())); System.out.println("fileName = " + Bytes.toString(result.getValue(Bytes.toBytes("fileInfo"), Bytes.toBytes("name")))); }); scanner.close(); } }
@Test public void prefixFilterTest(){ Filter filter = new PrefixFilter(Bytes.toBytes("rowkey2")); FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL, Arrays.asList(filter)); ResultScanner scanner = HBaseUtil.getScanner("FileTable","rowkey1","rowkey3", filterList);
if(scanner != null){ scanner.forEach(result -> { System.out.println("rowkey = " + Bytes.toString(result.getRow())); System.out.println("fileName = " + Bytes.toString(result.getValue(Bytes.toBytes("fileInfo"), Bytes.toBytes("name")))); }); scanner.close(); } }
@Test public void keyOnlyFilterTest(){ Filter filter = new KeyOnlyFilter(true); FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL, Arrays.asList(filter)); ResultScanner scanner = HBaseUtil.getScanner("FileTable","rowkey1","rowkey3", filterList);
if(scanner != null){ scanner.forEach(result -> { System.out.println("rowkey = " + Bytes.toString(result.getRow())); System.out.println("fileName = " + Bytes.toString(result.getValue(Bytes.toBytes("fileInfo"), Bytes.toBytes("name")))); }); scanner.close(); } }
@Test public void columnPrefixFilterTest(){ Filter filter = new ColumnPrefixFilter(Bytes.toBytes("nam")); FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL, Arrays.asList(filter)); ResultScanner scanner = HBaseUtil.getScanner("FileTable","rowkey1","rowkey3", filterList);
if(scanner != null){ scanner.forEach(result -> { System.out.println("rowkey = " + Bytes.toString(result.getRow())); System.out.println("fileName = " + Bytes.toString(result.getValue(Bytes.toBytes("fileInfo"), Bytes.toBytes("name")))); System.out.println("fileType = " + Bytes.toString(result.getValue(Bytes.toBytes("fileInfo"), Bytes.toBytes("type")))); }); scanner.close(); } } }
|