How to use Hive Metastore with MySQL (for Thrift Server or spark-shell)?

How to use Hive Metastore with MySQL (for Thrift Server or spark-shell)?

我正在使用 Apache Spark 2.1.1,我想使用外部 Hive 元存储(专门针对 Spark Thrift 服务器)进行设置。

我已将 hive-site.xml 添加到 $SPARK_HOME/conf 文件夹,如下所示:

<?xml version="1.0"?>
<configuration>
  <property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://home.cu:3306/hive_metastore?createDatabaseIfNotExist=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=Europe/Berlin&amp;nullNamePatternMatchesAll=true </value>
    <description>JDBC connect string for a JDBC metastore</description>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionDriverName</name>
    <value>com.mysql.jdbc.Driver</value>
    <description>Driver class name for a JDBC metastore</description>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>hive</value>
    <description>username to use against metastore database</description>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>hive</value>
    <description>password to use against metastore database</description>
  </property>
  <property>
    <name>hive.metastore.schema.verification</name>
    <value>false</value>
    <description>password to use against metastore database</description>
  </property>

  <property>
    <name>hive.metastore.warehouse.dir</name>
    <value>hdfs://spark-master.cu:9000/value_iq/hive_warehouse/</value>
    <description>Warehouse Location</description>
  </property>
</configuration>

每当我尝试 运行 spark-shell 或 Spark Thrift Server 时,他们都会尝试在 MySQL 上创建 Hive 元存储(因为还没有元存储)并且它们失败并显示以下内容错误:

17/07/13 19:57:55 ERROR Datastore: Error thrown executing ALTER TABLE `PARTITIONS` ADD COLUMN `TBL_ID` BIGINT NULL : Table 'hive_metastore.partitions' doesn't exist
java.sql.SQLSyntaxErrorException: Table 'hive_metastore.partitions' doesn't exist
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:536)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115)
        at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1983)
        at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1936)
        at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:891)
        at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:795)
        at com.jolbox.bonecp.StatementHandle.execute(StatementHandle.java:254)
        at org.datanucleus.store.rdbms.table.AbstractTable.executeDdlStatement(AbstractTable.java:760)
        at org.datanucleus.store.rdbms.table.AbstractTable.executeDdlStatementList(AbstractTable.java:711)
        at org.datanucleus.store.rdbms.table.TableImpl.validateColumns(TableImpl.java:259)
        at org.datanucleus.store.rdbms.RDBMSStoreManager$ClassAdder.performTablesValidation(RDBMSStoreManager.java:3393)
        at org.datanucleus.store.rdbms.RDBMSStoreManager$ClassAdder.addClassTablesAndValidate(RDBMSStoreManager.java:3190)
        at org.datanucleus.store.rdbms.RDBMSStoreManager$ClassAdder.run(RDBMSStoreManager.java:2841)
        at org.datanucleus.store.rdbms.AbstractSchemaTransaction.execute(AbstractSchemaTransaction.java:122)
        at org.datanucleus.store.rdbms.RDBMSStoreManager.addClasses(RDBMSStoreManager.java:1605)
        at org.datanucleus.store.AbstractStoreManager.addClass(AbstractStoreManager.java:954)
        at org.datanucleus.store.rdbms.RDBMSStoreManager.getDatastoreClass(RDBMSStoreManager.java:679)
        at org.datanucleus.store.rdbms.query.RDBMSQueryUtils.getStatementForCandidates(RDBMSQueryUtils.java:408)
        at org.datanucleus.store.rdbms.query.JDOQLQuery.compileQueryFull(JDOQLQuery.java:947)
        at org.datanucleus.store.rdbms.query.JDOQLQuery.compileInternal(JDOQLQuery.java:370)
        at org.datanucleus.store.query.Query.executeQuery(Query.java:1744)
        at org.datanucleus.store.query.Query.executeWithArray(Query.java:1672)
        at org.datanucleus.store.query.Query.execute(Query.java:1654)
        at org.datanucleus.api.jdo.JDOQuery.execute(JDOQuery.java:221)

我认为你的仓库目录 属性 配置不正确,它应该是 HDFS 上的路径

<configuration>
<property>
    <name>hive.metastore.uris</name>
    <value>thrift://maprdemo:9083</value>
</property>
<property>
    <name>hive.metastore.warehouse.dir</name>
    <value>/user/hive/warehouse</value>
</property>

我发现了问题,它与 MySQL 驱动程序有关,我使用的是 mysql-connector-java-6.0.6-bin.jar,我已将其替换为旧驱动程序 mysql-connector-java-5.1.23-bin.jar,现在可以使用了。

我已经尝试使用不同版本的 spark 将元数据管理从 derby 迁移到 mysql/postgres,并且我成功地使用了 spark-2.2.1。之前的版本不允许我迁移到其他数据库。这纯粹是他们对 spark 的版本依赖。我的建议是尝试将 spark 版本升级到 2.2.1,你应该可以开始了!

对于所有尝试将 hive metastore 与 mysql 驱动程序 8.x 一起使用并降级到 5.x 的人有帮助:

另一种解决方案是使用 mysql 选项配置 URL:

connectionURL: "jdbc:mysql://localhost:3306/hive_db?databaseTerm=SCHEMA&nullDatabaseMeansCurrent=true"

From release notes of mysql connector 8

新连接 属性,databaseTerm,设置应用程序中使用这两个术语中的哪一个来引用数据库。 属性 采用两个值 CATALOG 或 SCHEMA 之一,并使用它来确定哪些连接方法可用于 set/get 当前数据库,哪些参数可在各种 DatabaseMetaData 方法中用于过滤结果, DatabaseMetaData 方法返回的 ResultSet 中的哪些字段包含数据库标识信息。有关详细信息,请参阅配置属性中的 databaseTerm 条目。 此外,连接 属性 nullCatalogMeansCurrent 已重命名为 nullDatabaseMeansCurrent。旧名称仍然是连接的别名 属性。 感谢 Harald Aamot 对补丁的贡献。 (错误 #11891000、错误 #27356869、错误 #89133)

对于感兴趣的人:我们看到的问题是 hive 试图解析 mysql 中的所有模式,但它没有这样做的权限。将其限制为当前数据库(URL 中指定的hive_db)为我们解决了问题