近期在使用HBase Java Client連接HBase服務端創建Configuration對象時,遇到了hbase-default.xml file seems to be for and old version of HBase的異常,經過查找資料及閱讀HBase相關源碼,對這類異常的解決方法做一下總結。
異常出現的原因
HBase客戶端創建Configuration對象時,需要使用hbase-*.jar包,其中*部分標識了連接的HBase版本號:
[root@xxxxxx]$ ls hbase-*.jar hbase-0.92.1.jar
然后,在hbase-*.jar包的hbase-default.xml中,有一個關於HBase默認版本號的配置項:
<property skipInDoc="true"> <name>hbase.defaults.for.version</name> <value>0.92.1</value> <description> This defaults file was compiled for version 0.92.1. This variable is used to make sure that a user doesn't have an old version of hbase-default.xml on the classpath. </description> </property>
在客戶端啟動時,會首先檢查jar包名中指定的版本號和jar包中hbase-default.xml中設置的hbase.defaults.for.version選項是否一致,正常情況下二者一致,沒有Exception拋出(如上述示例)。
否則,如果jar包名指定版本號小於hbase.defaults.for.version指定版本號,就會報出如下異常:
java.lang.RuntimeException: hbase-default.xml file seems to be for and old version of HBase ......
......
異常的解決方法
第一種情況
異常現象:
Caused by: java.lang.RuntimeException: hbase-default.xml file seems to be for and old version of HBase (@@@VERSION@@@), this version is 0.92.1 at org.apache.hadoop.hbase.HBaseConfiguration.checkDefaultsVersion(HBaseConfiguration.java:68) at org.apache.hadoop.hbase.HBaseConfiguration.addHbaseResources(HBaseConfiguration.java:100)
這種情況是因為hbase-default.xml中的hbase.defaults.for.version配置項在打包時沒有被正常替換成maven指定的版本號,具體自己可以解開hbase-*.jar打開hbase-default.xml進行驗證。解決方法:
1、打開HBase maven工程的pom.properties文件,確定是否指定了version=0.92.1這一配置選項,如果沒有,加上后進行重新打包。stackoverflow上有人建議將pom.xml中的如下部分:
<execution> <phase>process-resources</phase> <configuration> <target> <replace file="${project.build.outputDirectory}/hbase-default.xml" token="@@@VERSION@@@" value="${project.version}" /> </target> </configuration> <goals> <goal>run</goal> </goals> </execution>
將其中的target改為tasks:
<execution> <phase>process-resources</phase> <configuration> <tasks> <replace file="${project.build.outputDirectory}/hbase-default.xml" token="@@@VERSION@@@" value="${project.version}" /> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution>
經查閱Maven AntRun Plugin關於tasks和target的解釋說明:
target PlexusConfiguration 1.5 The XML for the Ant target. You can add anything you can add between <target> and </target> in a build.xml. tasks PlexusConfiguration - Deprecated. Use target instead
tasks已經不推薦使用,建議使用target。stackoverflow上作者說的情況,可能是由於AntRun插件版本低等原因(個人猜測,未經實際驗證)。如果你已經確定@@@VERSION@@@被成功替換掉,則不用再進行以上操作。
2、另一種方法是直接解壓jar包后修改hbase-default.xml,然后重新打成jar包。
第二種情況
異常現象:
Caused by: java.lang.RuntimeException: hbase-default.xml file seems to be for and old version of HBase (*.**.*), this version is 0.92.1 at org.apache.hadoop.hbase.HBaseConfiguration.checkDefaultsVersion(HBaseConfiguration.java:68) at org.apache.hadoop.hbase.HBaseConfiguration.addHbaseResources(HBaseConfiguration.java:100)
這種情況是因為hbase-default.xml中的hbase.defaults.for.version配置項與hbase-*.jar名中指定版本號不一致。解決方法:
1、打開工程目錄下的hbase-site.xml,將hbase.defaults.for.version.skip配置為true,忽略默認版本的檢查:
<property> <name>hbase.defaults.for.version.skip</name> <value>true</value> <description> Set to true to skip the 'hbase.defaults.for.version' check. Setting this to true can be useful in contexts other than the other side of a maven generation; i.e. running in an ide. You'll want to set this boolean to true to avoid seeing the RuntimException complaint: "hbase-default.xml file seems to be for and old version of HBase (0.92.1), this version is X.X.X-SNAPSHOT" </description> </property>
2、將二者修改為一致的版本號,具體可參考第一種情況里的方法。
第三種情況
異常現象:
Caused by: java.lang.RuntimeException: hbase-default.xml file seems to be for and old version of HBase (null), this version is 0.92.1 at org.apache.hadoop.hbase.HBaseConfiguration.checkDefaultsVersion(HBaseConfiguration.java:68) at org.apache.hadoop.hbase.HBaseConfiguration.addHbaseResources(HBaseConfiguration.java:100)
這種情況是因為加載hbase-default.xml失敗沒有獲取默認的版本號。這種情況與實際環境相關,下面是一些可以嘗試的解決方法:
1、刪除Java的工程目錄下的hbase-default.xml文件;
2、參考HBase mail list的搜索結果: 點擊這里。