Tuesday, August 16, 2011

Using MySQL as a Hive backend database


Hive let us use a SQL like (HiveQL) style to analyse large datasets with ad-hoc queries, and comes as a service on top of hdfs. It is easy to use and most SQL programmers can instant write some queries.
The lack of the installation are the included derby DB, which is running on the node locally. For that Hive is not really multiuser-capable.

To use Hive with more than one user you have to setup a backend database. The database will hold all metainformations regarding your tables, partitions, splits and rows. For that the database should be safe (maybe replication) or a HVA installation. I use 2 MySQL servers in a ESX Cluster environment with enabled binary logs (Active/Standby).

Setup a server and install mysql-server version 5.1 and up. To get absolute safe you can setup a MySQL cluster ;) Let us configure the mysql-database:

# cat /etc/my.cnf
[mysqld_safe]
socket      = /var/lib/mysql/mysql.sock
[mysqld]
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/lib/mysql/mysql.sock
log-error   = /var/log/mysqld.log
datadir     = /opt/hadoop/mysql
default-storage_engine  = InnoDB
skip-bdb                = 1
old_passwords           = 0
skip_name_resolve
connect_timeout     = 30
wait_timeout        = 30
interactive_timeout = 100
key_buffer          = 128M
thread_concurrency  = 4
thread_cache        = 16
thread_stack        = 256K
table_cache         = 512
tmp_table_size      = 64M
max_heap_table_size = 64M
server-id           = 1001
log_bin             = /var/log/mysql/mysqlserver-bin.log
expire_logs_days    = 3
max_binlog_size     = 256M
innodb_file_per_table           = 1
innodb_data_file_path           = ibdata1:10M:autoextend
innodb_buffer_pool_size         = 512M
innodb_log_file_size            = 16M
innodb_flush_log_at_trx_commit  = 1
long_query_time     = 2
log_slow_queries    = /var/log/mysql/mysql-slow.log
query_cache_size    = 64M
query_cache_type    = 1
query_cache_limit   = 4M

You see, we use binary logging, as engine InnoDB (no locking tables) and some RAM related params. Consult the mysql manual for a complete description (would be a larger post).

Restart your mysqlserver (service mysqld restart). Now create the user and table:
mysql> CREATE USER 'USER'@'%' IDENTIFIED BY 'PASSWORD';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'USER'@'%' WITH GRANT OPTION;
mysql> create DATABASE hive-live;

Thats all. I know, very low security. But the server should only hold one database.

Setup hive to use the metastore and add in hive-default.xml:
# vi /etc/hive/conf/hive-default.xml
<!-- add mysql metastore -->
<property>
  <name>javax.jdo.option.ConnectionURL</name>
  <value>jdbc:mysql://mysqlserver:3306/hive_live_new?createDatabaseIfNotExist=true</value>
</property>
<property>
  <name>javax.jdo.option.ConnectionDriverName</name>
  <value>com.mysql.jdbc.Driver</value>
</property>
<property>
  <name>javax.jdo.option.ConnectionUserName</name>
  <value>USER</value>
</property>
<property>
  <name>javax.jdo.option.ConnectionPassword</name>
  <value>PASSWORD</value>
</property>
<!-- end -->

To get Hive running with the mysql-driver, you have to download them [1] and copy across your cluster:
for i in $(cat /etc/hadoop-0.20/conf/slaves); do scp -r /usr/lib/hive/lib/mysql-connector-java-5.1.11-bin.jar $i:/usr/lib/hive/lib/; done

Now copy the hive-config too:
for i in $(cat /etc/hadoop-0.20/conf/slaves); do scp -r /etc/hive/conf/hive-* $i:/etc/hive/conf.dist/; done

Thats all. If you use hive now the first statement should take a while, because hive will create the schema at the backend. Let us check:

mysql> use hive_live;
mysql> show tables;
+-------------------------+
| Tables_in_hive_live     |
+-------------------------+
| BUCKETING_COLS          |
| COLUMNS                 |
| DATABASE_PARAMS         |
| DBS                     |
| DB_PRIVS                |
| GLOBAL_PRIVS            |
| IDXS                    |
| INDEX_PARAMS            |
| PARTITIONS              |
| PARTITION_KEYS          |
| PARTITION_KEY_VALS      |
| PARTITION_PARAMS        |
| PART_COL_PRIVS          |
| PART_PRIVS              |
| ROLES                   |
| ROLE_MAP                |
| SDS                     |
| SD_PARAMS               |
| SEQUENCE_TABLE          |
| SERDES                  |
| SERDE_PARAMS            |
| SORT_COLS               |
| TABLE_PARAMS            |
| TBLS                    |
| TBL_COL_PRIVS           |
| TBL_PRIVS               |
| TYPES                   |
| TYPE_FIELDS             |
+-------------------------+
28 rows in set (0.01 sec)

If you have to ugrade the schema (hive-update maybe), don't be worry. Hive comes with SQL-statements, take a look at /var/lib/hive/metastore/scripts/upgrade/mysql/. To apply a schema cd into the directory and open mysql-cli:
mysql> source /var/lib/hive/metastore/scripts/upgrade/mysql/hive-schema-0.7.0.mysql.sql <enter>

The script will load all sql-statements they will need to upgrade the database. A good idea is a full DB backup before.


[1] http://dev.mysql.com/downloads/connector/j/