--- title: "共用 Hive" slug: "df-eco-hive" tags: ["Data Forest", "vpc"] updated: 2026-09-17T09:30:01Z published: 2026-09-17T09:30:01Z canonical: "guide.ncloud-docs.com/df-eco-hive" --- > ## Documentation Index > Fetch the complete documentation index at: https://guide.ncloud-docs.com/llms.txt > Use this file to discover all available pages before exploring further. # 共用 Hive VPC環境で利用できます。 Data Forestで Hiveデータベースとテーブルを作成して使用する方法を説明します。 参考 UDF、Storage Handler機能は準備中です。 ## Hiveコマンドの実行 HiveServerに初めてアクセスする場合は、[Hiveを使用する](/docs/ja/df-hive)をご参考ください。Kerberos認証は[アカウント作成と管理](/docs/ja/df-account)をご参考ください。 [Devアプリ](/docs/ja/df-dev)で、以下のように Hiveコマンドを実行できます。 ``` $ kinit example -kt df.example.keytab # Kerberos 認証 $ beeline -u "jdbc:hive2://zk1.kr.df.naverncp.com:2181,zk2.kr.df.naverncp.com:2181,zk3.kr.df.naverncp.com:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2" -n example -p 'PASSWORD' ``` ## データベースの作成 参考 Data Forestユーザーは Hiveのディフォルト値のデータベースを利用できません。ルールに沿ってユーザーのデータベース新たに作成します。 Data Forestで Hiveデータベースを作成するためには、以下のルールに従います。 - データベース名の形式は`{USER}__db_*`です。ユーザーのアカウント名が`example`である場合、以下のように DB名を指定できます。 ``` example__db_foo example__db_bar example__db_hello_foo example__db_hello_foo_bar ``` - データベース名に`-`(ハイフン)は入力できません。ユーザー名に`-`(ハイフン)が入力されている場合、`_`(アンダースコア)に置換します。ユーザーのアカウント名が`example-ex`である場合、以下のように DB名を指定できます。 ``` example_ex__db_foo ``` このルールに反する DB名の例は、以下のとおりです。 ``` example_db_foo # _(アンダーバー)数を間違った場合 example__DB_foo # dbを大文字で表記している場合 example-ex__db_foo # -(ハイフン)を_(アンダーバー)に直してない場合 ``` - Hiveテーブルデータは HDFSに保存されます。データベースを作成する時は、必ず **LOCATION** 設定でパスを指定します。このパスは、ユーザーのホームディレクトリの下の **warehouse** 下位パスに制限されます。 - まず、ユーザーのホームディレクトリの下位に`warehouse`ディレクトリを作成します。 ``` $ hadoop fs -mkdir warehouse $ hadoop fs -ls Found 4 items ... drwx------ - example hdfs 0 2021-03-29 16:18 warehouse ``` - [Beeline](https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-Beeline%E2%80%93NewCommandLineShell)コマンドで以下のようにデータベースを作成できます。 ``` # データベースの作成 CREATE DATABASE example__db_foo LOCATION '/user/example/warehouse/example__db_foo.db'; # データベースの使用 USE example__db_foo; ``` 注意 Beelineコマンドでデータベースを照会する場合、他のユーザーのデータベースは確認できません。ただし、[Spark SQL](/docs/ja/df-eco-spark#SparkSQL)ではすべてのデータベースリストが返されるため、データベース名に個人情報が含まれないようご注意ください。 ## テーブルの作成 テーブルの作成時にネーミングの制約はありません。`LOCATION`オプションは必須ではありませんが、別途指定したい場合は **/user/example/warehouse** の下位のパスに指定します。 ``` CREATE TABLE `table1` (`id` BIGINT) LOCATION '/user/example/warehouse/table1'; ``` ## コマンドの制限 `SET`、`TRANSFORM`節は使用できません。 専用キューを導入して`SET`コマンドを使用できるようにアップデートする予定です。 ## Spark-Hiveへのアクセスの設定 Sparkで Hiveの Managed Tableデータを読み込むために、LLAPを使用します。Interactive Hiveを用いて Hiveにアクセスするように設定する方法を説明します。 ### アクセス情報の確認 設定できるアクセス情報は、以下のとおりです。 | Property | Value | | --- | --- | | spark.sql.hive.hiveserver2.jdbc.url (Interactive) | jdbc:hive2://zk1.kr.df.naverncp.com:2181,zk2.kr.df.naverncp.com:2181,zk3.kr.df.naverncp.com:2181/;serviceDiscoveryMode=zooKeeperHA;zooKeeperNamespace=df-interactive | | spark.sql.hive.hiveserver2.jdbc.url (Batch) | jdbc:hive2://zk1.kr.df.naverncp.com:2181,zk2.kr.df.naverncp.com:2181,zk3.kr.df.naverncp.com:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2 | | spark.datasource.hive.warehouse.metastoreUri | thrift://hms1.kr.df.naverncp.com:9083,thrift://hms2.kr.df.naverncp.com:9083 | | spark.datasource.hive.warehouse.load.staging.dir | /user/ **{dataforestアカウント名}** /tmp | | spark.hadoop.hive.zookeeper.quorum | zk1.kr.df.naverncp.com:2181,zk2.kr.df.naverncp.com:2181,zk3.kr.df.naverncp.com:2181 | | spark.hadoop.hive.llap.daemon.service.hosts | @df-llap | | principal | **{dataforestアカウント名}** @KR.DF.NAVERNCP.COM | | keytab | **{dataforesアカウントの keytabファイルのパス}** | | jars | **{hwc jarファイルのパス}** | - 表の内容から、`spark.sql.hive.hiveserver2.jdbc.url`に interactiveと batchの2種類の Connection Stringが存在することが確認できます。 - Interactive Hiveでタスクを実行するために、interactive Connection Stringを使用します。Sparkシェルや PySparkでも同じ設定値を使用してタスクを実行できます。 - principalキータブは Spark Executorを YARNで実行するための設定として、Data Forestの Kerberosアカウントを使用します。 - `spark.sql.hive.hiveserver2.jdbc.url`の userと passwordは、Executor内で Interactive Hiveにアクセスするための認証情報です。Data Forestのアカウント名(Kerberos IDと同一)と Rangerパスワードを使用します。 ### Interactive Hiveでタスクを実行する例 Spark-shellを用いて Interactive Hiveでタスクを実行する例は、以下のとおりです。 ``` spark-shell \ --master local[2] \ --conf spark.sql.hive.hiveserver2.jdbc.url="jdbc:hive2://zk1.kr.df.naverncp.com:2181,zk2.kr.df.naverncp.com:2181,zk3.kr.df.naverncp.com:2181/;serviceDiscoveryMode=zooKeeperHA;zooKeeperNamespace=df-interactive" \ --conf spark.datasource.hive.warehouse.metastoreUri="thrift://hms1.kr.df.naverncp.com:9083,thrift://hms2.kr.df.naverncp.com:9083" \ --conf spark.datasource.hive.warehouse.load.staging.dir=/user/example/tmp \ --conf spark.hadoop.hive.zookeeper.quorum="zk1.kr.df.naverncp.com:2181,zk2.kr.df.naverncp.com:2181,zk3.kr.df.naverncp.com:2181" \ --conf spark.hadoop.hive.llap.daemon.service.hosts=@df-llap \ --conf spark.security.credentials.hiveserver2.enabled=false \ --conf spark.sql.hive.hiveserver2.jdbc.url.principal="hive/_HOST@KR.DF.NAVERNCP.COM" \ --principal example@KR.DF.NAVERNCP.COM \ --keytab ./df.example.keytab \ --jars /usr/hdp/current/hive_warehouse_connector/hive-warehouse-connector-assembly-1.0.0.3.1.0.0-78.jar ``` アクセスが完了すると、以下のような画面が表示されます。 ``` Setting default log level to "WARN". To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel). Spark context Web UI available at http://ambari1.kr.df.naverncp.com:4040 Spark context available as 'sc' (master = local[2], app id = local-1624438130183). Spark session available as 'spark'. Welcome to ____ __ / __/__ ___ _____/ /__ _\ \/ _ \/ _ `/ __/ '_/ /___/ .__/\_,_/_/ /_/\_\ version 2.3.2.3.1.0.0-78 /_/ Using Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_112) Type in expressions to have them evaluated. Type :help for more information. scala> import com.hortonworks.hwc.HiveWarehouseSession import com.hortonworks.hwc.HiveWarehouseSession scala> import com.hortonworks.hwc.HiveWarehouseSession._ import com.hortonworks.hwc.HiveWarehouseSession._ scala> val hive1 = HiveWarehouseSession.session(spark).build() hive1: com.hortonworks.spark.sql.hive.llap.HiveWarehouseSessionImpl = com.hortonworks.spark.sql.hive.llap.HiveWarehouseSessionImpl@3da61af2 ```