2018年5月31日木曜日

VagrantでApache SupersetとPostgreSQLをインストールした仮想マシン(Debian Stretch/9.4)を構築する

Apache SupersetはPython製のデータ可視化ツールです。

○Apache Supersetの画面


○構築方法
以下のVagrantfileを使用して、Apache SupersetとPostgreSQLをインストールした仮想マシン(Debian Stretch/9.4)を構築する事ができます。
仮想マシンが構築された後、ブラウザからhttp://192.168.55.107:8088/にアクセスします。
デフォルトユーザ名はadmin、パスワードもadminです。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.4"
  config.vm.hostname = "db94supersetpg"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db94supersetpg"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.107", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.107", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get -y install task-japanese
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
update-locale LANG=ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade


# install postgresql
apt-get -y install postgresql-9.6
echo "listen_addresses='*'" >> /etc/postgresql/9.6/main/postgresql.conf
echo "standard_conforming_strings=off" >> /etc/postgresql/9.6/main/postgresql.conf

#sed -i 's/host.*all.*all.*127.0.0.1/#host    all             all             127.0.0.1/g' /etc/postgresql/9.6/main/pg_hba.conf

echo "host    all         all         127.0.0.1/32          password" >> /etc/postgresql/9.6/main/pg_hba.conf
echo "host    all         all         192.168.1.0/24          password" >> /etc/postgresql/9.6/main/pg_hba.conf
echo "host    all         all         192.168.55.0/24          password" >> /etc/postgresql/9.6/main/pg_hba.conf

su - postgres << EOF
createdb -T template0 --locale=ja_JP.UTF-8 --encoding=UTF8 test
psql -c "
alter user postgres with password 'postgres';
create user test with password 'test';
grant all privileges on database test to test;
"
EOF
echo "postgres:postgres" | chpasswd
systemctl restart postgresql.service

# install anaconda
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
chmod +x Anaconda3-5.1.0-Linux-x86_64.sh
./Anaconda3-5.1.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip

# install dependencies
apt-get -y install build-essential libssl-dev libffi-dev python-dev python-pip libsasl2-dev libldap2-dev

# install psycopg2
apt-get -y install libpq-dev python-dev
pip install psycopg2-binary

pip install --upgrade setuptools
pip install superset
pip install cryptography --upgrade
mkdir -p /opt/superset
cd /opt/superset
fabmanager create-admin --app superset --username admin --firstname admin --lastname user --email admin@localhost.localdomain --password admin
superset db upgrade
superset load_examples
superset init
superset runserver -a 0.0.0.0 &
echo 'access http://192.168.55.107:8088/'
echo 'user:admin, password: admin'

SHELL
end

○データソースの追加
同じ仮想マシンにインストールされたPostgreSQLに接続するには、以下の画面のようにSQLAlchemy URIにpostgresql+psycopg2://test:test@localhost/testを指定します。



○関連情報
・psycopg2に関する他の記事はこちらを参照してください。

DockerでBookStack、MySQLがインストールされたコンテナ(Alpine 3.7)を構築する

BookStackはブック単位でページで管理できるPHP製のWikiです。

○BookStackの画面

ブラウザからhttp://<dokcerホスト>にアクセスします。デフォルトユーザ/パスワードはadmin@admin.com/passwordです。

○構築方法
以下の手順で、BookStack、MySQLのコンテナを構築・実行します。

1. BookStackイメージの作成(Dockerfileがあるフォルダで実行)
docker build -t bookstack .

Dockerfile
FROM alpine:3.7
WORKDIR /
RUN  apk update \
  && apk add --no-cache apache2 php7 php7-phar php7-simplexml php7-fileinfo php7-xmlwriter php7-tokenizer php7-ctype php7-apache2 php7-mysqli php7-mbstring php7-session php7-xml php7-dom php7-gd php7-zip php7-zip php7-json php7-pdo php7-pdo_mysql php7-tidy openssl openrc unzip git curl mysql-client \
  && rm -rf /var/cache/apk/* \
  && curl -s https://getcomposer.org/installer | php \
  && mv composer.phar /usr/local/bin/composer
WORKDIR /opt
RUN git clone https://github.com/BookStackApp/BookStack.git --branch release --single-branch
WORKDIR /opt/BookStack
RUN /usr/local/bin/composer install \
  && mkdir -p /run/apache2 \
  && cp .env.example .env \
  && sed -i -e 's/DB_HOST=localhost/DB_HOST=db/' /opt/BookStack/.env \
  && sed -i -e 's/DB_DATABASE=database_database/DB_DATABASE=bookstack/' /opt/BookStack/.env \
  && sed -i -e 's/DB_USERNAME=database_username/DB_USERNAME=root/' /opt/BookStack/.env \
  && sed -i -e 's/DB_PASSWORD=database_user_password/DB_PASSWORD=bookstack/' /opt/BookStack/.env \
  && chown -R apache:apache /opt/BookStack \
  && sed -i -e 's#DocumentRoot "/var/www/localhost/htdocs"#DocumentRoot "/opt/BookStack/public"#' /etc/apache2/httpd.conf \
  && echo $'<Directory /opt/BookStack/public>\n\
  AllowOverride All\n\
  Require all granted\n\
</Directory>' >> /etc/apache2/httpd.conf \
  && sed -i -e 's|#LoadModule rewrite_module modules/mod_rewrite.so|LoadModule rewrite_module modules/mod_rewrite.so|' /etc/apache2/httpd.conf \
  && echo $'#!/bin/sh \n\
if [ -f /opt/BookStack/cinit ]; then \n\
  echo "ok." \n\
else \n\
  php artisan key:generate --force  \n\
  until mysqladmin ping -h db -P 3306 --silent; do echo -n "." && sleep 1; done \n\
  php artisan migrate --force  \n\
  touch /opt/BookStack/cinit \n\
fi \n\
/usr/sbin/httpd -D FOREGROUND' >> /opt/BookStack/init.sh \
  && chmod +x /opt/BookStack/init.sh
EXPOSE 80
VOLUME /opt/BookStack
CMD ["/opt/BookStack/init.sh"]

2. BookStackとMySQLコンテナの構築・実行(docker-compose.ymlがあるフォルダで実行)
docker-compose up -d

docker-compose.yml
version: "2"
services:
  db:
    image: mysql:5.7
    container_name: db
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    environment:
      MYSQL_DATABASE: bookstack
      MYSQL_ROOT_PASSWORD: bookstack
    ports:
      - "3306:3306"
    volumes:
      - db-data:/var/lib/mysql
  bookstack:
    image: bookstack
    container_name: "bookstack"
    volumes:
      - "bookstack-data:/opt/BookStack"
    ports:
      - "80:80"
    depends_on:
      - db
volumes:
  db-data:
    driver: local
  bookstack-data:
    driver: local


○関連情報
・BookStackに関する他の記事はこちらを参照してください。

VagrantでApache Ignite2.4.0をインストールした仮想マシン(Debian Stretch/9.4)を構築する

Apache Igniteはメモリ指向の分散データベースです。

○構築方法
以下のVagrantfileを使用して、Apache Ignite2.4.0をインストールした仮想マシン(Debian Stretch/9.4)を構築する事が出来ます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.4"
  config.vm.hostname = "db94ignite240"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db94ignite240"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.106", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.106", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get -y install task-japanese
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
update-locale LANG=ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

# install java
apt-get -y install openjdk-8-jdk

apt-get -y install unzip
wget http://ftp.meisei-u.ac.jp/mirror/apache/dist//ignite/2.4.0/apache-ignite-fabric-2.4.0-bin.zip
unzip apache-ignite-fabric-2.4.0-bin.zip
mv apache-ignite-fabric-2.4.0-bin /opt/ignite

cat << EOF > /etc/systemd/system/ignite.service
[Unit]
Description=Apache Ignite

[Service]
ExecStart=/opt/ignite/bin/ignite.sh
WorkingDirectory=/opt/ignite
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
systemctl enable ignite.service
systemctl start ignite.service

# 11211ポートでlistenするまで待つ.
while netstat -lnt | awk '$4 ~ /:11211$/ {exit 1}'; do sleep 10; done
sleep 10

# サンプルSQLの実行
wget http://wiki.metawerx.net/attach/SJSQL/sjsql.java
wget http://wiki.metawerx.net/attach/SJSQL/sjsql.class

cat << EOF >> /home/vagrant/test.sql
create table test1 (test_id integer primary key, test_desc varchar(100));
insert into test1 (test_id, test_desc) values (1, 'test_id_001');
select * from test1;
EOF
java -cp .:/opt/ignite/libs/ignite-core-2.4.0.jar sjsql org.apache.ignite.IgniteJdbcThinDriver jdbc:ignite:thin://127.0.0.1/ test test /home/vagrant/test.sql

SHELL
end

○関連情報
・Apache Igniteに関する他の記事はこちらを参照してください。

2018年5月30日水曜日

VagrantでJupyter LabとHDFS/Hive/Sparkをインストールした仮想マシン(Ubuntu16.04)を構築する

Jupyter Labでインタラクティブなコンピューティング環境を提供する事ができます。AmbariでHDFS/Hive/Sparkをインストールした1ノードクラスタを構築します。
pysparkを使用して、JupyterからSparkに接続します。

〇Jupyter Labの画面


〇構築方法
以下のVagrantfileで、Jupyter LabとHiveをインストールした仮想マシン(Ubuntu16.04)を構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604jupyterspark.vm.internal"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604jupyterspark.vm.internal"
     vbox.cpus = 4
     vbox.memory = 10240
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  # private network
config.vm.network "private_network", ip: "192.168.55.117", :netmask => "255.255.255.0"
  # bridge netwrok
config.vm.network "public_network", ip: "192.168.1.117", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
echo "192.168.55.117 ub1604jupyterspark" >> /etc/hosts
apt-get -y install curl

cd /root
mkdir ./.ssh
ssh-keygen -f ./.ssh/id_rsa -t rsa -N ''

# copy private key
cp -f ./.ssh/id_rsa /vagrant
cat ./.ssh/id_rsa.pub >> ./.ssh/authorized_keys
chmod 600 ./.ssh/authorized_keys

# install and configure ambari server
wget -O /etc/apt/sources.list.d/ambari.list http://public-repo-1.hortonworks.com/ambari/ubuntu16/2.x/updates/2.6.2.0/ambari.list
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com B9733A7A07513CAD
apt-get update

# install postgresql
apt-get -y install postgresql
echo "listen_addresses='*'" >> /etc/postgresql/9.5/main/postgresql.conf

sed -i 's/host.*all.*all.*127.0.0.1/#host    all             all             127.0.0.1/g' /etc/postgresql/9.5/main/pg_hba.conf

echo "host    all         all         127.0.0.1/32          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         all         192.168.1.0/24          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         all         192.168.55.0/24          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         hive         127.0.0.1/32          password" >> /etc/postgresql/9.5/main/pg_hba.conf

# create hive database and hive user
su - postgres << EOF
createdb -T template0 --encoding=UTF8 ambari
createdb -T template0 --encoding=UTF8 hive
psql -c "
alter user postgres with password 'postgres';
create user ambari with password 'ambari';
grant all privileges on database ambari to ambari;
create user hive with password 'hive';
grant all privileges on database hive to hive;
"
EOF
echo "postgres:postgres" | chpasswd
systemctl restart postgresql.service

# install jdbc driver for postgresql
wget https://jdbc.postgresql.org/download/postgresql-42.2.2.jar
mkdir -p /opt/jdbc
cp postgresql-42.2.2.jar /opt/jdbc/postgresql-jdbc.jar
chmod 644 /opt/jdbc/postgresql-jdbc.jar


# install ambari
apt-get -y install ambari-server ambari-agent ambari-metrics-assembly

ambari-server setup -s --database=postgres --databasehost=localhost --databaseport=5432 --databasename=ambari --databaseusername=ambari --databasepassword=ambari --jdbc-db=postgres --jdbc-driver=/opt/jdbc/postgresql-jdbc.jar
ambari-server setup --silent
ambari-server start
ambari-agent start


cat << EOF > /home/vagrant/cluster_configuration.json
{
  "configurations" : [
    {
      "hive-site": {
        "hive.support.concurrency": "true",
        "hive.txn.manager": "org.apache.hadoop.hive.ql.lockmgr.DbTxnManager",
        "hive.compactor.initiator.on": "true",
        "hive.compactor.worker.threads": "5",
        "javax.jdo.option.ConnectionDriverName": "org.postgresql.Driver",
        "javax.jdo.option.ConnectionPassword": "hive",
        "javax.jdo.option.ConnectionURL": "jdbc:postgresql://localhost/hive",
        "javax.jdo.option.ConnectionUserName": "hive"
      }
    },
    {
      "hive-env": {
        "hive_ambari_database": "PostgreSQL",
        "hive_database": "Existing PostgreSQL Database",
        "hive_database_type": "postgres",
        "hive_database_name": "hive"
      }
    },
    {
      "core-site": {
        "properties" : {
          "hadoop.proxyuser.root.groups" : "*",
          "hadoop.proxyuser.root.hosts" : "*"
        }
      }
    }
  ],
  "host_groups" : [
    {
      "name" : "host_group_1",
      "components" : [
        {
          "name" : "NAMENODE"
        },
        {
          "name" : "SECONDARY_NAMENODE"
        },
        {
          "name" : "DATANODE"
        },
        {
          "name" : "HDFS_CLIENT"
        },
        {
          "name" : "RESOURCEMANAGER"
        },
        {
          "name" : "NODEMANAGER"
        },
        {
          "name" : "YARN_CLIENT"
        },
        {
          "name" : "HISTORYSERVER"
        },
        {
          "name" : "APP_TIMELINE_SERVER"
        },
        {
          "name" : "ZOOKEEPER_SERVER"
        },
        {
          "name" : "ZOOKEEPER_CLIENT"
        },
        {
          "name" : "METRICS_MONITOR"
        },
        {
          "name" : "TEZ_CLIENT"
        },
        {
          "name" : "HIVE_SERVER"
        },
        {
          "name" : "HIVE_METASTORE"
        },
        {
          "name" : "METRICS_COLLECTOR"
        },
        {
          "name" : "WEBHCAT_SERVER"
        },
        {
          "name" : "PIG"
        },
        {
          "name" : "SLIDER"
        },
        {
          "name" : "SPARK2_THRIFTSERVER"
        },
        {
          "name" : "SPARK2_CLIENT"
        },
        {
          "name" : "SPARK2_JOBHISTORYSERVER"
        }
      ],
      "cardinality" : "1"
    }
  ],
  "settings" : [{
     "recovery_settings" : [{
       "recovery_enabled" : "true"
    }]
  }],
  "Blueprints" : {
    "blueprint_name" : "hdp26-jupyter-spark",
    "stack_name" : "HDP",
    "stack_version" : "2.6"
  }
}
EOF

curl -H "X-Requested-By: ambari" -X POST -u admin:admin http://localhost:8080/api/v1/blueprints/hdp26-jupyter-spark -d @/home/vagrant/cluster_configuration.json

cat << EOF > /home/vagrant/hostmapping.json
{
  "blueprint" : "hdp26-jupyter-spark",
  "default_password" : "admin",
  "provision_action" : "INSTALL_AND_START",
  "host_groups" :[
    {
      "name" : "host_group_1",
      "hosts" : [
        {
          "fqdn" : "ub1604jupyterspark.vm.internal"
        }
      ]
    }
  ]
}
EOF

curl -H "X-Requested-By: ambari" -X POST -u admin:admin http://localhost:8080/api/v1/clusters/hdp26-jupyter-spark -d @/home/vagrant/hostmapping.json
sleep 30


# wait until the cluster is ready.
ProgressPercent=`curl -s --user admin:admin -X GET http://localhost:8080/api/v1/clusters/hdp26-jupyter-spark/requests/1 | grep progress_percent | awk '{print $3}' | cut -d . -f 1`
while [[ `echo $ProgressPercent | grep -v 100` ]]; do
  ProgressPercent=`curl -s --user admin:admin -X GET http://localhost:8080/api/v1/clusters/hdp26-jupyter-spark/requests/1 | grep progress_percent | awk '{print $3}' | cut -d . -f 1`
  echo " Progress: $ProgressPercent %"
  sleep 10
done


cat << EOF > /home/vagrant/shutdown_components.sh
#!/bin/bash
#stop all services
curl -u admin:admin -i -H 'X-Requested-By: ambari' -X PUT \
   -d '{"RequestInfo":{"context":"_PARSE_.STOP.ALL_SERVICES","operation_level":{"level":"CLUSTER","cluster_name":"hdp26-minnimal-hive"}},"Body":{"ServiceInfo":{"state":"INSTALLED"}}}' \
http://localhost:8080/api/v1/clusters/hdp26-jupyter-spark/services
EOF
chmod +x /home/vagrant/shutdown_components.sh


cat << EOF > /tmp/test.csv
100,15000000
200,20000000
300,18000000
EOF


cat << EOF > /tmp/sample.sql
CREATE EXTERNAL TABLE sample (
  store_id INT,
  sales INT
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES (
   "separatorChar" = ",",
   "quoteChar"     = '"'
)
stored as textfile
LOCATION '/user/hive';

LOAD DATA LOCAL INPATH '/tmp/test.csv' OVERWRITE INTO TABLE sample;

select * from sample;
EOF


# upload sample content
sudo -u hive hdfs dfs -put /tmp/sample.sql /user/hive
sudo -u hive hdfs dfs -ls /user/hive

# create table and select
beeline -u 'jdbc:hive2://localhost:10016/' -n hive -f /tmp/sample.sql

# install anaconda & jupyterlab
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
chmod +x Anaconda3-5.1.0-Linux-x86_64.sh
./Anaconda3-5.1.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip
pip install jupyterlab

# install pyspark
pip install findspark pyspark


useradd py
mkdir -p /home/py
chown -R py:py /home/py
sudo -u py bash -c "mkdir /home/py/.jupyter"
sudo -u py bash -c "cat << EOF > /home/py/.jupyter/jupyter_notebook_config.py
conf = get_config()
conf.NotebookApp.ip = '*'
conf.NotebookApp.open_browser = False
conf.NotebookApp.port = 8888
conf.NotebookApp.token = 'jupyter'
EOF"

cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
Environment=SPARK_HOME=/usr/hdp/current/spark2-client
ExecStartPre=source /opt/anaconda/bin/activate
ExecStart=/opt/anaconda/bin/jupyter lab
User=py
Group=py
WorkingDirectory=/home/py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable jupyter
sudo systemctl start jupyter


echo 'access -> http://192.168.1.117:8080'
echo 'user/password -> admin/admin'
echo ''
echo 'jupyter -> http://192.168.55.117:8888/?token=jupyter'
SHELL
end

〇動作確認用コード
import os
os.environ["HADOOP_USER_NAME"] = "hive"
from pyspark import SparkContext
from pyspark import SparkConf
from pyspark.sql import SparkSession

spark.sql("SELECT * from csv.`/user/hive/test.csv`").show()

○関連情報
・Ambariに関する他の記事はこちらを参照してください。

VagrantでKritaをインストールした仮想マシン(Ubuntu18.04)を構築する

Kritaはオープンソースのペイントソフトです

○Kritaの画面


○構築方法
以下のVagrantfileを使用して、Kritaがインストールされた仮想マシン(Ubuntu18.04)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804gnomekrita"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804gnomekrita"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.gui = true
  end
  # bridge netwrok
config.vm.network "public_network", ip: "192.168.1.103", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
sed -i.bak -e "s#http://archive.ubuntu.com/ubuntu#http://ftp.riken.jp/pub/Linux/ubuntu#g" /etc/apt/sources.list
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
apt-get -y install language-pack-ja
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106

apt-get -y install xrdp fcitx-mozc ubuntu-desktop  virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11
im-config -n fcitx

sed -i -e "s/allowed_users=console/allowed_users=anybody/" /etc/X11/Xwrapper.config

cat << EOF >> /etc/polkit-1/localauthority/50-local.d/allow-colord.pkla
[Allow colord for all users]
Identity=unix-user:*
Action=org.freedesktop.color-manager.create-device;org.freedesktop.color-manager.create-profile;org.freedesktop.color-manager.delete-device;org.freedesktop.color-manager.delete-profile;org.freedesktop.color-manager.modify-device;org.freedesktop.color-manager.modify-profile
ResultAny=no
ResualtInactive=no
ResultActive=yes
EOF
systemctl restart polkit

add-apt-repository ppa:kritalime/ppa
apt-get update
apt-get -y install krita

init 5
SHELL
end


○関連情報
・kritaに関する他の記事はこちらを参照してください。

DockerでProcessWire、Percona Serverがインストールされたコンテナ(Alpine 3.7)を構築する

ProcessWireはPHP製のCMSです。

○ProcessWireの画面


○構築方法
以下の手順で、ProcessWire、Percona Serverのコンテナを構築・実行します。

1. ProcessWireイメージの作成(Dockerfileがあるフォルダで実行)
docker build -t processwire .

Dockerfile
FROM alpine:3.7
WORKDIR /
RUN  apk update \
  && apk add --no-cache apache2 php7-apache2 php7-mysqli php7-mbstring php7-session php7-xml php7-dom php7-ctype php7-tokenizer php7-gd php7-simplexml php7-iconv php7-zip php7-json php7-pdo php7-pdo_mysql openssl openrc unzip \
  && rm -rf /var/cache/apk/* \
  && wget https://github.com/processwire/processwire/archive/master.zip \
  && unzip master.zip \
  && rm -f master.zip \
  && mkdir /opt \
  && mv processwire-master /opt/processwire \
  && chown -R apache:apache /opt/processwire \
  && mkdir -p /run/apache2 \
  && echo $'\n\
  Options Indexes FollowSymLinks \n\
  AllowOverride All\n\
  Options All\n\
  Require all granted\n\
' >> /etc/apache2/httpd.conf \
  && ln -s /opt/processwire /var/www/localhost/htdocs/ \
  && sed -i -e 's|#LoadModule rewrite_module modules/mod_rewrite.so|LoadModule rewrite_module modules/mod_rewrite.so|' /etc/apache2/httpd.conf
EXPOSE 80
VOLUME /opt/processwire
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

2. ProcessWireとPercona Serverコンテナの構築・実行(docker-compose.ymlがあるフォルダで実行)
docker-compose up -d

docker-compose.yml
version: "2"
services:
  processwire:
    image: processwire
    container_name: "processwire"
    volumes:
      - "processwire-data:/opt/processwire"
    ports:
      - "80:80"
    depends_on:
      - db
  db:
    image: percona:5.7
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    container_name: "processwire-db"
    volumes:
      - "db-data:/var/lib/mysql"
    environment:
        MYSQL_DATABASE: processwire
        MYSQL_ROOT_PASSWORD: processwire
volumes:
  db-data:
    driver: local
  processwire-data:
    driver: local

3. ブラウザからhttp://<Dockerホスト名またはIP>/processwire/にアクセス
「Get Started」ボタンをクリック


4. Site Installation Profileで「Default (Beginner Edition)」を選択して「Continue」をクリック。


5. Compatibility Checkで「Continue to Next Step」をクリック。


6. MySQL Databaseで以下の項目を入力し、「Continue」をクリック。
DB Name : processwire
DB User : root
DB Password : processwire
DB Host : db
DB Engine : InnoDB
他は好みにあわせて設定する


7.Admin Account Informationで管理者ユーザの情報を入力して、「Continue」をクリック


8.初期設定完了画面


○関連情報
・ProcessWireに関する他の記事はこちらを参照してください。

VagrantでPercona ToolkitとPercona Serverがインストールされた仮想マシン(Ubuntu16.04)を構築する

Percona ToolkitはMySQLを管理・メンテナンスするのに役立つコマンド群です。

○構築方法
以下のVagrantfileを使用して、Percona ToolkitとPercona Serverをインストールした仮想マシンを構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604perconaperconatoolkit"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604perconaperconatoolkit"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  # private network
config.vm.network "private_network", ip: "192.168.55.103", :netmask => "255.255.255.0"
  # bridge netwrok
config.vm.network "public_network", ip: "192.168.1.103", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
sed -i.bak -e "s#http://archive.ubuntu.com/ubuntu/#http://ftp.riken.jp/pub/Linux/ubuntu/#g" /etc/apt/sources.list
#localectl set-locale LANG=ja_JP.UTF-8
#localectl set-keymap jp106
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

# install percona server
export DEBIAN_FRONTEND=noninteractive
echo "percona-server-server-5.7 percona-server-server/root_password password root" | sudo debconf-set-selections
echo "percona-server-server-5.7 percona-server-server/root_password_again password root" | sudo debconf-set-selections
wget https://repo.percona.com/apt/percona-release_0.1-4.$(lsb_release -sc)_all.deb
dpkg -i percona-release_0.1-4.$(lsb_release -sc)_all.deb
apt-get update
apt-get -y install percona-server-server-5.7
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' identified by 'root';"
mysql -uroot -proot -e "CREATE DATABASE test DEFAULT CHARACTER SET utf8mb4;"
mysql -uroot -proot -e "CREATE USER test@localhost IDENTIFIED BY 'test';"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost';"
mysql -uroot -proot -e "FLUSH PRIVILEGES;"

# install percona toolkit
wget https://repo.percona.com/apt/percona-release_0.1-4.$(lsb_release -sc)_all.deb
dpkg -i percona-release_0.1-4.$(lsb_release -sc)_all.deb
apt-get update
apt-get -y install percona-toolkit

# display summary information
pt-mysql-summary --user root --password root

SHELL
end


○関連情報
・Percona Toolkitに関する他の記事はこちらを参照してください。

2018年5月29日火曜日

VagrantでJupyter LabとPostgreSQLをインストールした仮想マシン(Ubuntu18.04)を構築する

Jupyter Labでインタラクティブなコンピューティング環境を提供する事ができます。

〇Jupyter Labの画面


構築方法

以下のVagrantfileで、Jupyter LabとPostgreSQL をインストールした仮想マシン(Ubuntu18.04)を構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804jupyterlabpg"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804jupyterlabpg"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.104", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8

# install postgresql
apt-get -y install postgresql
echo "listen_addresses='*'" >> /etc/postgresql/9.5/main/postgresql.conf

sed -i 's/host.*all.*all.*127.0.0.1/#host    all             all             127.0.0.1/g' /etc/postgresql/9.5/main/pg_hba.conf

echo "host    all         all         127.0.0.1/32          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         all         192.168.1.0/24          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         all         192.168.55.0/24          password" >> /etc/postgresql/9.5/main/pg_hba.conf

su - postgres << EOF
createdb -T template0 --locale=ja_JP.UTF-8 --encoding=UTF8 test
psql -c "
alter user postgres with password 'postgres';
create user test with password 'test';
grant all privileges on database test to test;
"
export PGPASSWORD=test
psql -h localhost -d test -U test -c "
create table messages (message_id integer not null, message varchar(100));
insert into messages values (1, 'hello world.');
insert into messages values (2, 'test message.');
"
EOF
echo "postgres:postgres" | chpasswd
systemctl restart postgresql.service


# install anaconda & jupyterlab
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
chmod +x Anaconda3-5.1.0-Linux-x86_64.sh
./Anaconda3-5.1.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip
pip install jupyterlab

# install psycopg2
apt-get -y install libpq-dev python-dev
pip install psycopg2-binary

useradd py
mkdir -p /home/py
chown -R py:py /home/py
sudo -u py bash -c "mkdir /home/py/.jupyter"
sudo -u py bash -c "cat << EOF > /home/py/.jupyter/jupyter_notebook_config.py
conf = get_config()
conf.NotebookApp.ip = '*'
conf.NotebookApp.open_browser = False
conf.NotebookApp.port = 8080
conf.NotebookApp.token = 'jupyter'
EOF"

cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
EnvironmentFile=/opt/anaconda/bin/activate
ExecStart=/opt/anaconda/bin/jupyter lab
User=py
Group=py
WorkingDirectory=/home/py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable jupyter
sudo systemctl start jupyter

echo 'access -> http://192.168.55.104:8080/?token=jupyter'
SHELL
end

〇動作検証用コード
import psycopg2

con = psycopg2.connect("dbname=test host=localhost user=test password=test")

try:
  cur= con.cursor()
  sql = "select message_id, message from messages"
  cur.execute(sql)

  for row in cur.fetchall():
    print(row)

except psycopg2.Error as er:
    print('psycopg2.Error:', er)
cur.close
con.close


関連情報

・Jupyterに関する他の情報はこちらを参照してください。

・psycopg2に関する他の記事はこちらを参照してください。

DockerでPydio、Percona Serverがインストールされたコンテナ(Alpine 3.7)を構築する

PydioはDropboxのようにファイルを管理できるソフトウェアです。

○Pydioの画面


○構築方法
以下の手順で、PydioとPercona Serverのコンテナを構築・実行します。
1. Pydioイメージの作成(Dockerfileがあるフォルダで実行)
docker build -t pydio .

Dockerfile
FROM alpine:3.7
WORKDIR /
RUN  apk update \
  && apk add --no-cache apache2 php7 php7-apache2 php7-mysqli php7-mbstring php7-mcrypt php7-session php7-xml php7-dom php7-ctype php7-tokenizer php7-gd php7-simplexml php7-iconv php7-zip php7-intl php7-json php7-exif php7-opcache php7-zlib openssl openrc \
  && rm -rf /var/cache/apk/* \
  && wget https://download.pydio.com/pub/core/archives/pydio-core-8.0.2.tar.gz \
  && tar xvfz pydio-core-8.0.2.tar.gz \
  && rm -f pydio-core-8.0.2.tar.gz \
  && mkdir /opt \
  && mv pydio-core-8.0.2/ /opt/pydio \
  && chown -R apache:apache /opt/pydio \
  && mkdir -p /run/apache2 \
  && echo $'\n\
  Options Indexes FollowSymLinks \n\
  AllowOverride All\n\
  Options All\n\
  Require all granted\n\
' >> /etc/apache2/httpd.conf \
  && ln -s /opt/pydio /var/www/localhost/htdocs/ \
  && sed -i -e 's/output_buffering = 4096/output_buffering = off/'  /etc/php7/php.ini \
  && sed -i -e 's|#LoadModule rewrite_module modules/mod_rewrite.so|LoadModule rewrite_module modules/mod_rewrite.so|' /etc/apache2/httpd.conf
EXPOSE 80
VOLUME /opt/pydio
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

2. PydioとPercona Serverコンテナの構築・実行(docker-compose.ymlがあるフォルダで実行)
docker-compose up -d

docker-compose.yml
version: "2"
services:
  pydio:
    image: pydio
    container_name: "pydio"
    volumes:
      - "pydio-data:/opt/pydio"
    ports:
      - "80:80"
    depends_on:
      - db
  db:
    image: percona:5.7
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    container_name: "pydio-db"
    volumes:
      - "db-data:/var/lib/mysql"
    environment:
        MYSQL_DATABASE: pydio
        MYSQL_ROOT_PASSWORD: pydio
volumes:
  db-data:
    driver: local
  pydio-data:
    driver: local

3. ブラウザからhttp://<Dockerホスト名またはIP>/pydio/にアクセス
「CONTINUE TO PYDIO INSTALLATION」ボタンをクリック


4. Pick your languageで「English」を選択して「Start Wizard」をクリック。


5. Application Settingsで「NEXT」をクリック。


6. Authenticationで管理者情報を入力して、「NEXT」をクリック


7. Database Connectionで以下の項目を入力し、「TEST DB CONNECTION」をクリック。
Host : db
Database : pydio
User : root
Password : pydio


8. Advanced Optionsで「INSTALL PYDIO」をクリックする


VagrantでMezzanineとMariaDBをインストールした仮想マシン(Debian Stretch/9.4)を構築する

MezzanineはPython製のCMSです。

○Mezzanineの画面


○構築方法
以下のVagrantfileを使用して、MezzanineとMariaDBがインストールされた仮想マシン(Debian Stretch/9.4)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.4"
  config.vm.hostname = "db94mezzaninemariadb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db94mezzaninemariadb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  config.vm.network "private_network", ip: "192.168.55.105", :netmask => "255.255.255.0"
  config.vm.network "public_network", ip:"192.168.1.105", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get -y install task-japanese
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
update-locale LANG=ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

# install mariadb
echo "mariadb-server-10.0 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mariadb-server-10.0 mysql-server/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mariadb-server
mysql -uroot -proot -e "CREATE DATABASE mezzanine DEFAULT CHARACTER SET utf8;"
mysql -uroot -proot -e "CREATE USER mezzanine@localhost IDENTIFIED BY 'mezzanine';"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON mezzanine.* TO 'mezzanine'@'localhost';"
mysql -uroot -proot -e "FLUSH PRIVILEGES;"

# install apache2 and wsgi
apt-get -y install apache2 libapache2-mod-wsgi
a2enmod wsgi

# install mezzanine
apt-get -y install python-pip
#pip install --upgrade pip
pip install mezzanine

apt-get -y install libmariadbclient-dev
pip install mysqlclient

mkdir -p /opt/mezzanine
cd /opt/mezzanine

mezzanine-project mysite

sed -i -e 's/"ENGINE": "django.db.backends.sqlite3",/"ENGINE": "django.db.backends.mysql",/' /opt/mezzanine/mysite/mysite/local_settings.py
sed -i -e 's/"NAME": "dev.db",/"NAME": "mezzanine",/' /opt/mezzanine/mysite/mysite/local_settings.py
sed -i -e 's/"USER": "",/"USER": "mezzanine",/' /opt/mezzanine/mysite/mysite/local_settings.py
sed -i -e 's/"PASSWORD": "",/"PASSWORD": "mezzanine",/' /opt/mezzanine/mysite/mysite/local_settings.py
sed -i -e 's/"HOST": "",/"HOST": "localhost",/' /opt/mezzanine/mysite/mysite/local_settings.py
sed -i -e 's/"PORT": "",/"PORT": "3306",/' /opt/mezzanine/mysite/mysite/local_settings.py
echo 'ALLOWED_HOSTS = ["*"]' >> /opt/mezzanine/mysite/mysite/local_settings.py

cd /opt/mezzanine/mysite
python manage.py createdb --noinput
python manage.py collectstatic --noinput

cat << EOF > /etc/apache2/sites-available/mezzanine.conf
<VirtualHost *:80>
  ServerName 192.168.55.105
  WSGIDaemonProcess mezzanine processes=1 threads=25
  WSGIScriptAlias / /opt/mezzanine/mysite/mysite/wsgi.py
  Alias /static/ /opt/mezzanine/mysite/static/
  <Directory /opt/mezzanine/mysite>
    WSGIProcessGroup mezzanine
    WSGIApplicationGroup %{GLOBAL}
    Require all granted
  </Directory>
</VirtualHost>
EOF

cat << EOF > /opt/mezzanine/mysite/mysite/wsgi.py
"""
WSGI config for mysite project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/
"""

import os
import sys

from django.core.wsgi import get_wsgi_application
from mezzanine.utils.conf import real_project_name
sys.path.append("/opt/mezzanine/mysite")
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
                      "%s.settings" % real_project_name("mysite"))

application = get_wsgi_application()
EOF

chown -R www-data:www-data /opt/mezzanine

a2ensite mezzanine.conf
a2dissite 000-default.conf
systemctl restart apache2

echo 'access to http://192.168.55.105/'
echo 'access to http://192.168.55.105/admin/'
echo 'user: admin  password: default'
SHELL
end

2018年5月28日月曜日

DockerでAdminerとMySQLのコンテナーを構築する

より新しい記事「DockerでAdminer4.7とMySQL8.0のコンテナーを構築する」を参照してください。

Adminerでブラウザからデータベースを操作する事ができます。

○Adminerの画面

ユーザ:root、パスワード:root、データベース:mysqlを指定します。

○構築方法
以下のdocker-compose.ymlを使用して、AdminerとMySQLのコンテナーを構築する事ができます。
docker-compose up -d

docker-compose.yml
version: "2"
services:
  adminer:
    image: adminer:4.6
    container_name: "adminer"
    ports:
      - "8080:8080"
    depends_on:
      - db
  db:
    image: mysql:5.7
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    container_name: "test-db"
    volumes:
      - "db-data:/var/lib/mysql"
    ports:
      - "3306:3306"
    environment:
        MYSQL_DATABASE: test
        MYSQL_ROOT_PASSWORD: root
volumes:
  db-data:
    driver: local


〇関連情報
・Adminerに関する他の情報はこちらを参照してください。

・Adminerのプロジェクトwebサイト
https://www.adminer.org/

VagrantでAmbari(HDP2.6)/HDFS/Sparkをインストールした仮想マシン(Ubuntu16.04)を構築する

Apache SparkでHadoop上のデータにクエリーを実行する事ができます。

〇Ambariの画面

ブラウザでhttp://192.168.1.117:8080/にアクセスします。ユーザ/パスワードはadmin/adminです。

〇構築方法
以下のVagrantfileを使用して、Ambari/HDFS/Sparkをインストールした仮想マシン(Ubuntu16.04)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604spark.vm.internal"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604spark.vm.internal"
     vbox.cpus = 4
     vbox.memory = 10240 
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  # private network
  config.vm.network "private_network", ip: "192.168.55.117", :netmask => "255.255.255.0"
  # bridge netwrok
  config.vm.network "public_network", ip: "192.168.1.117", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
echo "192.168.55.117 ub1604spark" >> /etc/hosts
apt-get -y install curl

cd /root
mkdir ./.ssh
ssh-keygen -f ./.ssh/id_rsa -t rsa -N ''

# copy private key
cp -f ./.ssh/id_rsa /vagrant
cat ./.ssh/id_rsa.pub >> ./.ssh/authorized_keys
chmod 600 ./.ssh/authorized_keys

# install and configure ambari server
wget -O /etc/apt/sources.list.d/ambari.list http://public-repo-1.hortonworks.com/ambari/ubuntu16/2.x/updates/2.6.2.0/ambari.list
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com B9733A7A07513CAD
apt-get update

# install postgresql
apt-get -y install postgresql 
echo "listen_addresses='*'" >> /etc/postgresql/9.5/main/postgresql.conf

sed -i 's/host.*all.*all.*127.0.0.1/#host    all             all             127.0.0.1/g' /etc/postgresql/9.5/main/pg_hba.conf

echo "host    all         all         127.0.0.1/32          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         all         192.168.1.0/24          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         all         192.168.55.0/24          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         hive         127.0.0.1/32          password" >> /etc/postgresql/9.5/main/pg_hba.conf

# create hive database and hive user
su - postgres << EOF
createdb -T template0 --encoding=UTF8 ambari
createdb -T template0 --encoding=UTF8 hive
createdb -T template0 --encoding=UTF8 oozie
psql -c "
alter user postgres with password 'postgres';
create user ambari with password 'ambari';
grant all privileges on database ambari to ambari;
create user hive with password 'hive';
grant all privileges on database hive to hive;
create user oozie with password 'oozie';
grant all privileges on database oozie to oozie;
"
EOF
echo "postgres:postgres" | chpasswd
systemctl restart postgresql.service

# install jdbc driver for postgresql
wget https://jdbc.postgresql.org/download/postgresql-42.2.2.jar
mkdir -p /opt/jdbc
cp postgresql-42.2.2.jar /opt/jdbc/postgresql-jdbc.jar
chmod 644 /opt/jdbc/postgresql-jdbc.jar


# install ambari
apt-get -y install ambari-server ambari-agent ambari-metrics-assembly 

ambari-server setup -s --database=postgres --databasehost=localhost --databaseport=5432 --databasename=ambari --databaseusername=ambari --databasepassword=ambari --jdbc-db=postgres --jdbc-driver=/opt/jdbc/postgresql-jdbc.jar
ambari-server setup --silent
ambari-server start
ambari-agent start


cat << EOF > /home/vagrant/cluster_configuration.json
{
  "configurations" : [
    {
      "hive-site": {
        "hive.support.concurrency": "true",
        "hive.txn.manager": "org.apache.hadoop.hive.ql.lockmgr.DbTxnManager",
        "hive.compactor.initiator.on": "true",
        "hive.compactor.worker.threads": "5",
        "javax.jdo.option.ConnectionDriverName": "org.postgresql.Driver",
        "javax.jdo.option.ConnectionPassword": "hive",
        "javax.jdo.option.ConnectionURL": "jdbc:postgresql://localhost/hive",
        "javax.jdo.option.ConnectionUserName": "hive"
      }
    },
    {
      "hive-env": {
        "hive_ambari_database": "PostgreSQL",
        "hive_database": "Existing PostgreSQL Database",
        "hive_database_type": "postgres",
        "hive_database_name": "hive"
      }
    },
    {
      "core-site": {
        "properties" : {
          "hadoop.proxyuser.root.groups" : "*",
          "hadoop.proxyuser.root.hosts" : "*"
        }
      }
    }
  ],
  "host_groups" : [
    {
      "name" : "host_group_1",
      "components" : [
        {
          "name" : "NAMENODE"
        },
        {
          "name" : "SECONDARY_NAMENODE"
        },
        {
          "name" : "DATANODE"
        },
        {
          "name" : "HDFS_CLIENT"
        },
        {
          "name" : "RESOURCEMANAGER"
        },
        {
          "name" : "NODEMANAGER"
        },
        {
          "name" : "YARN_CLIENT"
        },
        {
          "name" : "HISTORYSERVER"
        },
        {
          "name" : "APP_TIMELINE_SERVER"
        },
        {
          "name" : "ZOOKEEPER_SERVER"
        },
        {
          "name" : "ZOOKEEPER_CLIENT"
        },
        {
          "name" : "METRICS_MONITOR"
        },
        {
          "name" : "TEZ_CLIENT"
        },
        {
          "name" : "HIVE_SERVER"
        },
        {
          "name" : "HIVE_METASTORE"
        },
        {
          "name" : "METRICS_COLLECTOR"
        },
        {
          "name" : "WEBHCAT_SERVER"
        },
        {
          "name" : "PIG"
        },
        {
          "name" : "SLIDER"
        },
        {
          "name" : "SPARK2_THRIFTSERVER"
        },
        {
          "name" : "SPARK2_CLIENT"
        },
        {
          "name" : "SPARK2_JOBHISTORYSERVER"
        }
      ],
      "cardinality" : "1"
    }
  ],
  "settings" : [{
     "recovery_settings" : [{
       "recovery_enabled" : "true"
    }]
  }],
  "Blueprints" : {
    "blueprint_name" : "hdp26-minimal-spark",
    "stack_name" : "HDP",
    "stack_version" : "2.6"
  }
}
EOF

curl -H "X-Requested-By: ambari" -X POST -u admin:admin http://localhost:8080/api/v1/blueprints/hdp26-minimal-spark -d @/home/vagrant/cluster_configuration.json

cat << EOF > /home/vagrant/hostmapping.json
{
  "blueprint" : "hdp26-minimal-spark",
  "default_password" : "admin",
  "provision_action" : "INSTALL_AND_START",
  "host_groups" :[
    {
      "name" : "host_group_1",
      "hosts" : [
        {
          "fqdn" : "ub1604spark.vm.internal"
        }
      ]
    }
  ]
}
EOF

curl -H "X-Requested-By: ambari" -X POST -u admin:admin http://localhost:8080/api/v1/clusters/hdp26-minimal-spark -d @/home/vagrant/hostmapping.json
sleep 30


# wait until the cluster is ready.
ProgressPercent=`curl -s --user admin:admin -X GET http://localhost:8080/api/v1/clusters/hdp26-minimal-spark/requests/1 | grep progress_percent | awk '{print $3}' | cut -d . -f 1`
while [[ `echo $ProgressPercent | grep -v 100` ]]; do
  ProgressPercent=`curl -s --user admin:admin -X GET http://localhost:8080/api/v1/clusters/hdp26-minimal-spark/requests/1 | grep progress_percent | awk '{print $3}' | cut -d . -f 1`
  echo " Progress: $ProgressPercent %"
  sleep 10
done


cat << EOF > /home/vagrant/shutdown_components.sh
#!/bin/bash
#stop all services
curl -u admin:admin -i -H 'X-Requested-By: ambari' -X PUT \
   -d '{"RequestInfo":{"context":"_PARSE_.STOP.ALL_SERVICES","operation_level":{"level":"CLUSTER","cluster_name":"hdp26-minnimal-hive"}},"Body":{"ServiceInfo":{"state":"INSTALLED"}}}' \
   http://localhost:8080/api/v1/clusters/hdp26-minimal-spark/services
EOF
chmod +x /home/vagrant/shutdown_components.sh


cat << EOF > /tmp/test.csv
100,15000000
200,20000000
300,18000000
EOF


cat << EOF > /tmp/sample.sql
CREATE EXTERNAL TABLE sample (
  store_id INT,
  sales INT
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES (
   "separatorChar" = ",",
   "quoteChar"     = '"'
)
stored as textfile
LOCATION '/user/hive';

LOAD DATA LOCAL INPATH '/tmp/test.csv' OVERWRITE INTO TABLE sample;

select * from sample;
EOF


# upload sample content
sudo -u hive hdfs dfs -put /tmp/sample.sql /user/hive
sudo -u hive hdfs dfs -ls /user/hive

# create table and select
beeline -u 'jdbc:hive2://localhost:10016/' -n hive -f /tmp/sample.sql

echo 'access -> http://192.168.1.117:8080'
echo 'user/password -> admin/admin'
SHELL
end

○関連情報
・Ambariに関する他の記事はこちらを参照してください。

VagrantでAmbari(HDP2.6)/HDFS/HBase/Phoenixをインストールした仮想マシン(CentOS7.4)を構築する

Apache PhoenixはHBase上に構築されるRDBレイヤーです。

〇Ambariの画面

ブラウザでhttp://192.168.1.117:8080/にアクセスします。ユーザ/パスワードはadmin/adminです。

〇構築方法
以下のVagrantfileを使用して、Ambari/HDFS/HBase/Phoenixをインストールした仮想マシン(CentOS7.4)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.4"
  config.vm.hostname = "co74phoenix.vm.internal"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "co74phoenix.vm.internal"
     vbox.cpus = 4
     vbox.memory = 10240 
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  # private network
  config.vm.network "private_network", ip: "192.168.55.117", :netmask => "255.255.255.0"
  # bridge netwrok
  config.vm.network "public_network", ip: "192.168.1.117", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
localectl set-locale LANG=ja_JP.UTF-8
echo "192.168.55.117 co74phoenix" >> /etc/hosts

cd /root
mkdir ./.ssh
ssh-keygen -f ./.ssh/id_rsa -t rsa -N ''

# copy private key
cp -f ./.ssh/id_rsa /vagrant
cat ./.ssh/id_rsa.pub >> ./.ssh/authorized_keys
chmod 600 ./.ssh/authorized_keys

# install and configure ambari server
cd /etc/yum.repos.d/
wget http://public-repo-1.hortonworks.com/ambari/centos7/2.x/updates/2.6.2.0/ambari.repo
yum -y install ambari-server ambari-agent
ambari-server setup --silent
ambari-server start

# create hive database and hive user
echo "host    all         all         127.0.0.1/32          password" >> /var/lib/pgsql/data/pg_hba.conf
echo "host    all         all         192.168.1.0/24          password" >> /var/lib/pgsql/data/pg_hba.conf
echo "host    all         all         192.168.55.0/24          password" >> /var/lib/pgsql/data/pg_hba.conf
service postgresql restart

yum -y install postgresql-jdbc*
chmod 644 /usr/share/java/postgresql-jdbc.jar

ambari-server setup -s --database=postgres --databasehost=localhost --databaseport=5432 --databasename=hive --databaseusername=hive --databasepassword=hive --jdbc-db=postgres --jdbc-driver=/usr/share/java/postgresql-jdbc.jar
ambari-agent start


cat << EOF > /home/vagrant/cluster_configuration.json
{
  "configurations" : [
    {
      "hbase-env" : {
        "properties_attributes" : { },
        "properties" : {
          "phoenix_sql_enabled" : "true"
        }
      }
    },
    {
      "core-site": {
        "properties" : {
          "hadoop.proxyuser.root.groups" : "*",
          "hadoop.proxyuser.root.hosts" : "*"
        }
      }
    }
  ],
  "host_groups" : [
    {
      "name" : "host_group_1",
      "components" : [
        {
          "name" : "NAMENODE"
        },
        {
          "name" : "SECONDARY_NAMENODE"
        },
        {
          "name" : "DATANODE"
        },
        {
          "name" : "HDFS_CLIENT"
        },
        {
          "name" : "RESOURCEMANAGER"
        },
        {
          "name" : "NODEMANAGER"
        },
        {
          "name" : "YARN_CLIENT"
        },
        {
          "name" : "HISTORYSERVER"
        },
        {
          "name" : "APP_TIMELINE_SERVER"
        },
        {
          "name" : "ZOOKEEPER_SERVER"
        },
        {
          "name" : "ZOOKEEPER_CLIENT"
        },
        {
          "name" : "METRICS_MONITOR"
        },
        {
          "name" : "METRICS_COLLECTOR"
        },
        {
          "name" : "HBASE_CLIENT"
        },
        {
          "name" : "HBASE_MASTER"
        },
        {
          "name" : "HBASE_REGIONSERVER"
        }
      ],
      "cardinality" : "1"
    }
  ],
  "settings" : [{
     "recovery_settings" : [{
       "recovery_enabled" : "true"
    }]
  }],
  "Blueprints" : {
    "blueprint_name" : "hdp26-minimal-phoenix",
    "stack_name" : "HDP",
    "stack_version" : "2.6"
  }
}
EOF

curl -H "X-Requested-By: ambari" -X POST -u admin:admin http://localhost:8080/api/v1/blueprints/hdp26-minimal-phoenix -d @/home/vagrant/cluster_configuration.json

cat << EOF > /home/vagrant/hostmapping.json
{
  "blueprint" : "hdp26-minimal-phoenix",
  "default_password" : "admin",
  "provision_action" : "INSTALL_AND_START",
  "host_groups" :[
    {
      "name" : "host_group_1",
      "hosts" : [
        {
          "fqdn" : "co74phoenix.vm.internal"
        }
      ]
    }
  ]
}
EOF

curl -H "X-Requested-By: ambari" -X POST -u admin:admin http://localhost:8080/api/v1/clusters/hdp26-minimal-phoenix -d @/home/vagrant/hostmapping.json
sleep 30


# wait until the cluster is ready.
ProgressPercent=`curl -s --user admin:admin -X GET http://localhost:8080/api/v1/clusters/hdp26-minimal-phoenix/requests/1 | grep progress_percent | awk '{print $3}' | cut -d . -f 1`
while [[ `echo $ProgressPercent | grep -v 100` ]]; do
  ProgressPercent=`curl -s --user admin:admin -X GET http://localhost:8080/api/v1/clusters/hdp26-minimal-phoenix/requests/1 | grep progress_percent | awk '{print $3}' | cut -d . -f 1`
  echo " Progress: $ProgressPercent %"
  sleep 10
done


cat << EOF > /home/vagrant/shutdown_components.sh
#!/bin/bash
#stop all services
curl -u admin:admin -i -H 'X-Requested-By: ambari' -X PUT \
   -d '{"RequestInfo":{"context":"_PARSE_.STOP.ALL_SERVICES","operation_level":{"level":"CLUSTER","cluster_name":"hdp26-minnimal-hbase"}},"Body":{"ServiceInfo":{"state":"INSTALLED"}}}' \
   http://localhost:8080/api/v1/clusters/hdp26-minimal-phoenix/services
EOF
chmod +x /home/vagrant/shutdown_components.sh


# execute sql commands for test...
cat << EOF > test1.txt
create table test1 (message_id integer not null primary key, message varchar(100));
upsert into test1 values (100, 'hello world.');
select * from test1;
!quit
EOF
/usr/hdp/2.6.5.0-292/phoenix/bin/sqlline.py test1.txt


echo 'access -> http://192.168.1.117:8080'
echo 'user/password -> admin/admin'
SHELL
end

○関連情報
・Apache HBaseに関する他の記事はこちらを参照してください。
・Ambariに関する他の記事はこちらを参照してください。

VagrantでApache SupersetとMariaDBをインストールした仮想マシン(CentOS7.4)を構築する

Apache SupersetはPython製のデータ可視化ツールです。

○Apache Supersetの画面


○構築方法
以下のVagrantfileを使用して、Apache SupersetとMariaDBをインストールした仮想マシン(CentOS7.4)を構築する事ができます。
仮想マシンが構築された後、ブラウザからhttp://192.168.55.109:8088/にアクセスします。
デフォルトユーザ名はadmin、パスワードもadminです。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.4"
  config.vm.hostname = "co74supersetmariadb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "co74supersetmariadb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.109", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.109", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
localectl set-locale LANG=ja_JP.UTF-8


# install mariadb
yum -y install mariadb mariadb-server
sudo systemctl enable mariadb.service
sudo systemctl start mariadb.service

# change password and create users and databases.
mysql -uroot -e "SET PASSWORD = PASSWORD('root'); FLUSH PRIVILEGES;"
mysql -uroot -proot -e "CREATE DATABASE test DEFAULT CHARACTER SET utf8;"
mysql -uroot -proot -e "CREATE USER test@localhost IDENTIFIED BY 'test';"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost';"
mysql -uroot -proot -e "FLUSH PRIVILEGES;"
mysql -utest -ptest test -e "create table messages (message_id integer not null, message varchar(100));"
mysql -utest -ptest test -e "insert into messages value (1, 'hello world.');"
mysql -utest -ptest test -e "insert into messages value (2, 'test message.');"

# install anaconda
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
chmod +x Anaconda3-5.1.0-Linux-x86_64.sh
./Anaconda3-5.1.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip

# install dependencies
yum -y install epel-release ython-devel mysql-devel gcc-c++ openldap-devel openssl-devel

# install mysqlclient
pip install mysqlclient

pip install --upgrade setuptools
pip install superset
pip install cryptography --upgrade
mkdir -p /opt/superset
cd /opt/superset
fabmanager create-admin --app superset --username admin --firstname admin --lastname user --email admin@localhost.localdomain --password admin
superset db upgrade
superset load_examples
superset init
superset runserver -a 0.0.0.0 &
echo 'access http://192.168.55.109:8088/'
echo 'user:admin, password: admin'

SHELL
end

○データソースの追加
同じ仮想マシンにインストールされたMariaDBに接続するには、以下の画面のようにSQLAlchemy URIにmysql://test:test@localhost/testを指定します。


VagrantでApache SupersetとPercona Serverをインストールした仮想マシン(Debian Stretch/9.4)を構築する

Apache SupersetはPython製のデータ可視化ツールです。

○Apache Supersetの画面


○構築方法
以下のVagrantfileを使用して、Apache SupersetとPercona Sercerをインストールした仮想マシン(Debian Stretch/9.4)を構築する事ができます。
仮想マシンが構築された後、ブラウザからhttp://192.168.55.107:8088/にアクセスします。
デフォルトユーザ名はadmin、パスワードもadminです。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.4"
  config.vm.hostname = "db94supersetpercona"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db94supersetpercona"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.107", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.107", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get -y install task-japanese
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
update-locale LANG=ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade


# install percona server
export DEBIAN_FRONTEND=noninteractive
echo "percona-server-server-5.7 percona-server-server/root_password password root" | sudo debconf-set-selections
echo "percona-server-server-5.7 percona-server-server/root_password_again password root" | sudo debconf-set-selections
wget https://repo.percona.com/apt/percona-release_0.1-4.$(lsb_release -sc)_all.deb
dpkg -i percona-release_0.1-4.$(lsb_release -sc)_all.deb
apt-get update
apt-get -y install percona-server-server-5.7
mysql -uroot -e "CREATE DATABASE test DEFAULT CHARACTER SET utf8;"
mysql -uroot -e "CREATE USER test@localhost IDENTIFIED BY 'test';"
mysql -uroot -e "GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost';"
mysql -uroot -e "FLUSH PRIVILEGES;"
mysql -utest -ptest test -e "create table messages (message_id integer not null, message varchar(100));"
mysql -utest -ptest test -e "insert into messages value (1, 'hello world.');"
mysql -utest -ptest test -e "insert into messages value (2, 'test message.');"

# install anaconda
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
chmod +x Anaconda3-5.1.0-Linux-x86_64.sh
./Anaconda3-5.1.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip

# install dependencies
apt-get -y install build-essential libssl-dev libffi-dev python-dev python-pip libsasl2-dev libldap2-dev

# install mysqlclient
apt-get -y install default-libmysqlclient-dev
pip install mysqlclient

pip install --upgrade setuptools
pip install superset
pip install cryptography --upgrade
mkdir -p /opt/superset
cd /opt/superset
fabmanager create-admin --app superset --username admin --firstname admin --lastname user --email admin@localhost.localdomain --password admin
superset db upgrade
superset load_examples
superset init
superset runserver -a 0.0.0.0 &
echo 'access http://192.168.55.107:8088/'
echo 'user:admin, password: admin'

SHELL
end

○データソースの追加
同じ仮想マシンにインストールされたPercona Serverに接続するには、以下の画面のようにSQLAlchemy URIにmysql://test:test@localhost/testを指定します。


2018年5月27日日曜日

VagrantでJupyter LabとHiveをインストールした仮想マシン(Ubuntu16.04)を構築する

Jupyter Labでインタラクティブなコンピューティング環境を提供する事ができます。AmbariでHDFS/Hiveをインストールした1ノードクラスタを構築します。pyhiveを使用して、JupyterからHiveに接続します。

〇Jupyter Labの画面


〇構築方法
以下のVagrantfileで、Jupyter LabとHiveをインストールした仮想マシン(Ubuntu16.04)を構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604jupyterhive.vm.internal"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604jupyterhive.vm.internal"
     vbox.cpus = 4
     vbox.memory = 10240
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  # private network
config.vm.network "private_network", ip: "192.168.55.117", :netmask => "255.255.255.0"
  # bridge netwrok
config.vm.network "public_network", ip: "192.168.1.117", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
echo "192.168.55.117 ub1604jupyterhive" >> /etc/hosts
apt-get -y install curl

cd /root
mkdir ./.ssh
ssh-keygen -f ./.ssh/id_rsa -t rsa -N ''

# copy private key
cp -f ./.ssh/id_rsa /vagrant
cat ./.ssh/id_rsa.pub >> ./.ssh/authorized_keys
chmod 600 ./.ssh/authorized_keys

# install and configure ambari server
wget -O /etc/apt/sources.list.d/ambari.list http://public-repo-1.hortonworks.com/ambari/ubuntu16/2.x/updates/2.6.2.0/ambari.list
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com B9733A7A07513CAD
apt-get update

# install postgresql
apt-get -y install postgresql
echo "listen_addresses='*'" >> /etc/postgresql/9.5/main/postgresql.conf

sed -i 's/host.*all.*all.*127.0.0.1/#host    all             all             127.0.0.1/g' /etc/postgresql/9.5/main/pg_hba.conf

echo "host    all         all         127.0.0.1/32          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         all         192.168.1.0/24          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         all         192.168.55.0/24          password" >> /etc/postgresql/9.5/main/pg_hba.conf
echo "host    all         hive         127.0.0.1/32          password" >> /etc/postgresql/9.5/main/pg_hba.conf

# create hive database and hive user
su - postgres << EOF
createdb -T template0 --encoding=UTF8 ambari
createdb -T template0 --encoding=UTF8 hive
createdb -T template0 --encoding=UTF8 oozie
psql -c "
alter user postgres with password 'postgres';
create user ambari with password 'ambari';
grant all privileges on database ambari to ambari;
create user hive with password 'hive';
grant all privileges on database hive to hive;
create user oozie with password 'oozie';
grant all privileges on database oozie to oozie;
"
EOF
echo "postgres:postgres" | chpasswd
systemctl restart postgresql.service

# install jdbc driver for postgresql
wget https://jdbc.postgresql.org/download/postgresql-42.2.2.jar
mkdir -p /opt/jdbc
cp postgresql-42.2.2.jar /opt/jdbc/postgresql-jdbc.jar
chmod 644 /opt/jdbc/postgresql-jdbc.jar


# install ambari
apt-get -y install ambari-server ambari-agent ambari-metrics-assembly

ambari-server setup -s --database=postgres --databasehost=localhost --databaseport=5432 --databasename=ambari --databaseusername=ambari --databasepassword=ambari --jdbc-db=postgres --jdbc-driver=/opt/jdbc/postgresql-jdbc.jar
ambari-server setup --silent
ambari-server start
ambari-agent start


cat << EOF > /home/vagrant/cluster_configuration.json
{
  "configurations" : [
    {
      "hive-site": {
        "hive.support.concurrency": "true",
        "hive.txn.manager": "org.apache.hadoop.hive.ql.lockmgr.DbTxnManager",
        "hive.compactor.initiator.on": "true",
        "hive.compactor.worker.threads": "5",
        "javax.jdo.option.ConnectionDriverName": "org.postgresql.Driver",
        "javax.jdo.option.ConnectionPassword": "hive",
        "javax.jdo.option.ConnectionURL": "jdbc:postgresql://localhost/hive",
        "javax.jdo.option.ConnectionUserName": "hive"
      }
    },
    {
      "hive-env": {
        "hive_ambari_database": "PostgreSQL",
        "hive_database": "Existing PostgreSQL Database",
        "hive_database_type": "postgres",
        "hive_database_name": "hive"
      }
    },
    {
      "core-site": {
        "properties" : {
          "hadoop.proxyuser.root.groups" : "*",
          "hadoop.proxyuser.root.hosts" : "*"
        }
      }
    }
  ],
  "host_groups" : [
    {
      "name" : "host_group_1",
      "components" : [
        {
          "name" : "NAMENODE"
        },
        {
          "name" : "SECONDARY_NAMENODE"
        },
        {
          "name" : "DATANODE"
        },
        {
          "name" : "HDFS_CLIENT"
        },
        {
          "name" : "RESOURCEMANAGER"
        },
        {
          "name" : "NODEMANAGER"
        },
        {
          "name" : "YARN_CLIENT"
        },
        {
          "name" : "HISTORYSERVER"
        },
        {
          "name" : "APP_TIMELINE_SERVER"
        },
        {
          "name" : "ZOOKEEPER_SERVER"
        },
        {
          "name" : "ZOOKEEPER_CLIENT"
        },
        {
          "name" : "METRICS_MONITOR"
        },
        {
          "name" : "TEZ_CLIENT"
        },
        {
          "name" : "HIVE_SERVER"
        },
        {
          "name" : "HIVE_METASTORE"
        },
        {
          "name" : "METRICS_COLLECTOR"
        },
        {
          "name" : "WEBHCAT_SERVER"
        }
      ],
      "cardinality" : "1"
    }
  ],
  "settings" : [{
     "recovery_settings" : [{
       "recovery_enabled" : "true"
    }]
  }],
  "Blueprints" : {
    "blueprint_name" : "hdp26-jupyter-hive",
    "stack_name" : "HDP",
    "stack_version" : "2.6"
  }
}
EOF

curl -H "X-Requested-By: ambari" -X POST -u admin:admin http://localhost:8080/api/v1/blueprints/hdp26-jupyter-hive -d @/home/vagrant/cluster_configuration.json

cat << EOF > /home/vagrant/hostmapping.json
{
  "blueprint" : "hdp26-jupyter-hive",
  "default_password" : "admin",
  "provision_action" : "INSTALL_AND_START",
  "host_groups" :[
    {
      "name" : "host_group_1",
      "hosts" : [
        {
          "fqdn" : "ub1604jupyterhive.vm.internal"
        }
      ]
    }
  ]
}
EOF

curl -H "X-Requested-By: ambari" -X POST -u admin:admin http://localhost:8080/api/v1/clusters/hdp26-jupyter-hive -d @/home/vagrant/hostmapping.json
sleep 30


# wait until the cluster is ready.
ProgressPercent=`curl -s --user admin:admin -X GET http://localhost:8080/api/v1/clusters/hdp26-jupyter-hive/requests/1 | grep progress_percent | awk '{print $3}' | cut -d . -f 1`
while [[ `echo $ProgressPercent | grep -v 100` ]]; do
  ProgressPercent=`curl -s --user admin:admin -X GET http://localhost:8080/api/v1/clusters/hdp26-jupyter-hive/requests/1 | grep progress_percent | awk '{print $3}' | cut -d . -f 1`
  echo " Progress: $ProgressPercent %"
  sleep 10
done


cat << EOF > /home/vagrant/shutdown_components.sh
#!/bin/bash
#stop all services
curl -u admin:admin -i -H 'X-Requested-By: ambari' -X PUT \
   -d '{"RequestInfo":{"context":"_PARSE_.STOP.ALL_SERVICES","operation_level":{"level":"CLUSTER","cluster_name":"hdp26-jupyter-hive"}},"Body":{"ServiceInfo":{"state":"INSTALLED"}}}' \
http://localhost:8080/api/v1/clusters/hdp26-jupyter-hive/services
EOF
chmod +x /home/vagrant/shutdown_components.sh


cat << EOF > /tmp/test.csv
store_id,sales
100,15000000
200,20000000
300,18000000
EOF


cat << EOF > /tmp/sample.sql
CREATE EXTERNAL TABLE sample (
  store_id INT,
  sales INT
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES (
   "separatorChar" = ",",
   "quoteChar"     = '"'
)
stored as textfile
LOCATION '/user/hive'
tblproperties ("skip.header.line.count"="1");

LOAD DATA LOCAL INPATH '/tmp/test.csv' OVERWRITE INTO TABLE sample;

select * from sample;
EOF


# upload sample content
sudo -u hive hdfs dfs -put /tmp/sample.sql /user/hive
sudo -u hive hdfs dfs -ls /user/hive

# create table and select
beeline -u 'jdbc:hive2://localhost:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2' -n hive -f /tmp/sample.sql


# install anaconda & jupyterlab
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
chmod +x Anaconda3-5.1.0-Linux-x86_64.sh
./Anaconda3-5.1.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip
pip install jupyterlab

# install pyhive
apt-get -y install build-essential libsasl2-dev
pip install pyhive[hive]


useradd py
mkdir -p /home/py
chown -R py:py /home/py
sudo -u py bash -c "mkdir /home/py/.jupyter"
sudo -u py bash -c "cat << EOF > /home/py/.jupyter/jupyter_notebook_config.py
conf = get_config()
conf.NotebookApp.ip = '*'
conf.NotebookApp.open_browser = False
conf.NotebookApp.port = 8888
conf.NotebookApp.token = 'jupyter'
EOF"

cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
ExecStartPre=source /opt/anaconda/bin/activate
ExecStart=/opt/anaconda/bin/jupyter lab
User=py
Group=py
WorkingDirectory=/home/py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable jupyter
sudo systemctl start jupyter



echo 'ambari -> http://192.168.1.117:8080'
echo 'user/password -> admin/admin'
echo ''
echo 'jupyter -> http://192.168.55.117:8888/?token=jupyter'

SHELL
end

〇動作確認用コード
from pyhive import hive
cursor = hive.connect('localhost').cursor()
cursor.execute('SELECT * FROM sample')
print(cursor.fetchall())

VagrantでVisualStudio Code、Gnomeデスクトップ環境とXRDPがインストールされた仮想マシン(Ubuntu18.04)を構築する

VisualStudio Codeはオープンソースのテキストエディターです。

○VisualStudio Codeの画面


○構築方法
以下のVagrantfileを使用して、VisualStudio Code、Gnomeデスクトップ環境デスクトップ環境をインストールした仮想マシン(Ubuntu18.04)を構築する事ができます。
XRDPがインストールされているので、Windowsのリモートデスクトップで接続することができます。ユーザ名はVagrant、パスワードもVagrantでログオンできます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804gnomevscode"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804gnomevscode"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.gui = true
  end
  # bridge netwrok
config.vm.network "public_network", ip: "192.168.1.103", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
sed -i.bak -e "s#http://archive.ubuntu.com/ubuntu#http://ftp.riken.jp/pub/Linux/ubuntu#g" /etc/apt/sources.list
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
apt-get -y install language-pack-ja
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106

apt-get -y install xrdp fcitx-mozc ubuntu-desktop  virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11
im-config -n fcitx

sed -i -e "s/allowed_users=console/allowed_users=anybody/" /etc/X11/Xwrapper.config

cat << EOF >> /etc/polkit-1/localauthority/50-local.d/allow-colord.pkla
[Allow colord for all users]
Identity=unix-user:*
Action=org.freedesktop.color-manager.create-device;org.freedesktop.color-manager.create-profile;org.freedesktop.color-manager.delete-device;org.freedesktop.color-manager.delete-profile;org.freedesktop.color-manager.modify-device;org.freedesktop.color-manager.modify-profile
ResultAny=no
ResualtInactive=no
ResultActive=yes
EOF
systemctl restart polkit

# install VisualStudio Code
apt-get -y install libgconf-2-4 libnotify4 libnss3 libxkbfile1 libgdk-pixbuf2.0-0 libsecret-1-0 libnspr4 libsecret-common libgdk-pixbuf2.0-bin libjpeg8 libtiff5 libx11-6 libgdk-pixbuf2.0-common libjpeg-turbo8 libjbig0 libxcb1 libx11-data
wget -O vsc.deb https://go.microsoft.com/fwlink/?LinkID=760868
dpkg -i vsc.deb

init 5
SHELL
end

DockerでTextpatternとPercona Serverがインストールされたコンテナ(Alpine 3.7)を構築する

TextpatternはPHP製のCMSです。

○Textpatternの画面



○構築方法
以下の手順で、TextpatternとPercona Serverのコンテナを構築・実行します。

1. Textpatternイメージの作成(Dockerfileがあるフォルダで実行)
docker build -t textpattern .

Dockerfile
FROM alpine:3.7
WORKDIR /
RUN  apk update \
  && apk add --nocache apache2 php7-apache2 php7-mysqli php7-pdo php7-mbstring php7-session php7-curl php7-json php7-xml php7-zip openssl openrc \
  && rm -rf /var/cache/apk/* \
  && wget https://textpattern.com/file_download/74/textpattern-4.6.2.tar.gz \
  && tar xvfz textpattern-4.6.2.tar.gz \
  && rm -f textpattern-4.6.2.tar.gz \
  && mkdir /opt \
  && mv textpattern-4.6.2 /opt/textpattern \
  && chown -R apache:apache /opt/textpattern \
  && mkdir -p /run/apache2 \
  && echo $'\n\
Options Indexes FollowSymLinks \n\
AllowOverride All\n\
Require all granted\n\
' >> /etc/apache2/httpd.conf \
  && ln -s /opt/textpattern /var/www/localhost/htdocs \
  && sed -i -e 's|#LoadModule rewrite_module modules/mod_rewrite.so|LoadModule rewrite_module modules/mod_rewrite.so|' /etc/apache2/httpd.conf
EXPOSE 80
VOLUME /opt/textpattern
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

2. TextpatternとMariaDBコンテナの構築・実行(docker-compose.ymlがあるフォルダで実行)
docker-compose up -d

docker-compose.yml
version: "2"
services:
  textpattern:
    image: textpattern
    container_name: "textpattern"
    volumes:
      - "textpattern-data:/opt/textpattern"
    ports:
      - "80:80"
    depends_on:
      - db
  db:
    image: percona:5.7
    container_name: "textpattern-db"
    volumes:
      - "db-data:/var/lib/mysql"
    environment:
        MYSQL_DATABASE: textpattern
        MYSQL_ROOT_PASSWORD: textpattern
volumes:
  db-data:
    driver: local
  textpattern-data:
    driver: local

3. ブラウザからhttp://<Dockerホスト名またはIP>/textpattern/textpattern/setup/index.phpにアクセス
インストール言語として日本語を選択して、submitボタンをクリックする


4. 以下の項目を入力し、「次へ」ボタンをクリックする
MySQLログイン: root
MySQLパスワード:textpattern
MySQLサーバ:db
MySQLデータベース:textpattern
テーブルプリフィックス:(空のまま)
http(s)://:<Dockerホスト名またはIP>/textpattern


5. 画面に表示されたconfig.phpの内容をコピーして、config.phpとして保存して、以下のコマンドを実行する
docker cp config.php textpattern:/opt/textpattern/textpattern
実行後、「行いました!」ボタンをクリックする


6. 管理者ユーザの情報を入力、管理ページのテーマを選択して「次へ」ボタンをクリックする


7. 完了画面から「今すぐログイン」ボタンをクリックする



○関連情報
・Textpatternに関する他の記事はこちらを参照してください。