1. 程式人生 > >Ambari Hive 無法正常查詢 問題分析處理

Ambari Hive 無法正常查詢 問題分析處理

mysql 大小寫 hive inconsistent

1.今天遇到一個錯誤,在連接到hive之後,無法執行任何命令。

$ beeline -u "jdbc:hive2://hostname.domain.cn:10000/default;principal=hive/[email protected];"
Connecting to jdbc:hive2://hostname.domain.cn:10000/default;principal=hive/[email protected];
Connected to: Apache Hive (version release-1.2.1-EDH-1.1.2)
Driver: Hive JDBC (version release-1.2.1-EDH-1.1.2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
Beeline version release-1.2.1-EDH-1.1.2 by Apache Hive
0: jdbc:hive2://hostname.domain.cn:10000> show databases;

返回錯誤如下

Error: Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException
(message:For direct MetaStore DB connections, we don't support retries at the client level.) (state=08S01,code=1)

2. 百度這個錯誤,很多文檔都提及是字符集的問題,需要保證mysql的字符集是latin1.

mysql>  show variables like '%collation_%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+

3. 如果字符集不是latin1,需要手動執行(本次問題,不是這個,我的字符集,原本就是latin1)

mysql>use hive
mysql>alter database hive character set latin1;

4. 後來嘗試重啟一下hive,結果,metastore竟然啟動失敗。查看日誌,說創建表失敗。這個hive已經運行一段時間了,一直沒有問題。對應的表也都是存在的,沒有道理重建的。
技術分享圖片

5.日誌輸出如下:

Traceback (most recent call last):
  File "/var/lib/ambari-agent/cache/common-services/HIVE/0.12.0.2.0/package/scripts/hive_metastore.py", line 259, in <module>
    HiveMetastore().execute()
  File "/usr/lib/python2.6/site-packages/resource_management/libraries/script/script.py", line 280, in execute
    method(env)
  File "/var/lib/ambari-agent/cache/common-services/HIVE/0.12.0.2.0/package/scripts/hive_metastore.py", line 59, in start
    self.configure(env)
  File "/var/lib/ambari-agent/cache/common-services/HIVE/0.12.0.2.0/package/scripts/hive_metastore.py", line 73, in configure
    hive(name = 'metastore')
  File "/usr/lib/python2.6/site-packages/ambari_commons/os_family_impl.py", line 89, in thunk
    return fn(*args, **kwargs)
  File "/var/lib/ambari-agent/cache/common-services/HIVE/0.12.0.2.0/package/scripts/hive.py", line 320, in hive
    user = params.hive_user
  File "/usr/lib/python2.6/site-packages/resource_management/core/base.py", line 155, in __init__
    self.env.run()
  File "/usr/lib/python2.6/site-packages/resource_management/core/environment.py", line 160, in run
    self.run_action(resource, action)
  File "/usr/lib/python2.6/site-packages/resource_management/core/environment.py", line 124, in run_action
    provider_action()
  File "/usr/lib/python2.6/site-packages/resource_management/core/providers/system.py", line 273, in action_run
    tries=self.resource.tries, try_sleep=self.resource.try_sleep)
  File "/usr/lib/python2.6/site-packages/resource_management/core/shell.py", line 71, in inner
    result = function(command, **kwargs)
  File "/usr/lib/python2.6/site-packages/resource_management/core/shell.py", line 93, in checked_call
    tries=tries, try_sleep=try_sleep)
  File "/usr/lib/python2.6/site-packages/resource_management/core/shell.py", line 141, in _call_wrapper
    result = _call(command, **kwargs_copy)
  File "/usr/lib/python2.6/site-packages/resource_management/core/shell.py", line 294, in _call
    raise Fail(err_msg)
resource_management.core.exceptions.Fail: Execution of 'export HIVE_CONF_DIR=/usr/hdp/current/hive-metastore/conf/conf.server ; /usr/hdp/current/hive-metastore/bin/schematool -initSchema -dbType mysql -userName hive -passWord [PROTECTED] -verbose' returned 1. Metastore connection URL:     jdbc:mysql://hostname.mysql.cn/hivemeta
Metastore Connection Driver :     com.mysql.jdbc.Driver
Metastore connection User:     hive
Starting metastore schema initialization to 1.2.0
Initialization script hive-schema-1.2.0.mysql.sql
Connecting to jdbc:mysql://hostname.mysql.cn/hivemeta
Connected to: MySQL (version 5.1.73)
Driver: MySQL Connector Java (version mysql-connector-java-5.1.40 ( Revision: 402933ef52cad9aa82624e80acbea46e3a701ce6 ))
Transaction isolation: TRANSACTION_READ_COMMITTED
0: jdbc:mysql://hostname.mysql.cn/hivem> !autocommit on
Autocommit status: true
0: jdbc:mysql://hostname.mysql.cn/hivem> /*!40101 SET @OLD_CHARACTER_SET_C 
LIENT=@@CHARACTER_SET_CLIENT */
No rows affected (0.003 seconds)
0: jdbc:mysql://hostname.mysql.cn/hivem> /*!40101 SET @OLD_CHARACTER_SET_R 
ESULTS=@@CHARACTER_SET_RESULTS */
No rows affected (0 seconds)
0: jdbc:mysql://hostname.mysql.cn/hivem> /*!40101 SET @OLD_COLLATION_CONNE 
CTION=@@COLLATION_CONNECTION */
No rows affected (0 seconds)
0: jdbc:mysql://hostname.mysql.cn/hivem> /*!40101 SET NAMES utf8 */
No rows affected (0 seconds)
0: jdbc:mysql://hostname.mysql.cn/hivem> /*!40103 SET @OLD_TIME_ZONE=@@TIM 
E_ZONE */
No rows affected (0.002 seconds)
0: jdbc:mysql://hostname.mysql.cn/hivem> /*!40103 SET TIME_ZONE='+00:00' * 
/
No rows affected (0 seconds)
0: jdbc:mysql://hostname.mysql.cn/hivem> /*!40014 SET @OLD_UNIQUE_CHECKS=@ 
@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */
No rows affected (0 seconds)
0: jdbc:mysql://hostname.mysql.cn/hivem> /*!40014 SET @OLD_FOREIGN_KEY_CHE 
CKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */
No rows affected (0.001 seconds)
0: jdbc:mysql://hostname.mysql.cn/hivem> /*!40101 SET @OLD_SQL_MODE=@@SQL_ 
MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */
No rows affected (0 seconds)
0: jdbc:mysql://hostname.mysql.cn/hivem> /*!40111 SET @OLD_SQL_NOTES=@@SQL 
_NOTES, SQL_NOTES=0 */
No rows affected (0.001 seconds)
0: jdbc:mysql://hostname.mysql.cn/hivem> /*!40101 SET @saved_cs_client     
 = @@character_set_client */
No rows affected (0 seconds)
0: jdbc:mysql://hostname.mysql.cn/hivem> /*!40101 SET character_set_client 
 = utf8 */
No rows affected (0.001 seconds)
0: jdbc:mysql://hostname.mysql.cn/hivem> CREATE TABLE IF NOT EXISTS `BUCKE 
TING_COLS` ( `SD_ID` bigint(20) NOT NULL, `BUCKET_COL_NAME` varchar(256) CHARACT 
ER SET latin1 COLLATE latin1_bin DEFAULT NULL, `INTEGER_IDX` int(11) NOT NULL, P 
RIMARY KEY (`SD_ID`,`INTEGER_IDX`), KEY `BUCKETING_COLS_N49` (`SD_ID`), CONSTRAI 
NT `BUCKETING_COLS_FK1` FOREIGN KEY (`SD_ID`) REFERENCES `SDS` (`SD_ID`) ) ENGIN 
E=InnoDB DEFAULT CHARSET=latin1
Error: Can't create table 'hivemeta.bucketing_cols' (errno: 121) (state=HY000,code=1005)
Closing: 0: jdbc:mysql://hostname.mysql.cn/hivemeta
org.apache.hadoop.hive.metastore.HiveMetaException: Schema initialization FAILED! Metastore state would be inconsistent !!
org.apache.hadoop.hive.metastore.HiveMetaException: Schema initialization FAILED! Metastore state would be inconsistent !!
    at org.apache.hive.beeline.HiveSchemaTool.doInit(HiveSchemaTool.java:270)
    at org.apache.hive.beeline.HiveSchemaTool.doInit(HiveSchemaTool.java:243)
    at org.apache.hive.beeline.HiveSchemaTool.main(HiveSchemaTool.java:473)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.io.IOException: Schema script failed, errorcode 2
    at org.apache.hive.beeline.HiveSchemaTool.runBeeLine(HiveSchemaTool.java:358)
    at org.apache.hive.beeline.HiveSchemaTool.runBeeLine(HiveSchemaTool.java:326)
    at org.apache.hive.beeline.HiveSchemaTool.doInit(HiveSchemaTool.java:266)
    ... 8 more
*** schemaTool failed ***

6.根據關鍵字“Schema initialization FAILED! Metastore state would be inconsistent !!”,查詢到hdp的官方論壇。
解釋說,這個是一個已知的問題。需要修改ambari的sql腳本,刪除mysql的一些index等。
https://community.hortonworks.com/questions/113748/hive-metastore-start.html

7. 官方建議的步驟繁多。個人比較懶。而且這個hive庫,前一天還是正常的。這個問題是突然出現的,我不傾向與,這個是bug的可能。
繼續分析,查看hivemetastore的log,還是有必要的。
發現問題,表不存在

2018-03-07 15:34:43,871 INFO  [pool-8-thread-200]: metastore.HiveMetaStore (HiveMetaStore.java:logInfo(746)) - 200: get_all_databases
2018-03-07 15:34:43,871 INFO  [pool-8-thread-200]: HiveMetaStore.audit (HiveMetaStore.java:logAuditEvent(371)) - [email protected]      ip=/172.18.98.57        cmd=get_all_databases
2018-03-07 15:34:43,873 ERROR [pool-8-thread-200]: metastore.RetryingHMSHandler (RetryingHMSHandler.java:invoke(165)) - HMSHandler Fatal error: javax.jdo.JDOException: Exception thrown when executing query
        at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:596)
      ......
NestedThrowablesStackTrace:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hivemeta.dbs' doesn't exist
        at sun.reflect.GeneratedConstructorAccessor60.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)


8.現在要從mysql入手分析,逐步檢查。

mysql> use hive
mysql> show tables;
+---------------------------+
| Tables_in_hivemeta        |
+---------------------------+
| BUCKETING_COLS            |
| CDS                       |
| COLUMNS_V2                |
| COMPACTION_QUEUE          |
| COMPLETED_TXN_COMPONENTS  |
| DATABASE_PARAMS           |
| DBS                       |
| DB_PRIVS                  |
| DELEGATION_TOKENS         |

9. 問題來了,表不存在。為什麽不存在呢?其實我已經知道問題了。Mysql表名大小寫敏感問題。

mysql>select count(*) from `DBS`;
ERROR 1146 (42S02): Table 'hivemeta.DBS' doesn't exist


10. 由於很多Oracle數據庫的表名是大小寫不敏感的。但是Mysql卻是表名大小寫敏感的。所以,很多開發人員,都喜歡將mysql設置為表名大小寫不敏感。
而show tables之後,我就懷疑這個可能了。

11. 經過檢查/etc/my.cnf,確實已經設置lower_case_table_names=1

12. Hive庫創建的時候,mysql是大小寫敏感的。所以,表名都是大寫的。而後期,有人修改了mysql參數,取消大小寫敏感。這樣,問題就來了。Mysql並不是完全不敏感,而是在設置參數lower_case_table_names=1 之後,創建和查詢的sql語句中的表名,默認修改為小寫的。而已經創建的表名,還是大寫的顯示。但是你無論用大寫表名,還是小寫表名,都無法正確查詢到了。

13. 這個問題出現的場景:
只有是在hive庫創建完成之後,又修改/etc/my.cnf,添加參數lower_case_table_names=1導致的。

14. 問題找到之後,有兩個解決方案:
a. 取消參數lower_case_table_names=1
b. 如果mysql的hive庫中沒有什麽數據,刪除hive庫表,重新創建hive meta表,直接就創建出小寫的表名了。
輸出就是小寫的了:

mysql> show tables;
+---------------------------+
| Tables_in_hive_beta       |
+---------------------------+
| bucketing_cols            |
| cds                       |
| columns_v2                |
| compaction_queue          |
| completed_txn_components  |
| database_params           |
| db_privs                  |
| dbs                       |
| delegation_tokens         |
| func_ru                   |

15. 我最後選擇取消參數lower_case_table_names=1,並重啟mysql。查詢正常了。

mysql> select count(*) from `DBS`;
+----------+
| count(*) |
+----------+
|        2 |

16. Ambari啟動hive metastore也成功了。
問題解決,一切恢復正常

$ beeline -u "jdbc:hive2://hostname.domain.cn:10000/default;principal=hive/[email protected];"
Connecting to jdbc:hive2://hostname.domain.cn:10000/default;principal=hive/[email protected];
Connected to: Apache Hive (version release-1.2.1-EDH-1.1.2)
Driver: Hive JDBC (version release-1.2.1-EDH-1.1.2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
Beeline version release-1.2.1-EDH-1.1.2 by Apache Hive
0: jdbc:hive2://hostname.domain.cn:10000> show databases;
+----------------+--+
| database_name  |
+----------------+--+
| default        |
| hbjt           |
+----------------+--+
2 rows selected (1.206 seconds)


最終的原因,就是有人修改的/etc/my.cnf,導致的本次問題。

Ambari Hive 無法正常查詢 問題分析處理