hbase java api 连接_JavaAPI与hbase的交互

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值