hive元数据的mysql迁移和升级的流程方案

本方案包含 hive-1.2 集群,新建的 hive-3.1集群(上面存储的表和库信息可以删除,升级之后和 hive-1.2 公用一个元数据库)。hive-1.2 集群使用的 mysql 数据库迁移到新的地址。升级 hive 1.2 元数据库到 3.1。升级之后,hive-1.2 集群和 hive-3.1 集群共同使用同一个 mysql。一个集群可以看到并操作另一个集群存储在 BOS 上的数据。

以下是操作步骤:

1. 停止hive-1.2 集群 的 hive-metastore 和 hive-server2

停止方法:使用命令systemctl stop hive-metastore 或者通过 ambari 操作。

systemctl stop hive-metastore
systemctl stop hive-server2

停止 hive metastore 后,集群所有的访问 hive 的任务都会报错。

2. 备份 mysql 数据库

mysqldump -h ${MYSQL_HOST} -uhive '-p${MYSQL_PASSWD}' hive > hive-1.2.bak

3. 恢复 mysql数据库到新的地址

登录新的数据库

mysql -h ${NEW_MYSQL_HOST} -uhive '-p${MYSQL_PASSWD}'

创建目标数据库

create datbase hive;

导入数据到新的数据库。

mysql -h ${NEW_MYSQL_HOST} -uhive '-p${MYSQL_PASSWD}' hive < hive-1.2.bak

${NEW_MYSQL_HOST} 是数据库新的地址。

4. hive-3.1 集群 停止所有的 hive-metastore 和 hive-server2

systemctl stop hive-metastore
systemctl stop hive-server2

5. hive-3.1 集群 修改 metastore 服务器所在的 hive-site.xml

按需要修改以下配置。

<property>
      <name>javax.jdo.option.ConnectionDriverName</name>
      <value>com.mysql.jdbc.Driver</value>
    </property>

    <property>
      <name>javax.jdo.option.ConnectionPassword</name>
      <value>${MYSQL_PASSWD}</value>
    </property>

    <property>
      <name>javax.jdo.option.ConnectionURL</name>
      <value>jdbc:mysql://${NEW_MYSQL_HOST}/hive?createDatabaseIfNotExist=true&characterEncoding=UTF-8</value>
    </property>
    <property>
      <name>javax.jdo.option.ConnectionUserName</name>
      <value>hive</value>
    </property>
    <property>
      <name>hive.metastore.schema.verification</name>
      <value>false</value>
    <description>
      Enforce metastore schema version consistency.
      True: Verify that version information stored in is compatible with one from Hive jars.  Also disable automatic
            schema migration attempt. Users are required to manually migrate schema after Hive upgrade which ensures
            proper metastore schema migration. (Default)
      False: Warn if the version information stored in metastore doesn't match with one from in Hive jars.
    </description>
    </property>

6. 升级 metastore 到 hive-3.1

在 hive-3.1 集群 metastore 所在服务器,使用 hive 用户下执行以下操作, ${NEW_MYSQL_HOST}是新 mysql 数据库所在的服务器 IP 地址。

cd /opt/bmr/hive/scripts/metastore/upgrade/mysql

mysql -uhive '-p${MYSQL_PASSWD}' -h ${NEW_MYSQL_HOST} hive
source upgrade-1.2.0-to-2.0.0.mysql.sql;
source upgrade-2.0.0-to-2.1.0.mysql.sql;
source upgrade-2.1.0-to-2.2.0.mysql.sql;
source upgrade-2.2.0-to-2.3.0.mysql.sql;
source upgrade-2.3.0-to-3.0.0.mysql.sql;
source upgrade-3.0.0-to-3.1.0.mysql.sql;

执行 source upgrade-1.2.0-to-2.0.0.mysql.sql 时,报以下错误,不用处理

ERROR 1060 (42S21): Duplicate column name 'CQ_HIGHEST_TXN_ID'
ERROR 1060 (42S21): Duplicate column name 'CQ_META_INFO'
ERROR 1060 (42S21): Duplicate column name 'CQ_HADOOP_JOB_ID'
ERROR 1050 (42S01): Table 'COMPLETED_COMPACTIONS' already exists
ERROR 1060 (42S21): Duplicate column name 'TXN_AGENT_INFO'
ERROR 1060 (42S21): Duplicate column name 'TXN_HEARTBEAT_COUNT'
ERROR 1060 (42S21): Duplicate column name 'HL_HEARTBEAT_COUNT'
ERROR 1060 (42S21): Duplicate column name 'TXN_META_INFO'
ERROR 1060 (42S21): Duplicate column name 'HL_AGENT_INFO'
ERROR 1060 (42S21): Duplicate column name 'HL_BLOCKEDBY_EXT_ID'
ERROR 1060 (42S21): Duplicate column name 'HL_BLOCKEDBY_INT_ID'
ERROR 1050 (42S01): Table 'AUX_TABLE' already exists

其他错误,需要暂停执行,如改字段的宽度可能失败。

7. hive-1.2 集群做以下操作

7.1 修改所有 metastore 和 hive-server 服务器的 hive-site.xml

<property>
      <name>javax.jdo.option.ConnectionDriverName</name>
      <value>com.mysql.jdbc.Driver</value>
    </property>

    <property>
      <name>javax.jdo.option.ConnectionPassword</name>
      <value>${MYSQL_PASSWD}</value>
    </property>

    <property>
      <name>javax.jdo.option.ConnectionURL</name>
      <value>jdbc:mysql://${NEW_MYSQL_HOST}/hive?createDatabaseIfNotExist=true&characterEncoding=UTF-8</value>
    </property>
    <property>
      <name>javax.jdo.option.ConnectionUserName</name>
      <value>hive</value>
    </property>
    <property>
      <name>hive.metastore.schema.verification</name>
      <value>false</value>
    <description>
      Enforce metastore schema version consistency.
      True: Verify that version information stored in is compatible with one from Hive jars.  Also disable automatic
            schema migration attempt. Users are required to manually migrate schema after Hive upgrade which ensures
            proper metastore schema migration. (Default)
      False: Warn if the version information stored in metastore doesn't match with one from in Hive jars.
    </description>
  </property>

7.2 hive-1.2 启动所有 hive-metastore 和 hive-server2

systemctl start hive-metastore
systemctl start hive-server2

hive-1.2 集群恢复正常。

8. hive-3.1 集群启动所有 hive-metastore 和 hive-server2

systemctl start hive-metastore
systemctl start hive-server2

hive-3.1 集群恢复正常。

9.集群不能使用时间

从停止 hive-metastore 起,到 hive metastore 和 hive server 启动之间,所有的 Hive 任务都不能运行。
执行升级操作时,由于需要改表的结构,或者增加删除索引,可能比较耗费时间。

10. 升级影响

10.1 创建数据库。

升级之后,只能在 hive-3.1 集群创建 hive 数据库。

10.2 事务和锁

现有集群 hive-1.2 配置如下

hive.lock.manager=org.apache.hadoop.hive.ql.lockmgr.zookeeper.ZooKeeperHiveLockManager
hive.support.concurrency=false
hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DummyTxnManager

此配置是没有锁和事务。

hive-3.1.3,事务管理器使用 org.apache.hadoop.hive.ql.lockmgr.DbTxnManager。锁放到 hive 元数据库里,加了锁之后,如果一个任务在写一张表,那么读这个表的任务需要等待。多个任务都读取同一张表没有影响。迁移之后,可能有任务较长时间不执行,等待锁释放。

10.3 集合的分割符

hive 3.1.3 修复了 hive-1.2 的拼写错误的 bug.https://issues.apache.org/jira/browse/HIVE-16922

升级之后测试如下:
数据文件: test_coll.txt

1,a|b
2,c|d

如在 hive-1.2 执行以下语句。

CREATE TABLE test_coll(
 id BIGINT,
 value array<string>
)row format delimited
fields terminated by ','
collection items terminated by '|' stored as textfile;
load data local inpath 'test_coll.txt' overwrite into table test_coll;

在 hive-1.2 检索数据,正常,每个 value 认为有两个元素。

hive> select * from test_coll;
OK
1 ["a","b"]
2 ["c","d"]

在 hive-3.1 中检索数据,结果如下,认为 value 只有一个元素:

hive> select * from test_coll;
OK
1 ["a|b"]
2 ["c|d"]

在升级过程中,升级脚本会替换数据库中错误的值,替换文件 043-HIVE-16922.mysql.sql ,文件内容如下:

UPDATE SERDE_PARAMS
SET PARAM_KEY='collection.delim'
WHERE PARAM_KEY='colelction.delim';

所以在升级之后,带有array 类型的字段的表仅能在 hive-3.1.3 中查询。之后在 hive-1.2 创建的带 array 类型的字段,在 hive-3.1.3 中不能正常解析。

其他信息

hive-1.2 和 hive-3.1 函数不兼容的说明