0
点赞
收藏
分享

微信扫一扫

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

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


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客户端配置

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


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

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

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

<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>

(可左右滑动)

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

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

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


3

客户端访问HBase工具类


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

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;
}
}

(可左右滑动)

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

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

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();
}
}

}

(可左右滑动)

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


4

HBase访问示例代码及运行


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

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) {

}
}
}

(可左右滑动)

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

2.示例代码运行

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



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​​


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


为天地立心,为生民立命,为往圣继绝学,为万世开太平。

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


推荐关注Hadoop实操,第一时间,分享更多Hadoop干货,欢迎转发和分享。

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

原创文章,欢迎转载,转载请注明:转载自微信公众号Hadoop实操

举报

相关推荐

0 条评论