highlight

2017年3月2日木曜日

Hive を Ubuntu 16.10 にインストールする

方針

Hadoop はインストール済み、という前提

  • /opt/hive を $HIVE_HOME とする
  • Hive 用ユーザ hive を作成する
  • metastore には、mariadb を利用する
  • Hive ユーザ認証は取り敢えず設定しない。つまり NOSASL

hive ユーザを作る

uid, gid は適当に決めれば良い

groupadd -g 10011 hive
useradd -c "Hive Admin" -d /var/lib/hive -g hive -p hive -s /usr/sbin/nologin -u 10011 hive

/opt/hive を準備する

アーカイブを展開して、hive-site.xml, hive-env.sh, log4j2.properties ファイルを書き換えて、/var に hive 用のディレクトリを作成する。

cd /opt
tar xvzf /tmp/apache-hive-2.1.1-bin.tar.gz
ln -sf apache-hive-2.1.1-bin hive
cd hive/conf
vi hive-site.xml
cp hive-env.sh.template hive-env.sh
vi hive-env.sh # uncomment HADOOP_HOME
cp hive-log4j2.properties.template hive-log4j2.properties
vi hive-log4j2.properties
### property.hive.log.dir = /var/log/hive

mkdir /var/lib/hive
chown hive:hive /var/lib/hive
mkdir /opt/hive/logs
chown hive:hive /opt/hive/logs
cd /var/log
ln -sf /opt/hive/logs hive

hive-site.xml は以下のようなファイル内容にしておいた

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
 <property>
  <name>javax.jdo.option.ConnectionURL</name>
  <value>jdbc:mysql://localhost/hive_metas?useSSL=false</value>
 </property>

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

 <property>
  <name>javax.jdo.option.ConnectionUserName</name>
  <value>hive</value>
 </property>

 <property>
  <name>javax.jdo.option.ConnectionPassword</name>
  <value>hive</value>
 </property>

 <property>
  <name>hive.server2.authentication</name>
  <value>NOSASL</value>
 </property>

 <property>
  <name>hive.server2.enable.doAs</name>
  <value>false</value>
 </property>

 <property>
  <name>hive.metastore.sasl.enabled</name>
  <value>false</value>
 </property>

 <property>
  <name>datanucleus.autoCreateSchema</name>
  <value>false</value>
 </property>

 <property>
  <name>datanucleus.fixedDataStore</name>
  <value>true</value>
 </property>

 <property>
  <name>hive.metastore.uris</name>
  <value>thrift://127.0.0.1:9083</value>
 </property>
</configuration>

mariadb をインストールする

データベース本体と jdbc ドライバのパッケージをインストールする。metastore 用データベースを create しておく。

apt install mariadb-server libmysql-java

sudo mysql

#mariadb

SELECT user,host FROM mysql.user;
CREATE USER 'hive'@'localhost' IDENTIFIED BY 'hive';
CREATE DATABASE hive_metas;
USE hive_metas;
GRANT ALL PRIVILEGES ON hive_metas.* TO 'hive'@'localhost';
FLUSH PRIVILEGES;

Hadoop HDFS のセットアップ

sudo su - hadoop
hdfs dfs -mkdir /tmp
hdfs dfs -chmod g+w /tmp
hdfs dfs -mkdir /tmp/hive
hdfs dfs -chown hive:hive /tmp/hive
hdfs dfs -chmod 777 /tmp/hive
hdfs dfs -mkdir /user/hive
hdfs dfs -mkdir /user/hive/warehouse
hdfs dfs -chmod g+w /user/hive/warehouse
hdfs dfs -chown -R hive:hive /user/hive

Hive metastore をセットアップ

mysql jdbc ドライバを hive が参照できるようにして、hive metastore データベースの初期化を行う

cd /opt/hive
mkdir auxlib
cd auxlib
ln -sf /usr/share/java/mysql.jar .
schematool -dbType mysql -initSchema -userName hive -passWord hive -verbose
schematool -dbType mysql -info -passWord hive # kakunin

systemd の service ファイルを作成

Hive サービスの自動起動設定

mkdir /opt/hive/etc
vi /opt/hive/etc/hive-metastore.service
[Unit]
Description=Hive Metastore Service
After=hadoop.service
Requires=hadoop.service mysql.service

[Service]
ExecStartPre=/bin/mkdir -p --mode=777 /tmp/hive
ExecStart=/opt/hive/bin/hive --service metastore
WorkingDirectory=/var/lib/hive
User=hive
Group=hive

[Install]
WantedBy=multi-user.target
vi /opt/hive/etc/hiveserver2.service
[Unit]
Description=Hive Server2 Service
After=hive-metastore.service
Requires=hive-metastore.service

[Service]
ExecStart=/opt/hive/bin/hiveserver2
WorkingDirectory=/var/lib/hive
User=hive
Group=hive

[Install]
WantedBy=multi-user.target
cd /etc/systemd/system
ln -sf /opt/hive/etc/hive-metastore.service .
ln -sf /opt/hive/etc/hiveserver2.service .
systemctl start hive-metastore.service
systemctl status hive-metastore.service
systemctl start hiveserver2.service
systemctl status hiveserver2.service
sudo lsof -i:9083 # kakunin
sudo lsof -i:10000 #kakunin

beeline
!connect jdbc:hive2://localhost:10000/default;auth=noSasl

ハマるポイント

幾つかハマるところがあった。

ローカルファイルシステムの /tmp/hive パーミッション

Spark (spark-shell)を実行すると、The root scratch dir: /tmp/hive on HDFS should be writable. Current permissions are: rwxr-xr-x というエラーが出て spark session が取得できない。HDFS のパーミッションは hdfs dfs -ls /tmp で確認しても

drwxrwxrwx     - hadoop hadoop      0 2017-02-30 10:10 /tmp/hive

777 やんか。-chmmod -R してみたりしたものの、一向に解消しない。結論から言うと、このエラーを出している部分でチェックしているのは、どうやら HDFS ではなくて、ローカルファイルシステムの /tmp/hive のようだ。chmod 777 /tmp/hive とすれば、あっさり正常に起動できた。

/tmp ディレクトリは自動的にファイルが削除され、metastore サービスを起動するときに、また新たに作られるが、そのときに再び 755 で作成されてしまう。 毎回 chmod 777 するわけにも行かないので、hive-site.xml の hive.scratch.dir.permission を 777 にセットしてみたが、/tmp/hive を作るときには 755 になってしまう。仕方なく hive-site.xml で対応するのは諦めて、取り敢えず、service ファイルの ExecStartPre で /tmp/hive を作っておく事にした。 なお、mkdir だと、既に 755 のディレクトリがあった場合には、パーミッションが変わってくれないので、ExecStartPre=/bin/bash -c 'if [ -d /tmp/hive ]; then /bin/chmod 777 /tmp/hive; else /bin/mkdir --mode=777 /tmp/hive; fi' としても良いかもしれない。

hiveserver2 が port を Listen しない

systemd で正常起動しているようにみえるのに、lsof -i:10000 しても、このポートを LISTEN しているプロセスがなかった。 hive コマンドを叩いてみたら、エラーメッセージが表示されて気づいたのだが、hdfs の /user/hive に hive ユーザに chown するのを忘れていた。 この場合、hiveserver2 はエラーメッセージを出さずに、異常が発生したまま動作し続けるようだ。 hive cli は deprecated 扱いされているようだが、こういう場合にキチンとエラーを出力してくれるという教訓。

0 件のコメント:

コメントを投稿

スパムフィルタが機能しないようなので、コメント不可にしました。

注: コメントを投稿できるのは、このブログのメンバーだけです。