1 packagecom.hblink.test;2
3 importjava.io.IOException;4 importjava.util.Scanner;5
6 importorg.apache.hadoop.conf.Configuration;7 importorg.apache.hadoop.hbase.client.HTable;8 importorg.apache.hadoop.hbase.client.Result;9 importorg.apache.hadoop.hbase.client.ResultScanner;10 importorg.apache.hadoop.hbase.client.Scan;11 importorg.apache.hadoop.hbase.filter.CompareFilter.CompareOp;12 importorg.apache.hadoop.hbase.filter.Filter;13 importorg.apache.hadoop.hbase.filter.RegexStringComparator;14 importorg.apache.hadoop.hbase.filter.RowFilter;15 importorg.apache.hadoop.hbase.filter.SingleColumnValueFilter;16 importorg.apache.hadoop.hbase.util.Bytes;17
18 importcom.hblink.demo.Hblink;19
20 public classScanHbase {21 public static Boolean flag = true;22 public static String string = null;23
24 public static void main(String[] args) throwsIOException {25 while(flag) {26 HTable hTable = null;27 Configuration configuration =Hblink.getHBaseConfiguration();28 hTable = new HTable(configuration, "stock-info");29
30 Scanner sc = newScanner(System.in);31 System.out.print("请输入需要查询的股票代码:");32 string =sc.next();33
34 //scanTestCell(hTable);
35 scanTestRow(hTable);36 hTable.close();37 if (string.equals("quit")) {38 flag = false;39 }40 }41 }42
43 /**
44 * 通过列查询45 *46 *@paramhTable47 *@throwsIOException48 */
49 static void scanTestCell(HTable hTable) throwsIOException {50
51 //设置过滤器
52 SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("f"),53 Bytes.toBytes("Date"), CompareOp.EQUAL, Bytes.toBytes(string));54 //设置全表扫描封装类
55 Scan scan = newScan();56 //添加过滤器(通过股票代码查询)
57 scan.setFilter(singleColumnValueFilter);58 //扫描
59 ResultScanner resultScanner =hTable.getScanner(scan);60 for(Result result : resultScanner) {61 byte[] data = result.getValue(Bytes.toBytes("f"), Bytes.toBytes("Date"));62 byte[] stock = result.getValue(Bytes.toBytes("f"), Bytes.toBytes("Stock"));63 byte[] top = result.getValue(Bytes.toBytes("f"), Bytes.toBytes("Top"));64 byte[] change_rate = result.getValue(Bytes.toBytes("f"), Bytes.toBytes("Change-rate"));65 byte[] volume = result.getValue(Bytes.toBytes("f"), Bytes.toBytes("Volume"));66 byte[] turnover = result.getValue(Bytes.toBytes("f"), Bytes.toBytes("Turnover"));67
68 System.out.print(Bytes.toString(data) + ";");69 System.out.print(Bytes.toString(stock) + ";");70 if (Bytes.toString(top).equals("--")) {71 System.out.print(Bytes.toString(top) + ";");72 } else{73 System.out.print(Bytes.toInt(top) + ";");74 }75 System.out.print(Bytes.toString(change_rate) + ";");76 System.out.print(Bytes.toString(volume) + ";");77 System.out.print(Bytes.toString(turnover));78 System.out.println();79
80 }81
82 }83
84 /**
85 * 通过正则--匹配行键86 *87 *@paramhTable88 *@throwsIOException89 */
90 static void scanTestRow(HTable hTable) throwsIOException {91 RegexStringComparator re = new RegexStringComparator("^" + string + "");92 Filter filter = newRowFilter(CompareOp.EQUAL, re);93 Scan scan = newScan();94 //添加过滤器(通过股票代码查询)
95 scan.setFilter(filter);96 //扫描
97 ResultScanner resultScanner =hTable.getScanner(scan);98 for(Result result : resultScanner) {99 byte[] data = result.getValue(Bytes.toBytes("f"), Bytes.toBytes("Date"));100 byte[] stock = result.getValue(Bytes.toBytes("f"), Bytes.toBytes("Stock"));101 byte[] top = result.getValue(Bytes.toBytes("f"), Bytes.toBytes("Top"));102 byte[] change_rate = result.getValue(Bytes.toBytes("f"), Bytes.toBytes("Change-rate"));103 byte[] volume = result.getValue(Bytes.toBytes("f"), Bytes.toBytes("Volume"));104 byte[] turnover = result.getValue(Bytes.toBytes("f"), Bytes.toBytes("Turnover"));105
106 System.out.print(Bytes.toString(data) + ";");107 System.out.print(Bytes.toString(stock) + ";");108 if (Bytes.toString(top).equals("--")) {109 System.out.print(Bytes.toString(top) + ";");110 } else{111 System.out.print(Bytes.toInt(top) + ";");112 }113 System.out.print(Bytes.toString(change_rate) + ";");114 System.out.print(Bytes.toString(volume) + ";");115 System.out.print(Bytes.toString(turnover));116 System.out.println();117
118 }119 }120 }