前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何使用Java访问非Kerberos环境的HBase

如何使用Java访问非Kerberos环境的HBase

作者头像
Fayson
发布2018-11-19 10:18:43
1.5K0
发布2018-11-19 10:18:43
举报
文章被收录于专栏:Hadoop实操Hadoop实操

温馨提示:如果使用电脑查看图片不清晰,可以使用手机打开文章单击文中的图片放大查看高清原图。

Fayson的github: https://github.com/fayson/cdhproject

提示:代码块部分可以左右滑动查看噢

1

文档编写目的

在前面的文章Fayson介绍了《如何使用Java连接Kerberos的HBase》,虽然非Kerberos环境下访问HBase比较简单,本篇文章Fayson还是主要介绍使用Java访问非Kerberos环境的HBase。

  • 测试环境

1.CDH版本为5.13.1

2.OS为Redhat7.2

2

环境准备

1.从CDH集群下载HBase客户端配置

2.在开发环境下为本机配置集群hosts信息,因为下载的HBase客户端配置使用的为hostname

3.在工程的pom.xml文件中增加如下配置

代码语言:javascript
复制
<dependency>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-client</artifactId>
  <version>2.6.5</version>
</dependency>
<dependency>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-common</artifactId>
  <version>2.6.5</version>
</dependency>
<dependency>
  <groupId>org.apache.hbase</groupId>
  <artifactId>hbase-client</artifactId>
  <version>1.2.0</version>
</dependency>

(可左右滑动)

4.在创建的java工程中,将hbase-conf目录及配置文件拷贝至工程resources目录下

3

客户端访问HBase工具类

1.ClientUtils类主要提供客户端初始化方法,内容如下:

代码语言:javascript
复制
package com.cloudera.hbase.utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;

/**
 * package: com.cloudera.hbase.utils
 * describe: 访问HBase客户端工具类
 * creat_user: Fayson
 * email: htechinfo@163.com
 * creat_date: 2018/11/17
 * creat_time: 下午4:55
 * 公众号:Hadoop实操
 */
public class ClientUtils {

    /**
     * 初始化访问HBase访问
     */
    public static Configuration initHBaseENV() {
        try {
            Configuration configuration = HBaseConfiguration.create();
            configuration.addResource(ClientUtils.class.getClass().getResourceAsStream("/hbase-conf/core-site.xml"));
            configuration.addResource(ClientUtils.class.getClass().getResourceAsStream("/hbase-conf/hdfs-site.xml"));
            configuration.addResource(ClientUtils.class.getClass().getResourceAsStream("/hbase-conf/hbase-site.xml"));

            return  configuration;
        } catch(Exception e) {
            e.printStackTrace();
        }

        return  null;
    }
}

(可左右滑动)

2.HBaseUtils用于操作Kudu的工具类

代码语言:javascript
复制
package com.cloudera.hbase.utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * package: com.cloudera.hbase.utils
 * describe: 操作HBase工具类
 * creat_user: Fayson
 * email: htechinfo@163.com
 * creat_date: 2018/11/17
 * creat_time: 下午5:08
 * 公众号:Hadoop实操
 */
public class HBaseUtils {


    /**
     * 获取HBase Connection
     * @param configuration
     * @return
     */
    public static Connection initConn(Configuration configuration) {
        Connection connection = null;

        try {
            connection = ConnectionFactory.createConnection(configuration);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return connection;
    }

    /**
     * 获取库中所有的表
     * @param connection
     */
    public static void listTables(Connection connection){
        try {
            //获取所有的表名
            TableName[] tbs = connection.getAdmin().listTableNames();
            for (TableName tableName : tbs){
                System.out.println(tableName.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取HBase表中的数据
     * @param tname
     * @param connection
     */
    public static void readTable(String tname, Connection connection){
        try {
            TableName tableName = TableName.valueOf(tname);
            Admin admin = connection.getAdmin();
            //判断tname是否存在,存在就返回true,否则返回false
            Boolean flag = admin.tableExists(tableName);
            if(!flag) {
                System.out.println("表不存在");
                return;
            }
            //判断当前的表是否被禁用了,是就开启
            if (admin.isTableDisabled(tableName)){
                admin.enableTable(tableName);
            }

            Table table = connection.getTable(tableName);
            ResultScanner resultScanner = table.getScanner(new Scan());

            for (Result result:resultScanner){
                for (Cell cell:result.listCells()){
                    //取行健
                    String rowKey=Bytes.toString(CellUtil.cloneRow(cell));
                    //取到时间戳
                    long timestamp = cell.getTimestamp();
                    //取到族列
                    String family = Bytes.toString(CellUtil.cloneFamily(cell));
                    //取到修饰名
                    String qualifier  = Bytes.toString(CellUtil.cloneQualifier(cell));
                    //取到值
                    String value = Bytes.toString(CellUtil.cloneValue(cell));

                    System.out.println("RowKey : " + rowKey + ",  Timestamp : " + timestamp + ", ColumnFamily : " + family + ", Key : " + qualifier + ", Value : " + value);
                }
            }
            resultScanner.close();
            table.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

(可左右滑动)

4

HBase访问示例代码及运行

1.HBaseSample.java类调用API接口访问HBase示例代码

代码语言:javascript
复制
package com.cloudera.hbase;

import com.cloudera.hbase.utils.ClientUtils;
import com.cloudera.hbase.utils.HBaseUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.*;

/**
 * package: com.cloudera.hbase
 * describe: 访问非Kerberos环境下的HBase
 * creat_user: Fayson
 * email: htechinfo@163.com
 * creat_date: 2018/11/17
 * creat_time: 下午4:55
 * 公众号:Hadoop实操
 */
public class HBaseSample {

    public static void main(String[] args) {
        try {
            Configuration configuration = ClientUtils.initHBaseENV();
            Connection connection = HBaseUtils.initConn(configuration);
            if(connection == null) {
                System.exit(1);
            }
            //获取HBase库中所有的表
            HBaseUtils.listTables(connection);

            HBaseUtils.readTable("picHbase", connection);

            //释放连接
            connection.close();

        } catch (Exception e) {

        }
    }
}

(可左右滑动)

2.示例代码运行

5

总结

1.Java开发访问HBase时,注意Connection对象不要重复创建,在使用完成后记得进行close操作,以避免频繁操作时将Zookeeper的连接数占满。

GitHub源码地址:

https://github.com/fayson/cdhproject/blob/master/hbasedemo/src/main/java/com/cloudera/hbase/HBaseSample.java

https://github.com/fayson/cdhproject/blob/master/hbasedemo/src/main/java/com/cloudera/hbase/utils/ClientUtils.java

https://github.com/fayson/cdhproject/blob/master/hbasedemo/src/main/java/com/cloudera/hbase/utils/HBaseUtils.java

提示:代码块部分可以左右滑动查看噢

为天地立心,为生民立命,为往圣继绝学,为万世开太平。 温馨提示:如果使用电脑查看图片不清晰,可以使用手机打开文章单击文中的图片放大查看高清原图。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-11-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Hadoop实操 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档