Spring + Hbase整合

1、版本说明

(1)spring版本:4.3.9.RELEASE

(2)Hbase版本:1.1.2

(3)Hadoop版本:2.7.1

Spring + Hbase整合_第1张图片

Hbase和hadoop的版本一定要与集群上的版本一致,不然程序会报方法不存在的各种异常!

2、Hbase和Hadoop的pom



  org.apache.hbase
  hbase-client
  1.1.2


  org.apache.hbase
  hbase-server
  1.1.2


  org.apache.hbase
  hbase-common
  1.1.2



 
  org.apache.hadoop
  hadoop-hdfs
  2.7.1


  org.apache.hadoop
  hadoop-client
  2.7.1


  org.apache.hadoop
  hadoop-common
  2.7.1


  org.apache.hadoop
  hadoop-auth
  2.7.1

3、Hbase的使用(原生操作方式)

(1)HbaseClient:简单的连接管理

Hbase-site.xml从Hbase的安装目录下复制出来直接用。

public class HbaseClient {
    //自动配置hbase-site.xml
    //HBaseConfiguration会自动加载resources下的hbase-site.xml进行初始化
    public static Configuration config = HBaseConfiguration.create();

    /**
     * 获取连接(长连接,已实现连接池管理)
     * @return
     * @throws IOException
     */
    public static Connection getConnection() throws IOException {
        //手动配置hbase-site.xml
        //config.addResource("hbase-site.xml");
        Connection conn = ConnectionFactory.createConnection(config);
        return conn;
    }

    /**
     * 关闭连接
     * @param conn
     * @throws IOException
     */
    public static void close(Connection conn) throws IOException {
        if(conn!=null && !conn.isClosed()) {
            conn.close();
        }
    }
}

(2)Hbase命令操作

建表:create 'app_rec' ,'cf';

存值:put 'app_rec','1','cf:recdata','123';

取值:get 'app_rec','1';

字段说明:

表名:app_rec

key:1

族:cf

列名:recdata

Spring + Hbase整合_第2张图片

(3)Hbase原生API操作

1)controller下的get方法:

import com.utils.HbaseClient;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
------------------------------------------------------------

@RequestMapping( GET )
    public void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Connection connection = HbaseClient.getConnection();
        System.out.println("connection: "+ connection);
        TableName tableName = TableName.valueOf("app_rec");
        try {
            Table table = connection.getTable(tableName);
            Get get = new Get(Bytes.toBytes("1"));
            get.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("recdata"));
            Result results = table.get(get);
            showCell(results);
        } catch(Exception e) {

        } finally {
            HbaseClient.close(connection);
        }
    }

    public static void showCell(Result result) {
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
            System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");
        }
    }

2)部署后请求的结果如下,至此整合成功!

Spring + Hbase整合_第3张图片

本文只是做spring+hbase的搭建,搭建成功了后面就可以自由发挥了。

更多原生操作参见:

https://www.cnblogs.com/junrong624/p/7323483.html

https://blog.csdn.net/sinat_39409672/article/details/78403015

(4)扩展:Hbasetemplated操作Hbase

链接参见:https://blog.csdn.net/ludonqin/article/details/51509261

Hadoop与Hbase的版本对应:http://hbase.apache.org/book.html#basic.prerequisites

spring-data-hadoop:https://docs.spring.io/spring-hadoop/docs/2.3.0.RELEASE/reference/html/requirements.html

GitHub:https://github.com/spring-projects/spring-hadoop-samples/

提示:使用spring-data-hadoop会出现包冲突的问题,所以要自己控制版本!切记!

你可能感兴趣的:(Hbase)