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

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

框架下载地址

SpringMVC+Shiro+MongoDB+BootStrap基础框架

引入maven依赖

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

在pom.xml文件中新增

<dependency>
    <groupId>org.apache.hbasegroupId>
    <artifactId>hbase-thriftartifactId>
    <version>1.4.2version>
dependency>

开启hbase-thrift服务

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

hbase thrift2 start

默认端口是9090
更多关于thirift服务端的详情可查看
hadoop组件—面向列的开源数据库(三)—hbase的接口thrift

新建相关表格

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

hbase shell
create 'student','address','info'
put 'student','zzq','info:age','24'
put 'student','zzq','info:birthday','1990-05-03'
put 'student','zzq','info:company','23mofang'
put 'student','zzq','address:contry','china'
put 'student','zzq','address:province','guangxi'
put 'student','zzq','address:city','nanning'

编写java代码

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

package com.test.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.hbase.thrift2.generated.TColumnValue;
import org.apache.hadoop.hbase.thrift2.generated.TGet;
import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
import org.apache.hadoop.hbase.thrift2.generated.TIOError;
import org.apache.hadoop.hbase.thrift2.generated.TPut;
import org.apache.hadoop.hbase.thrift2.generated.TResult;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import java.nio.ByteBuffer;  

public class TestHbaseThrift {
    public static void main(String[] args) throws TIOError, TException {  
        System.out.println("Thrift2 Demo");  
        System.out.println("Usage: DemoClient [host=localhost] [port=9090]");  
        System.out.println("This demo assumes you have a table called \"student\" with a column family called \"info\"");  

        String host = "192.168.30.7";  
        int port = 9090;  

        int timeout = 10000;  
        boolean framed = false;  

        TTransport transport = new TSocket(host, port, timeout);  
        if (framed) {  
          transport = new TFramedTransport(transport);  
        }  
        TProtocol protocol = new TBinaryProtocol(transport);  
        // This is our thrift client.  
        THBaseService.Iface client = new THBaseService.Client(protocol);  

        // open the transport  
        transport.open();  

        ByteBuffer table = ByteBuffer.wrap("student".getBytes());  

        TPut put = new TPut();  
        put.setRow("joe".getBytes());  

        TColumnValue columnValue = new TColumnValue();  
        columnValue.setFamily("info".getBytes());  
        columnValue.setQualifier("age,".getBytes());  
        columnValue.setValue("29".getBytes());  
        List columnValues = new ArrayList();  
        columnValues.add(columnValue);  
        put.setColumnValues(columnValues);  

        client.put(table, put);  

        TGet get = new TGet();  
        get.setRow("zzq".getBytes());  

        TResult result = client.get(table, get);  

        System.out.print("row = " + new String(result.getRow()));  
        for (TColumnValue resultColumnValue : result.getColumnValues()) {  
          System.out.print(",family = " + new String(resultColumnValue.getFamily()));  
          System.out.print(",q = " + new String(resultColumnValue.getQualifier()));  
          System.out.print(",value = " + new String(resultColumnValue.getValue()));  
          System.out.print(",timestamp = " + resultColumnValue.getTimestamp());  
        }  

        transport.close();  
      }  

}

运行测试

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

Thrift2 Demo
Usage: DemoClient [host=localhost] [port=9090]
This demo assumes you have a table called "student" with a column family called "info"
row = 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

你可能感兴趣的:(hbase)