hadoop组件—面向列的开源数据库(五)–java–SpringMVC查询hbase

释放双眼,带上耳机,听听看~!

我们在之前的SpringMVC框架上直接使用thrift查询hbase。

框架下载地址

SpringMVC+Shiro+MongoDB+BootStrap基础框架

引入maven依赖

hbase已整合了thrift,如果是java不用再安装thrift产生服务端代码,只引入下面依赖:

在pom.xml文件中新增


1
2
3
4
5
6
1<dependency>
2    <groupId>org.apache.hbase</groupId>
3    <artifactId>hbase-thrift</artifactId>
4    <version>1.4.2</version>
5</dependency>
6

开启hbase-thrift服务

这里采用thrift2,thrift2是thrift的升级版。
在集群的服务器上使用命令


1
2
1hbase thrift2 start
2

新建相关表格

在集群服务器上进入hbase shell模式,创建定义表格
使用命令


1
2
3
4
5
6
7
8
9
1hbase shell
2create 'student','address','info'
3put 'student','zzq','info:age','24'
4put 'student','zzq','info:birthday','1990-05-03'
5put 'student','zzq','info:company','23mofang'
6put 'student','zzq','address:contry','china'
7put 'student','zzq','address:province','guangxi'
8put 'student','zzq','address:city','nanning'
9

编写java代码

新建一个类TestHbaseThrift.java
实现插入一条记录和查询一条记录
代码如下:


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
1package com.test.web.controller;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import org.apache.hadoop.hbase.thrift2.generated.TColumnValue;
7import org.apache.hadoop.hbase.thrift2.generated.TGet;
8import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
9import org.apache.hadoop.hbase.thrift2.generated.TIOError;
10import org.apache.hadoop.hbase.thrift2.generated.TPut;
11import org.apache.hadoop.hbase.thrift2.generated.TResult;
12import org.apache.thrift.TException;
13import org.apache.thrift.protocol.TBinaryProtocol;
14import org.apache.thrift.protocol.TProtocol;
15import org.apache.thrift.transport.TFramedTransport;
16import org.apache.thrift.transport.TSocket;
17import org.apache.thrift.transport.TTransport;
18import java.nio.ByteBuffer;  
19
20public class TestHbaseThrift {
21    public static void main(String[] args) throws TIOError, TException {  
22        System.out.println("Thrift2 Demo");  
23        System.out.println("Usage: DemoClient [host=localhost] [port=9090]");  
24        System.out.println("This demo assumes you have a table called \"student\" with a column family called \"info\"");  
25
26        String host = "192.168.30.7";  
27        int port = 9090;  
28
29        int timeout = 10000;  
30        boolean framed = false;  
31
32        TTransport transport = new TSocket(host, port, timeout);  
33        if (framed) {  
34          transport = new TFramedTransport(transport);  
35        }  
36        TProtocol protocol = new TBinaryProtocol(transport);  
37        // This is our thrift client.  
38        THBaseService.Iface client = new THBaseService.Client(protocol);  
39
40        // open the transport  
41        transport.open();  
42
43        ByteBuffer table = ByteBuffer.wrap("student".getBytes());  
44
45        TPut put = new TPut();  
46        put.setRow("joe".getBytes());  
47
48        TColumnValue columnValue = new TColumnValue();  
49        columnValue.setFamily("info".getBytes());  
50        columnValue.setQualifier("age,".getBytes());  
51        columnValue.setValue("29".getBytes());  
52        List<TColumnValue> columnValues = new ArrayList<TColumnValue>();  
53        columnValues.add(columnValue);  
54        put.setColumnValues(columnValues);  
55
56        client.put(table, put);  
57
58        TGet get = new TGet();  
59        get.setRow("zzq".getBytes());  
60
61        TResult result = client.get(table, get);  
62
63        System.out.print("row = " + new String(result.getRow()));  
64        for (TColumnValue resultColumnValue : result.getColumnValues()) {  
65          System.out.print(",family = " + new String(resultColumnValue.getFamily()));  
66          System.out.print(",q = " + new String(resultColumnValue.getQualifier()));  
67          System.out.print(",value = " + new String(resultColumnValue.getValue()));  
68          System.out.print(",timestamp = " + resultColumnValue.getTimestamp());  
69        }  
70
71        transport.close();  
72      }  
73
74}
75
76

运行测试

右键运行run as java application结果如下:


1
2
3
4
5
1Thrift2 Demo
2Usage: DemoClient [host=localhost] [port=9090]
3This demo assumes you have a table called "student" with a column family called "info"
4row = zzq,family = address,q = city,value = nanning,timestamp = 1520910557654,family = address,q = contry,value = china,timestamp = 1520910557610,family = address,q = province,value = guangxi,timestamp = 1520910557637,family = info,q = age,value = 24,,timestamp = 1520911412480,family = info,q = birthday,value = 1990-05-03,timestamp = 1520910557575,family = info,q = company,value = 23mofang,timestamp = 1520910557593
5

给TA打赏
共{{data.count}}人
人已打赏
安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

安全运维

设计模式的设计原则

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索