メインコンテンツまでスキップ

外部 Apache Hive metastore (レガシー)

important

このドキュメントは廃止されており、更新されない可能性があります。

注記

外部メタストアの使用は、従来のデータガバナンス モデルです。 Databricks では、Unity Catalog にアップグレードすることをお勧めします。 Unity Catalog は、アカウント内の複数のワークスペースにわたるデータアクセスを一元的に管理および監査するための場所を提供することで、データのセキュリティとガバナンスを簡素化します。 「Unity Catalog とは」を参照してください。

この記事では、既存の外部メタストアに接続するように Databricks クラスターを設定する方法について説明します Apache Hive 。 メタストアのデプロイ モード、推奨されるネットワーク設定、およびクラスターの構成要件に関する情報を提供し、その後、外部メタストアに接続するためのクラスターの構成手順を示します。 に含まれる Hiveライブラリバージョンについては、関連する バージョンDatabricks Runtime Databricks Runtimeリリースノート を参照してください。

important
  • Azure Database for MySQL を外部メタストアとして使用する場合は、サーバー側のデータベース構成で lower_case_table_names プロパティの値を 1 (デフォルト) から 2 に変更する必要があります。詳細については、「 識別子の大文字と小文字の区別」を参照してください。
  • 読み取り専用のメタストア データベースを使用する場合は、パフォーマンスを向上させるために Databricks 、クラスターで spark.databricks.delta.catalog.update.enabledfalse に設定することを強くお勧めします。

Hive metastore 展開モード

本番運用環境では、ローカルとリモートの 2 つのモードで Hive metastore をデプロイできます。

ローカルモード

クラスター内で実行されているメタストア クライアントは、 JDBCを介して基になるメタストア データベースに直接接続します。

リモートモード

メタストア クライアントは、基になるデータベースに直接接続する代わりに、Thrift プロトコルを介して別のメタストア サービスに接続します。 メタストア サービスは、基になるデータベースに接続します。 メタストアをリモート モードで実行する場合、DBFS はサポートされません

これらのデプロイ モードの詳細については、 Hive のドキュメントを参照してください。

注記

このドキュメントの例では、基になるメタストア データベースとして MySQL を使用しています。

ネットワークのセットアップ

Databricks クラスターは、仮想プライベート クラウド (VPC) 内で実行されます。 Hive metastore新しい 内に外部VPC を設定し、これら 2 つの VPC をピアリングして、クラスターがプライベートHive metastore IP アドレスを使用して に接続するようにすることをお勧めします。ピアリング では、VPC クラスターで使用されるVPC DatabricksVPCとメタストアが存在する をピアリングする方法について詳しく説明します。VPC をピアリングした後、ノートブック内で次のコマンドを実行して、クラスターからメタストア VPC へのネットワーク接続をテストできます。

Bash
%sh
nc -vz <DNS name or private IP> <port>

ここでは

  • <DNS name or private IP> は、MySQL データベース (ローカル モードの場合) またはメタストア サービス (リモート モードの場合) の DNS 名またはプライベート IP アドレスです。 ここでDNS名を使用する場合は、解決されたIPアドレスがプライベートアドレスであることを確認してください。
  • <port> は、MySQL データベースのポートまたはメタストア サービスのポートです。

クラスター構成

クラスターを外部メタストアに接続するには、次の 3 つの構成オプションのセットを設定する必要があります。

  • SparkSparkHive metastoreオプションは 、メタストア クライアントの バージョンと JAR を使用して を構成します。
  • Hive オプションは 、外部メタストアに接続するようにメタストア クライアントを構成します。
  • オプションの Hadoop オプション ・セットは、ファイル・システム・オプションを構成します。

Spark 構成オプション

次のように、spark.sql.hive.metastore.version を Hive metastore とspark.sql.hive.metastore.jarsのバージョンに設定します。

  • Hive 0.13: spark.sql.hive.metastore.jarsを設定しないでください。
注記

Hive 1.2.0 と 1.2.1 は、 Databricks Runtime 7.0 以降の組み込みメタストアではありません。 Hive 1.2.0 または 1.2.1 を Databricks Runtime 7.0 以降と共に使用する場合は、「 メタストア jar をダウンロードしてポイントする」で説明されている手順に従います。

  • Hive 2.3.7 (Databricks Runtime 7.0 - 9.x) または Hive 2.3.9 (Databricks Runtime 10.0 以降): spark.sql.hive.metastore.jarsbuiltinに設定します。

  • 他のすべての Hive バージョンについては、 Databricks メタストア JAR をダウンロードし、 メタストア jar のダウンロードとそれらを指す手順を使用して、ダウンロードした JAR を指すように構成spark.sql.hive.metastore.jarsを設定することをお勧めします。

メタストアの jar ファイルをダウンロードして、それらを指定します

  1. spark.sql.hive.metastore.jarsmaven に設定し、メタストアのバージョンに一致するようにspark.sql.hive.metastore.versionしたクラスターを作成します。

  2. クラスターが実行されている場合は、ドライバー ログを検索し、次のような行を見つけます。

    17/11/18 22:41:19 INFO IsolatedClientLoader: Downloaded metastore jars to <path>

    ディレクトリ <path> は、クラスターのドライバー ノードにダウンロードされた JAR の場所です。

    または、 Scala ノートブックで次のコードを実行して、JARの場所を印刷することもできます。

    Scala
    import com.typesafe.config.ConfigFactory
    val path = ConfigFactory.load().getString("java.io.tmpdir")

    println(s"\nHive JARs are downloaded to the path: $path \n")
  3. %sh cp -r <path> /dbfs/hive_metastore_jarを実行して (<path> をクラスターの情報に置き換えて)、このディレクトリを hive_metastore_jar というルート内のディレクトリにコピーするには DBFSドライバー ノードの DBFS クライアントを介して。

  4. ノードのローカル・ファイル・システムに/dbfs/hive_metastore_jarをコピーするinitスクリプトを作成し、initスクリプトがDBFSクライアントにアクセスする数秒前にスリープ状態になるようにします。これにより、クライアントの準備ができていることが保証されます。

  5. このディレクトリを使用するには、 spark.sql.hive.metastore.jars を設定します。 initスクリプトが /dbfs/hive_metastore_jar/databricks/hive_metastore_jars/にコピーする場合は、 spark.sql.hive.metastore.jars/databricks/hive_metastore_jars/*に設定します。 ロケーションには、末尾の /*を含める必要があります。

  6. クラスターを再起動します。

Hive 構成オプション

このセクションでは、Hive に固有のオプションについて説明します。

ローカルモードの設定オプション

ローカル モードを使用して外部メタストアに接続するには、次の Hive 構成オプションを設定します。

ini
# JDBC connect string for a JDBC metastore
javax.jdo.option.ConnectionURL jdbc:mysql://<metastore-host>:<metastore-port>/<metastore-db>

# Username to use against metastore database
javax.jdo.option.ConnectionUserName <mysql-username>

# Password to use against metastore database
javax.jdo.option.ConnectionPassword <mysql-password>

# Driver class name for a JDBC metastore (Runtime 3.4 and later)
javax.jdo.option.ConnectionDriverName org.mariadb.jdbc.Driver

# Driver class name for a JDBC metastore (prior to Runtime 3.4)
# javax.jdo.option.ConnectionDriverName com.mysql.jdbc.Driver

ここでは

  • <metastore-host> <metastore-port> は MySQL インスタンスのホストとリスニングポートです。
  • <metastore-db> は、すべてのメタストア テーブルを保持する MySQL データベースの名前です。
  • <mysql-username> <mysql-password><metastore-db>への読み取り/書き込みアクセス権を持つ MySQL アカウントのユーザー名とパスワードを指定します。
注記
  • MariaDBドライバーを使用してMySQLデータベースと通信します。
  • 本番運用環境の場合は、 hive.metastore.schema.verificationtrueに設定することをおすすめします。 これにより Hive metastore メタストア クライアントのバージョンがメタストア データベースのバージョンと一致しない場合に、クライアントがメタストア データベース スキーマを暗黙的に変更するのを防ぐことができます。 Hive 1.2.0 より前のメタストア クライアント バージョンでこの設定を有効にする場合は、メタストア クライアントにメタストア データベースへの書き込みアクセス許可があることを確認してください ( HIVE-9749 で説明されている問題を防ぐため)。
    • Hive metastore 1.2.0 以降では、hive.metastore.schema.verification.record.versiontrue に設定して hive.metastore.schema.verification を有効にします。
    • Hive metastore 2.1.1 以降では、hive.metastore.schema.verification.record.versionfalse by デフォルトに設定されているため、true に設定します。

リモートモードの設定オプション

リモート モードを使用して外部メタストアに接続するには、次の Hive 構成オプションを設定します。

ini
# Thrift URI for the remote metastore. Used by metastore client to connect to remote metastore.
hive.metastore.uris thrift://<metastore-host>:<metastore-port>

ここで、<metastore-host><metastore-port> は、 Hive metastore サービスのリッスン ホストとポートです。

ファイル・システム・オプション

インスタンスプロファイルを使用して AssumeRole を設定する場合は、以下を設定する必要があります。

  • fs.s3a.credentialsType - AssumeRole
  • fs.s3a.stsAssumeRole.arn を、引き受けるロールの Amazon リソースネーム (ARN) に設定します。

UI を使用した外部メタストアの設定

Databricks UI を使用して外部メタストアを設定するには:

  1. サイドバーの クラスター ボタンをクリックします。

  2. [クラスターの作成] をクリックします。

  3. 次の Spark 設定オプションを入力します。

    ローカルモード

    ini
    # Hive specific configuration options.
    # spark.hadoop prefix is added to make sure these Hive specific options will propagate to the metastore client.
    spark.hadoop.javax.jdo.option.ConnectionURL jdbc:mysql://<mysql-host>:<mysql-port>/<metastore-db>

    # Driver class name for a JDBC metastore (Runtime 3.4 and later)
    spark.hadoop.javax.jdo.option.ConnectionDriverName org.mariadb.jdbc.Driver

    # Driver class name for a JDBC metastore (prior to Runtime 3.4)
    # spark.hadoop.javax.jdo.option.ConnectionDriverName com.mysql.jdbc.Driver

    spark.hadoop.javax.jdo.option.ConnectionUserName <mysql-username>
    spark.hadoop.javax.jdo.option.ConnectionPassword <mysql-password>

    # Spark specific configuration options
    spark.sql.hive.metastore.version <hive-version>
    # Skip this one if <hive-version> is 0.13.x.
    spark.sql.hive.metastore.jars <hive-jar-source>

    # If you need to use AssumeRole, uncomment the following settings.
    # spark.hadoop.fs.s3a.credentialsType AssumeRole
    # spark.hadoop.fs.s3a.stsAssumeRole.arn <sts-arn>

    リモートモード

    ini
    # Hive specific configuration option
    # spark.hadoop prefix is added to make sure these Hive specific options will propagate to the metastore client.
    spark.hadoop.hive.metastore.uris thrift://<metastore-host>:<metastore-port>

    # Spark specific configuration options
    spark.sql.hive.metastore.version <hive-version>
    # Skip this one if <hive-version> is 0.13.x.
    spark.sql.hive.metastore.jars <hive-jar-source>

    # If you need to use AssumeRole, uncomment the following settings.
    # spark.hadoop.fs.s3a.credentialsType AssumeRole
    # spark.hadoop.fs.s3a.stsAssumeRole.arn <sts-arn>
  4. クラスターの設定を続行するには、 コンピュート設定リファレンスの手順に従います。

  5. 「クラスターの作成 」をクリックして、クラスターを作成します。

initスクリプトを使用した外部メタストアのセットアップ

initスクリプト を使用すると、必要な設定を手動で設定せずに既存の Hive metastore に接続できます。

ローカルモード

  1. initスクリプトが存在しない場合は、保存するベースディレクトリを作成します。 次の例では、 dbfs:/databricks/scripts.
  2. ノートブックで次のスニペットを実行します。 このスニペットは、 Databricks ファイルシステム(DBFS)にinitスクリプト/databricks/scripts/external-metastore.shを作成します。 または、 DBFS REST API put操作 を使用してinitスクリプトを作成することもできます。 このinitスクリプトは、必要な構成オプションを00-custom-spark.confという名前の構成ファイルに、クラスターの各ノード内の/databricks/driver/conf/の下に JSONのような形式で書き込みます。 Databricks は、/databricks/driver/conf/spark-branch.conf ファイルにデフォルトの Spark 構成を提供します。 /databricks/driver/confディレクトリ内の設定ファイルは、アルファベットの逆順に適用されます。00-custom-spark.conf ファイルの名前を変更する場合は、spark-branch.conf ファイルの前に引き続き適用されることを確認してください。
Scala
dbutils.fs.put(
"/databricks/scripts/external-metastore.sh",
"""#!/bin/sh
|# Loads environment variables to determine the correct JDBC driver to use.
|source /etc/environment
|# Quoting the label (i.e. EOF) with single quotes to disable variable interpolation.
|cat << 'EOF' > /databricks/driver/conf/00-custom-spark.conf
|[driver] {
| # Hive specific configuration options for metastores in local mode.
| # spark.hadoop prefix is added to make sure these Hive specific options will propagate to the metastore client.
| "spark.hadoop.javax.jdo.option.ConnectionURL" = "jdbc:mysql://<mysql-host>:<mysql-port>/<metastore-db>"
| "spark.hadoop.javax.jdo.option.ConnectionUserName" = "<mysql-username>"
| "spark.hadoop.javax.jdo.option.ConnectionPassword" = "<mysql-password>"
|
| # Spark specific configuration options
| "spark.sql.hive.metastore.version" = "<hive-version>"
| # Skip this one if <hive-version> is 0.13.x.
| "spark.sql.hive.metastore.jars" = "<hive-jar-source>"
|
| # If you need to use AssumeRole, uncomment the following settings.
| # "spark.hadoop.fs.s3a.credentialsType" = "AssumeRole"
| # "spark.hadoop.fs.s3a.stsAssumeRole.arn" = "<sts-arn>"
|EOF
|
|case "$DATABRICKS_RUNTIME_VERSION" in
| "")
| DRIVER="com.mysql.jdbc.Driver"
| ;;
| *)
| DRIVER="org.mariadb.jdbc.Driver"
| ;;
|esac
|# Add the JDBC driver separately since must use variable expansion to choose the correct
|# driver version.
|cat << EOF >> /databricks/driver/conf/00-custom-spark.conf
| "spark.hadoop.javax.jdo.option.ConnectionDriverName" = "$DRIVER"
|}
|EOF
|""".stripMargin,
overwrite = true
)
  1. クラスターをinitスクリプトで設定します。
  2. クラスターを再起動します。

リモートモード

  1. initスクリプトが存在しない場合は、保存するベースディレクトリを作成します。 次の例では、 dbfs:/databricks/scripts.

  2. ノートブックで次のスニペットを実行します。

    タブ :::タブ-item[Scala]

    Scala
    dbutils.fs.put(
    "/databricks/scripts/external-metastore.sh",
    """#!/bin/sh
    |
    |# Quoting the label (i.e. EOF) with single quotes to disable variable interpolation.
    |cat << 'EOF' > /databricks/driver/conf/00-custom-spark.conf
    |[driver] {
    | # Hive specific configuration options for metastores in remote mode.
    | # spark.hadoop prefix is added to make sure these Hive specific options will propagate to the metastore client.
    | "spark.hadoop.hive.metastore.uris" = "thrift://<metastore-host>:<metastore-port>"
    |
    | # Spark specific configuration options
    | "spark.sql.hive.metastore.version" = "<hive-version>"
    | # Skip this one if <hive-version> is 0.13.x.
    | "spark.sql.hive.metastore.jars" = "<hive-jar-source>"
    |
    | # If you need to use AssumeRole, uncomment the following settings.
    | # "spark.hadoop.fs.s3a.credentialsType" = "AssumeRole"
    | # "spark.hadoop.fs.s3a.stsAssumeRole.arn" = "<sts-arn>"
    |}
    |EOF
    |""".stripMargin,
    overwrite = true
    )

    :::

    タブ-item[Python]

    Python
    contents = """#!/bin/sh

    # Quoting the label (i.e. EOF) with single quotes to disable variable interpolation.
    cat << 'EOF' > /databricks/driver/conf/00-custom-spark.conf
    [driver] {
    # Hive specific configuration options for metastores in remote mode.
    # spark.hadoop prefix is added to make sure these Hive specific options will propagate to the metastore client.
    "spark.hadoop.hive.metastore.uris" = "thrift://<metastore-host>:<metastore-port>"

    # Spark specific configuration options
    "spark.sql.hive.metastore.version" = "<hive-version>"
    # Skip this one if <hive-version> is 0.13.x.
    "spark.sql.hive.metastore.jars" = "<hive-jar-source>"

    # If you need to use AssumeRole, uncomment the following settings.
    # "spark.hadoop.fs.s3a.credentialsType" = "AssumeRole"
    # "spark.hadoop.fs.s3a.stsAssumeRole.arn" = "<sts-arn>"
    }
    EOF"""

    dbutils.fs.put(
    file = "/databricks/scripts/external-metastore.sh",
    contents = contents,
    overwrite = True
    )

    ::: ::::

  3. クラスターをinitスクリプトで設定します。

  4. クラスターを再起動します。

トラブルシューティング

クラスターが開始されない(initスクリプトの設定が正しくないため)

外部メタストアを設定するためのinitスクリプトが原因でクラスターの作成が失敗する場合は、initスクリプトを ログに記録するように設定し、ログを使用してinitスクリプトをデバッグします。

SQL ステートメントのエラー: InvocationTargetException

  • 完全な例外スタック トレースのエラー メッセージ パターン:

    Caused by: javax.jdo.JDOFatalDataStoreException: Unable to open a test connection to the given database. JDBC url = [...]

    外部メタストア JDBC 接続情報が正しく構成されていません。 設定されたホスト名、ポート、ユーザー名、パスワード、および JDBC ドライバー クラス名を確認します。 また、ユーザー名にメタストア データベースにアクセスするための適切な特権があることを確認してください。

  • 完全な例外スタック トレースのエラー メッセージ パターン:

    Required table missing : "`DBS`" in Catalog "" Schema "". DataNucleus requires this table to perform its persistence operations. [...]

    外部メタストア データベースが正しく初期化されていません。 メタストア データベースを作成したことを確認し、JDBC 接続文字列に正しいデータベース名を入力します。 次に、次の 2 つの Spark 構成オプションを使用して新しいクラスターを開始します。

    ini
    datanucleus.schema.autoCreateTables true
    datanucleus.fixedDatastore false

    このように、Hive クライアント ライブラリは、メタストア データベース内のテーブルにアクセスしようとして存在しないテーブルを見つけた場合、自動的にテーブルの作成と初期化を試みます。

SQLステートメントのエラー: AnalysisException: org.apache.hadoop をインスタンス化できません。Hive metastore。ハイブメタストアクライアント

完全な例外スタックトレースのエラーメッセージ:

The specified datastore driver (driver name) was not found in the CLASSPATH

クラスターは、正しくない JDBC ドライバーを使用するように構成されています。

このエラーは、 Runtime 3.4 以降を使用するクラスターが MariaDB ドライバーではなく MySQL を使用するように構成されている場合に発生する可能性があります。

datanucleus.autoCreateSchema の設定 true に設定すると、期待どおりに機能しません

デフォルトでは、Databricks は datanucleus.fixedDatastoretrueに設定し、メタストアデータベースへの偶発的な構造変更を防ぎます。 したがって、Hive クライアント ライブラリは、 datanucleus.autoCreateSchematrueに設定してもメタストア テーブルを作成できません。 この戦略は、一般に、メタストア データベースが誤ってアップグレードされるのを防ぐため、本番運用環境にとって安全です。

datanucleus.autoCreateSchema を使用してメタストア データベースを初期化する場合は、datanucleus.fixedDatastorefalseに設定してください。また、メタストア データベースを初期化した後で両方のフラグを反転して、本番運用環境の保護を強化することもできます。

com.amazonaws.AmazonClientException:XMLReaderを作成するためのSAXドライバーを初期化できませんでした

この例外は、クラスターのバージョンが 2.1.1-db5 の場合にスローされる可能性があります。 この問題は 2.1.1-db6 で修正されています。 2.1.1-db5 の場合、 この問題は、 spark.driver.extraJavaOptionsspark.executor.extraJavaOptionsの設定の一部として次の JVM プロパティを設定することで修正できます。

ini
-Djavax.xml.datatype.DatatypeFactory=com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl
-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
-Djavax.xml.validation.SchemaFactory:https://www.w3.org/2001/XMLSchema=com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory
-Dorg.xml.sax.driver=com.sun.org.apache.xerces.internal.parsers.SAXParser
-Dorg.w3c.dom.DOMImplementationSourceList=com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl