Java 連接 HBase(kerberized 集羣)

來源:王潔

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

講解如何通過 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的。主要修改有:

修改了 pom.xml 文件:增加了對 HDP2.6.1 的支持;去掉了 Scala 相關依賴,因爲會導致構建失敗

修改了 src/main/java/com/jj/hbase/HBaseClient.java 中 jj 用戶主體爲 [email protected]

創建keytab

在 c7302 節點用管理員賬號登錄 KDC,然後創建叫jj的主體,並導出 keytab:

$ kinit root/[email protected]

$ kadmin -q "addprinc jj" (創建jj主體,需要輸入兩次密碼,密碼是1)

$ ktutil

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

Password for [email protected]: 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 [email protected]

$ 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", "[email protected]");

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

相关文章