Java 连接 HBase ( kerberized 集群 )

2018 年 8 月 10 日 ImportNew

(点击上方公众号,可快速关注)


来源:王洁 ,

imaidata.github.io/blog/2017/07/11/java连接HBASE(kerberized集群)/


社区原文 “Connecting to HBase in a Kerberos Enabled Cluster”:


https://community.hortonworks.com/articles/48831/connecting-to-hbase-in-a-kerberos-enabled-cluster.html


讲解如何通过 Java 或 Scala 在启用 Kerberos 的群集中连接到 HBase。

本测试需要一个启用了kerberos的HDP集群。集群搭建参考《Ambari在本地VM(centos7.3)部署hadoop集群》。本测试在HDP集群的C7302节点(centos7.3)上进行。首先,下载java样例代码:


https://imaidata.github.io/blog/ambari_centos/


$ cd /opt

$ git clone https://github.com/wbwangk/hdp-test-examples


这个github库是从jjmeyer0/hdp-test-examples库fork的。主要修改有:


  1. 修改了 pom.xml 文件:增加了对 HDP2.6.1 的支持;去掉了 Scala 相关依赖,因为会导致构建失败

  2. 修改了 src/main/java/com/jj/hbase/HBaseClient.java 中 jj 用户主体为 jj@AMBAR.APACHE.ORGI


创建keytab


在 c7302 节点用管理员账号登录 KDC,然后创建叫jj的主体,并导出 keytab:


$ kinit root/admin@AMBARI.APACHE.ORG

$ kadmin -q "addprinc jj"         (创建jj主体,需要输入两次密码,密码是1)

$ ktutil

ktutil:  addent -password -p jj -k 1 -e RC4-HMAC

Password for jj@AMBARI.APACHE.ORG: 1

ktutil:  wkt jj.keytab                              (生成了keytab文件)

ktutil:  q

$ scp jj.keytab /opt/hdp-test-examples/src/main/resources


准备HBase用户


jj 用户必须在 HBase 中获得正确的权限。Ambari 为 HBase创建一个管理员用户,通过 keytab 查找管理员用户主体。并利用它登录,利用密钥文件登录不需要密码:


$ klist -kt /etc/security/keytabs/hbase.headless.keytab           (查看hbase服务的printcipal )

KVNO Timestamp           Principal

---- ------------------- ------------------------------------------------------

   1 07/06/2017 03:53:35 hbase-hdp2610@AMBARI.APACHE.ORG

$ kinit -kt /etc/security/keytabs/hbase.headless.keytab hbase-hdp2610              (实测只能用这个主体登录,即使root/admin主体都不行)

$ hbase shell

hbase(main):001:0> grant 'jj','RW'


准备配置文件


运行例子需要的文件有三个:


  • hbase-site.xml

  • .keytab

  • krb5.conf   前文已经复制了jj.keytab,现在要复制另外两个。


由于使用HDP集群的节点充当客户机,所以直接在本节点复制文件即可:


$ scp /etc/hbase/conf/hbase-site.xml /opt/htp-test-examples/src/main/resources/

$ scp /etc/krb5.conf /opt/htp-test-examples/src/main/resources/


对于测试,建议在 hbase-site.xml 中更改 “hbase.client.retries.number” 属性。默认情况下为35。这个“重试次数”这在运行测试时太大了,复制后可以修改为3。


其它修改


目录/opt/hdp-test-examples/src`下有两个目录:`main`和`test`。`main`目录放置客户端程序,而`test`目录是单元测试目录。 来到目录`/opt/hdp-test-examples/src/test/java/com/jj下看看,发现除了hbase还有个pig目录。如果只是测试java客户端连接hbase,建议删除pig目录。否则在maven构建是也会执行pig的单元测试,而由于没有正确配置pig,导致必然出错使构建失败。


代码讲解


例子的 Java 代码位于 src/main/java/com/jj/hbase/HBaseClient.java。在代码中,首先需要做的是创建和加载 HBase 配置:


// Setting up the HBase configuration

Configuration configuration = new Configuration();

configuration.addResource("src/main/resources/hbase-site.xml");


接下来指向 krb5.conf 文件并设置 Kerberos 主体和 keytab。


// Point to the krb5.conf file.

System.setProperty("java.security.krb5.conf", "src/main/resources/krb5.conf");

System.setProperty("sun.security.krb5.debug", "true");

 

// Override these values by setting -DkerberosPrincipal and/or -DkerberosKeytab

String principal = System.getProperty("kerberosPrincipal", "jj@AMBARI.APACHE.ORG");

String keytabLocation = System.getProperty("kerberosKeytab", "src/main/resources/jj.keytab");


现在使用上面定义的主键和 keytab 登录。


UserGroupInformation.setConfiguration(configuration);

UserGroupInformation.loginUserFromKeytab(principal, keytabLocation)


Maven构建、测试


$ cd /opt/hdp-test-examples

$ mvn clean test -P hdp-2.6.1    (如果网络差则耗时较长)

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building hdp-test-examples 1.0-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hdp-test-examples ---

[INFO] Deleting /opt/hdp-test-examples/target

[INFO]

[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ hdp-test-examples ---

[debug] execute contextualize

[INFO] Using 'UTF-8' encoding to copy filtered resources.

[INFO] Copying 10 resources

[INFO]

[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hdp-test-examples ---

[INFO] Compiling 5 source files to /opt/hdp-test-examples/target/classes

[INFO]

[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ hdp-test-examples ---

[debug] execute contextualize

[INFO] Using 'UTF-8' encoding to copy filtered resources.

[INFO] Copying 1 resource

[INFO]

[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hdp-test-examples ---

[INFO] Compiling 1 source file to /opt/hdp-test-examples/target/test-classes

[INFO]

[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ hdp-test-examples ---

[INFO] Surefire report directory: /opt/hdp-test-examples/target/surefire-reports

 

-------------------------------------------------------

 T E S T S

-------------------------------------------------------

Running com.jj.hbase.HBaseClientTest

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.552 sec

 

Results :

 

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

 

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 5.145s

[INFO] Finished at: Wed Jul 19 07:19:34 UTC 2017

[INFO] Final Memory: 38M/91M

[INFO] ------------------------------------------------------------------------


可以自己读一下单元测试代码 /opt/hdp-test-examples/src/test/java/com/jj/hbase/HBaseClientTest.java。看上去,代码中它似乎连接上 HBase,然后建表并插入几行数据。


碰到的问题


  • 虚拟机内存不足,将内存由 3G 改成 4G 后问题解决;

  • 构建过程中一些 jar 包下载失败,修改 pom.xml,去掉 Scala相关依赖后问题解决;

  • pig 测试失败,删除 pig 的单元测试目录;

  • 通过 HBase shell 无法进行 grant,改用 hbase-hdp2610 主体并加大虚拟机内存后解决。


这里是完整代码


https://github.com/wbwangk/hdp-test-examples


Windows下的测试


前文是在 Centos7.3下进行的测试。下面在 Windows下进行测试。毕竟很多人使用 Windows+Eclipse 进行开发。下面的测试并没有直接使用 Eclipse,而是更直接的命令行测试。希望有人能够补充上 Eclipse 下的测试。关于 Eclipse 下的相关配置可以参考 hortonworks 的一篇社区文章(“Hortonworks Data Platform Artifacts”)


https://community.hortonworks.com/articles/43727/hortonworks-data-platform-development-guide.html


测试使用了git bash命令行工具。git base在 Windows 下模拟的类似 Linux 的命令,但实际上使用的 Windows  操作系统文件。关于 git base 的安装使用参考这个文档《Ambari 在本地 VM 部署 Hadoop 集群》。在 git base 上测试通过后,之后又直接在 Windows 命令行下进行了测试。需要说明的是,git bash 和 Windows 使用了不同的环境变量,如PATH。


https://imaidata.github.io/blog/ambari_vm/


在 Windows 下需要安装 JDK1.8 和 Maven。Maven是 Java 实现的,所以是所有平台通用的。在 Maven 的这篇文档(“Maven on Windows”)中要求 JDK 的安装目录名称不要有空格(如Program Files就不行)。Maven被我安装在了 e:\maven。在 git bash 下运行 Maven 的方法是 /e/maven/bin/mvn。


http://maven.apache.org/guides/getting-started/windows-prerequisites.html


准备代码和配置文件


测试在 Windows 的 e:\opt 目录下进行。以下操作在 git bash 窗口中进行:


$ cd /e/opt

$ git clone https://github.com/wbwangk/hdp-test-examples

$ cd hdp-test-examples

$ scp root@c7302:/etc/krb5.conf src/main/resources/

$ scp root@c7302:/etc/hbase/conf/hbase-site.xml src/main/resources/

$ scp root@c7302:/opt/hdp-test-examples/src/main/resources/jj.keytab src/main/resources/


上述三个 scp 操作时把测试用到3个配置文件从 Linux 下网络复制到了 Windows 下。确保 Windows 的 hosts 文件中定义了3台虚拟机的 IP 和域名。


执行构建和单元测试


$ /e/maven/bin/mvn clean test -P hdp-2.6.1

(省略一些下载信息)

-------------------------------------------------------

 T E S T S

-------------------------------------------------------

Running com.jj.hbase.HBaseClientTest

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.42 sec

 

Results :

 

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

 

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 02:27 min

[INFO] Finished at: 2017-07-20T07:40:15+08:00

[INFO] Final Memory: 31M/206M

[INFO] ------------------------------------------------------------------------


直接在Windows命令行下测试


进入 Windows 命令行后:


$ e:

$ cd \opt\hdp-test-examples

E:\opt\hdp-test-examples> mvn clean test -P hdp-2.6.1

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building hdp-test-examples 1.0-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hdp-test-examples ---

[INFO] Deleting E:\opt\hdp-test-examples\target

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hdp-test-examples ---

[INFO] Using 'UTF-8' encoding to copy filtered resources.

[INFO] Copying 10 resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hdp-test-examples ---

[INFO] Changes detected - recompiling the module!

[INFO] Compiling 5 source files to E:\opt\hdp-test-examples\target\classes

[INFO]

[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hdp-test-examples ---

[INFO] Using 'UTF-8' encoding to copy filtered resources.

[INFO] Copying 1 resource

[INFO]

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hdp-test-examples ---

[INFO] Changes detected - recompiling the module!

[INFO] Compiling 1 source file to E:\opt\hdp-test-examples\target\test-classes

[WARNING] /E:/opt/hdp-test-examples/src/test/java/com/jj/hbase/HBaseClientTest.java: E:\opt\hdp-test-examples\src\test\java\com\jj\hbase\HBaseClientTest.java使用了未经检查或不安全的操作。

[WARNING] /E:/opt/hdp-test-examples/src/test/java/com/jj/hbase/HBaseClientTest.java: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。

[INFO]

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hdp-test-examples ---

[INFO] Surefire report directory: E:\opt\hdp-test-examples\target\surefire-reports

 

-------------------------------------------------------

 T E S T S

-------------------------------------------------------

Running com.jj.hbase.HBaseClientTest

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.318 sec

 

Results :

 

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

 

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 3.624 s

[INFO] Finished at: 2017-07-20T08:15:17+08:00

[INFO] Final Memory: 30M/321M

[INFO] ------------------------------------------------------------------------


【关于投稿】


如果大家有原创好文投稿,请直接给公号发送留言。


① 留言格式:
【投稿】+《 文章标题》+ 文章链接

② 示例:
【投稿】《不要自称是程序员,我十多年的 IT 职场总结》:http://blog.jobbole.com/94148/

③ 最后请附上您的个人简介哈~



看完本文有收获?请转发分享给更多人

关注「ImportNew」,提升Java技能

登录查看更多
0

相关内容

HBase 全称是 Hadoop Database,是开源的高可靠性、高性能、可伸缩的分布式数据库系统,利用 HBase 技术建立大规模结构化存储集群。
【2020新书】实战R语言4,323页pdf
专知会员服务
98+阅读 · 2020年7月1日
【实用书】Python爬虫Web抓取数据,第二版,306页pdf
专知会员服务
115+阅读 · 2020年5月10日
【2020新书】Kafka实战:Kafka in Action,209页pdf
专知会员服务
65+阅读 · 2020年3月9日
【新书】Java企业微服务,Enterprise Java Microservices,272页pdf
TensorFlow Lite指南实战《TensorFlow Lite A primer》,附48页PPT
专知会员服务
68+阅读 · 2020年1月17日
【干货】大数据入门指南:Hadoop、Hive、Spark、 Storm等
专知会员服务
94+阅读 · 2019年12月4日
CALDERA 一款对手自动模拟工具
黑白之道
20+阅读 · 2019年9月17日
5大必知的图算法,附Python代码实现
AI100
4+阅读 · 2019年9月10日
在K8S上运行Kafka合适吗?会遇到哪些陷阱?
DBAplus社群
9+阅读 · 2019年9月4日
后渗透利用msf关闭防火墙
黑白之道
8+阅读 · 2019年8月24日
渗透某德棋牌游戏
黑白之道
12+阅读 · 2019年5月17日
浅谈 Kubernetes 在生产环境中的架构
DevOps时代
11+阅读 · 2019年5月8日
百度开源项目OpenRASP快速上手指南
黑客技术与网络安全
5+阅读 · 2019年2月12日
去哪儿网开源DNS管理系统OpenDnsdb
运维帮
21+阅读 · 2019年1月22日
R工程化—Rest API 之plumber包
R语言中文社区
11+阅读 · 2018年12月25日
Neo4j 和图数据库起步
Linux中国
8+阅读 · 2017年12月20日
Arxiv
9+阅读 · 2018年3月23日
Arxiv
11+阅读 · 2018年1月28日
VIP会员
相关资讯
CALDERA 一款对手自动模拟工具
黑白之道
20+阅读 · 2019年9月17日
5大必知的图算法,附Python代码实现
AI100
4+阅读 · 2019年9月10日
在K8S上运行Kafka合适吗?会遇到哪些陷阱?
DBAplus社群
9+阅读 · 2019年9月4日
后渗透利用msf关闭防火墙
黑白之道
8+阅读 · 2019年8月24日
渗透某德棋牌游戏
黑白之道
12+阅读 · 2019年5月17日
浅谈 Kubernetes 在生产环境中的架构
DevOps时代
11+阅读 · 2019年5月8日
百度开源项目OpenRASP快速上手指南
黑客技术与网络安全
5+阅读 · 2019年2月12日
去哪儿网开源DNS管理系统OpenDnsdb
运维帮
21+阅读 · 2019年1月22日
R工程化—Rest API 之plumber包
R语言中文社区
11+阅读 · 2018年12月25日
Neo4j 和图数据库起步
Linux中国
8+阅读 · 2017年12月20日
Top
微信扫码咨询专知VIP会员