reizist's blog

ウェブ

mac上にhive+hive metastore on mysql環境を構築する

hadoop & hive on macの環境構築記事結構ちらほらあるんだけど、その通りにやって動かなかったり、信頼に欠けるコメントがついていたりするので改めて自分でまとめる。

hadoop + hive + hive metastore on mysqlを想定してます。

環境

準備

java

$ brew cask install java

$ echo 'export JAVA_HOME=`/usr/libexec/java_home -v 1.8`' >> ~/.zshrc
$ echo 'export PATH="PATH=$JAVA_HOME/bin:$PATH"' >> ~/.zshrc

mysql

$ brew install mysql

hadoop

$ brew install hadoop

$ echo 'export HADOOP_HOME="/usr/local/Cellar/hadoop/2.8.2"' >> ~/.zshrc
$ echo 'export HADOOP_CONF_DIR="$HADOOP_HOME/libexec/etc/hadoop"' >> ~/.zshrc

$HADOOP_CONF_DIR 上で2つの設定ファイルを追加

  • core-site.xml
<configuration>
    <property>
        <name>fs.defaultFS</name>
        <value>hdfs://localhost:9000</value>
    </property>
</configuration>
<configuration>
    <property>
        <name>dfs.name.dir</name>
        <value>/var/lib/hdfs/name</value>
    </property>
    <property>
        <name>dfs.data.dir</name>
        <value>/var/lib/hdfs/data</value>
    </property>
    <property>
        <name>dfs.replication</name>
        <value>1</value>
    </property>
</configuration>

hive

$ brew install hive
$ echo 'export HIVE_HOME="/usr/local/Cellar/hive/2.3.1"' >> ~/.zshrc
$ echo 'export HIVE_CONF_DIR="$HIVE_HOME/libexec/conf"' >> ~/.zshrc

$HIVE_CONF_DIR 上で1つの設定ファイルを追加( cp hive-default.xml.template hive-site.xml としても可)

  • hive-site.xml
<property>
  <name>javax.jdo.option.ConnectionURL</name>
  <value>jdbc:mysql://localhost/hive_metastore</value>
</property>
<property>
  <name>javax.jdo.option.ConnectionDriverName</name>
  <value>com.mysql.jdbc.Driver</value>
</property>
<property>
  <name>javax.jdo.option.ConnectionUserName</name>
  <value>[mysql上に構築するhive metastore用dbにつなぐユーザー名 e.g. hive]</value>
</property>
<property>
  <name>javax.jdo.option.ConnectionPassword</name>
  <value>[mysql上に構築するhive metastore用dbにつなぐユーザー名 e.g. password]</value>
</property>
<property>
    <name>hive.exec.local.scratchdir</name>
    <value>/tmp/hive</value>
    <description>Local scratch space for Hive jobs</description>
</property>
<property>
    <name>hive.downloaded.resources.dir</name>
    <value>/tmp/hive</value>
    <description>Temporary local directory for added resources in the remote file system.</description>
</property>
<property>
    <name>hive.querylog.location</name>
    <value>/tmp/hive</value>
    <description>Location of Hive run time structured log file</description>
</property>

hive metastore接続用JDBCドライバを配置

$ curl -O https://cdn.mysql.com/Downloads/Connector-J/mysql-connector-java-5.1.44.tar.gz
$ tar xvzf mysql-connector-java-5.1.44.tar.gz
$ mv mysql-connector-java-5.1.44/mysql-connector-java-5.1.44-bin.jar $HIVE_HOME/lib

localhostへのssh

システム環境設定→共有→リモートログインをON

設定

hadoop hdfsフォーマット

$ hdfs namenode -format

mysqlにhive metastore用環境準備

  • FAQによれば、 When using MySQL as a metastore I see the error "com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Specified key was too long; max key length is 767 bytes". に対する回避策としてcharacter setに latin1 を使うパターンを記載しています。手っ取り早いのでこれで対応
$ mysql -u root
mysql> create database hive_metastore default character set 'latin1';
mysql> use hive_metastore;
mysql> create user 'hive'@'localhost' identified by 'password';

metastore用のテーブル初期化作成

$ schematool -dbType mysql -initSchema -verbose

Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClient が出る場合はmysqlにmetastoreテーブルが作られているか確認する

$ mysql -u hive -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 836
Server version: 5.7.19 Homebrew

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use hive_metastore;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;

hive側からmetastoreにアクセスできればOK

$ hive
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/usr/local/Cellar/hive/2.3.1/libexec/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/local/Cellar/hadoop/2.8.2/libexec/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
show tables;

Logging initialized using configuration in jar:file:/usr/local/Cellar/hive/2.3.1/libexec/lib/hive-common-2.3.1.jar!/hive-log4j2.properties Async: true
Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
hive> show tables;
OK
Time taken: 3.35 seconds
hive>

何かしらのエラーが発生する場合

hive -hiveconf hive.root.logger=DEBUG,console

としてログを見る。

hiveの細かいプロパティはConfiguration Properties 参照。例えば datanucleus.schema.autoCreateAll (hive2.0以降は datanucleus.autoCreateSchema ではない)を有効にすると初回起動時のメタストア作成を自動的に行なってくれる。