分享

Hadoop之Hive本地与远程mysql数据库管理模式安装手册

问题导读:
1、Hive如何高效的安装?


2、MYsql安装,需要注意哪些问题?
3、Hive和MySQL如何有效的整合?




一、环境描述
Mysql版本:mysql-installer-community-5.5.27.1   32位
Mysql  for  Windows 7  32位:我把mysql数据库安装在了自己win7的笔记本上,这样的好处就是减少了虚拟机 master  slave的开销和使用空间还可以多利用一台机器的资源,如果你的虚拟机资源很紧张的话也可以这样部署。
Linux ISO:CentOS-6.0-i386-bin-DVD.iso 32位
JDK version:"1.6.0_25-ea"
Hadoop software version:hadoop-0.20.205.0.tar.gz
Hbase version:hbase-0.90.5
Hive version:hive-0.8.1.tar.gz


二、Mysql数据库注意事项
1.安装mysql数据库步骤略过,虚拟机网络设置成host-only模式,两边系统都关闭防火墙,windows系统的360软件最好也关闭,有可能会影响通信。
数据库用户/密码:root/root  和 hive/hive
数据库端口:3306
Mysqll服务名:MySQL55        
以将MySQL服务器安装成服务。安装成服务,系统启动时可以自动启动MySQL服务器
目前设置为不随操作系统启动而自动启动
启动/关闭mysql数据库服务命令,必须启动服务才能操作数据库
net start mysql  net stop mysql
2.查看Mysql版本(注意 V大写)

3.登陆Mysql,从windows上登录
155836912.jpg

4.在mysql上建立hive用户并授予足够的权限
本人使用的是windows上mysql自带的MySQL workbench客户端软件创建的用户
155836350.jpg

Add Account :添加一个用户
Name:hive   密码  hive
Limit  Connectivity  to  Hosts  Matching:%     百分号是通配符意思,就是说不在限制登录的主机,你从任何一台客户机都可以登录,如果写上ip地址说明此用户只能从这个ip地址的机器上登录mysql数据库

55.jpg

全部勾选授予all权限
命令方式:Create  user  hive  identified  by  hive;


Hadoop部署环境
主机名
IP
节点名
备注
h1
192.168.2.102
master
namenodejobtracker
h2
192.168.2.103
slave1
datanodetasktracker
H4
192.168.2.105
slave2
datanodetasktracker


Windows               192.168.2.110           Mysql                  window7部署mysql
master上测试是否能成功连接远程mysql
  1. [grid@h1 grid]$ ping 192.168.2.110
  2. PING 192.168.2.110 (192.168.2.110) 56(84) bytes of data.
  3. 64 bytes from 192.168.2.110: icmp_seq=1 ttl=64 time=0.603 ms    是通的
  4. 64 bytes from 192.168.2.110: icmp_seq=2 ttl=64 time=0.283 ms
  5. 64 bytes from 192.168.2.110: icmp_seq=3 ttl=64 time=0.301 ms
  6. --- 192.168.2.110 ping statistics ---
  7. 3 packets transmitted, 3 received, 0% packet loss, time 2010ms
  8. rtt min/avg/max/mdev = 0.283/0.395/0.603/0.148 ms
  9. [grid@h1 grid]$ mysql -h192.168.2.110 -uhive -phive
  10. Welcome to the MySQL monitor.  Commands end with ; or \g.
  11. Your MySQL connection id is 11
  12. Server version: 5.5.27 MySQL Community Server (GPL)
  13. Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
  14. This software comes with ABSOLUTELY NO WARRANTY. This is free software,
  15. and you are welcome to modify and redistribute it under the GPL v2 license
  16. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  17. mysql>
复制代码

5.在mysql上创建hive数据库用于存放hive元数据(即数据字典)
查看mysql数据库软件中都存在哪些数据库,现在一共有6个
  1. mysql> show databases;
  2. +--------------------+
  3. | Database           |
  4. +--------------------+
  5. | information_schema |
  6. | mysql              |
  7. | performance_schema |
  8. | sakila             |
  9. | test               |
  10. | world              |
  11. +--------------------+
  12. 6 rows in set (0.01 sec)
复制代码

打开名为mysql的数据库,只有打开才能操作哦
  1. mysql> use mysql;
  2. Database changed   
复制代码
              
查看现在的数据库中存在什么表,一共有24张
  1. mysql> show tables;
  2. +---------------------------+
  3. | Tables_in_mysql           |
  4. +---------------------------+
  5. | columns_priv              |
  6. | db                        |
  7. | event                     |
  8. | func                      |
  9. | general_log               |
  10. | help_category             |
  11. | help_keyword              |
  12. | help_relation             |
  13. | help_topic                |
  14. | host                      |
  15. | ndb_binlog_index          |
  16. | plugin                    |
  17. | proc                      |
  18. | procs_priv                |
  19. | proxies_priv              |
  20. | servers                   |
  21. | slow_log                  |
  22. | tables_priv               |
  23. | time_zone                 |
  24. | time_zone_leap_second     |
  25. | time_zone_name            |
  26. | time_zone_transition      |
  27. | time_zone_transition_type |
  28. | user                      |
  29. +---------------------------+
  30. 24 rows in set (0.01 sec)
复制代码

创建名为hive的库
  1. mysql> create database hive;         
  2. Query OK, 1 row affected (0.00 sec)
复制代码

查看是否创建成功(查看mysql数据库软件中都存在哪些数据库)
  1. mysql> show database;
  2. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
  3. mysql> show databases;
  4. +--------------------+
  5. | Database           |
  6. +--------------------+
  7. | information_schema |
  8. | hive               |             这里已经创建hive库成功
  9. | mysql              |
  10. | performance_schema |
  11. | sakila             |
  12. | test               |
  13. | world              |
  14. +--------------------+
  15. 7 rows in set (0.00 sec)
复制代码

打开名为hive的数据库
  1. mysql> use hive;
  2. Database changed
  3. mysql> show tables;             检查hive库有哪些表,当然现在还没有创建目前没有表
  4. Empty set (0.00 sec)
复制代码

三、Hive远程模式安装
1.先把hive-0.8.1.tar.gz包上传到 master:/home/grid 目录下然后解包
  1. [grid@h1 grid]$ tar -zxvf hive-0.8.1.tar.gz
复制代码

2.把mysql-connector-java-5.1.22-bin.jar包复制到hive的lib目录下
  1. [grid@h1 grid]$ cp mysql-connector-java-5.1.22-bin.jar hive-0.8.1/lib/
复制代码

3.配置环境变量
  1. [grid@h1 grid]$ vim .bashrc
  2. export HIVE_HOME=/home/grid/hive-0.8.1
复制代码


4.修改/home/grid/hive-0.8.1/bin/hive-config.sh
  1. [grid@h1 bin]$ pwd
  2. /home/grid/hive-0.8.1/bin
  3. [grid@h1 bin]$ vim hive-config.sh
  4. # hive-config.sh
  5. export JAVA_HOME=/usr/java/jdk1.6.0_25
  6. export HIVE_HOME=/home/grid/hive-0.8.1
  7. export HADOOP_HOME=/home/grid/hadoop-0.20.2     hive捆绑hadoop集群,必须知道hadoop配置文件位置
复制代码


5.根据hive-default.xml 复制 hive-site.xml 核心配置文件,修改内容
  1. cp  hive-default.xml  hive-site.xml
  2. [grid@h1 conf]$ vim hive-site.xml
  3. <property>
  4.   <name>javax.jdo.option.ConnectionURL</name>
  5.   <value>jdbc:mysql://192.168.2.110:3306/hive?createDatabaseIfNotExist=true</value>
  6.   <description>JDBC connect string for a JDBC metastore</description>
  7. </property>
  8. 指出连接mysql的方法
  9. <property>
  10.   <name>javax.jdo.option.ConnectionDriverName</name>
  11.   <value>com.mysql.jdbc.Driver</value>
  12.   <description>Driver class name for a JDBC metastore</description>
  13. </property>
  14. Mysq连接驱动程序
  15. <property>
  16.   <name>javax.jdo.option.ConnectionUserName</name>
  17.   <value>hive</value>
  18.   <description>username to use against metastore database</description>
  19. </property>
  20. Mysql登陆用户名
  21. <property>
  22.   <name>javax.jdo.option.ConnectionPassword</name>
  23.   <value>hive</value>
  24.   <description>password to use against metastore database</description>
  25. </property>
  26. Mysql登陆密码
  27. <property>
  28.   <name>hive.metastore.local</name>
  29.   <value>true</value>这是本地   <value>false</value>这是远程
  30.   <description>controls whether to connect to remove metastore server or open a new metastore server in Hive Client JVM</description>
  31. </property>
  32. 使用远程数据库,这里的false是指hive服务和metastore元数据存储服务不在一个进程中,两个各自独立它们两个可以放在不同的机器上,如果是true说明这2个服务是合并在一个进程中的,mysql数据库是独立的一个进程,即可在本地机器也可以在远程机器
  33. <property>
  34.   <name>hive.metastore.warehouse.dir</name>
  35.   <value>/user/hive/warehouse</value>
  36.   <description>location of default database for the warehouse</description>
  37. </property>
  38. Hive表数据存放目录,每一个表对应一个目录,这是自带默认值
  39. <property>
  40.   <name>hive.exec.scratchdir</name>
  41.   <value>/tmp/hive-${user.name}</value>
  42.   <description>Scratch space for Hive jobs</description>
  43. </property>
  44. Hive临时文件存放的目录,这个也不用管,使用默认值即可
  45. 这句话是自己添加的,原文里没有
  46. <property>
  47.   <name>hive.metastore.uris</name>
  48.   <value>thrift://127.0.0.1:9083</value>
  49. </property>
复制代码
Thrift:是hive的通信协议
127.0.0.1:9083  是指在哪台机器上启动hive 服务,只有启动服务才能正常使用hive

6.配置hive-log4j.properties
log4j.appender.EventCounter=org.apache.hadoop.log.metrics.EventCounter   修改这一行
这里请注意,我修改后启动hive报错,恢复原状就没事了,大家可以试试

7.检查hadoop集群状态
  1. [grid@h1 conf]$ hadoop dfsadmin -report
  2. Configured Capacity: 19865944064 (18.5 GB)
  3. Present Capacity: 8831234048 (8.22 GB)
  4. DFS Remaining: 8816201728 (8.21 GB)
  5. DFS Used: 15032320 (14.34 MB)
  6. DFS Used%: 0.17%
  7. Under replicated blocks: 4
  8. Blocks with corrupt replicas: 0
  9. Missing blocks: 0
  10. -------------------------------------------------
  11. Datanodes available: 2 (2 total, 0 dead)             没有问题2个slave运行正常
  12. Name: 192.168.2.103:50010
  13. Decommission Status : Normal
  14. Configured Capacity: 9932972032 (9.25 GB)
  15. DFS Used: 7516160 (7.17 MB)
  16. Non DFS Used: 5402759168 (5.03 GB)
  17. DFS Remaining: 4522696704(4.21 GB)
  18. DFS Used%: 0.08%
  19. DFS Remaining%: 45.53%
  20. Last contact: Mon Nov 19 17:51:50 CST 2012
  21. Name: 192.168.2.105:50010
  22. Decommission Status : Normal
  23. Configured Capacity: 9932972032 (9.25 GB)
  24. DFS Used: 7516160 (7.17 MB)
  25. Non DFS Used: 5631950848 (5.25 GB)
  26. DFS Remaining: 4293505024(4 GB)
  27. DFS Used%: 0.08%
  28. DFS Remaining%: 43.22%
  29. Last contact: Mon Nov 19 17:51:48 CST 2012
复制代码

8.修改mysql字符集
  1. mysql> use hive;
  2. Database changed
  3. mysql> alter database hive character set latin1;
  4. Query OK, 1 row affected (0.11 sec)
复制代码

9.启动hive
  1. [grid@h1 bin]$ pwd
  2. /home/grid/hive-0.8.1/bin
  3. [grid@h1 bin]$ ./hive
  4. [grid@h1 bin]$ ./hive
  5. Logging initialized using configuration in file:/home/grid/hive-0.8.1/conf/hive-log4j.properties
  6. Hive history file=/tmp/grid/hive_job_log_grid_201211191942_411407039.txt
  7. hive> show tables;                           显示表,没有创建当然没有
  8. OK
  9. Time taken: 3.2 seconds
  10. hive> create table leo1 (x int,y int);              创建表
  11. OK
  12. Time taken: 1.191 seconds
  13. hive> show tables;                           可以显示出来了
  14. OK
  15. leo1
  16. Time taken: 0.148 seconds
复制代码

已经进入hive模式,还可以创建表
注明:
当你设置的为本地模式时候
  1. <property>
  2.   <name>hive.metastore.local</name>
  3.   <value>true</value>
  4.   <description>controls whether to connect to remove metastore server or open a new metastore server in Hive Client JVM</description>
  5. </property>
  6. [grid@h1 bin]$ ./hive
  7. Logging initialized using configuration in file:/home/grid/hive-0.8.1/conf/hive-log4j.properties
  8. Hive history file=/tmp/grid/hive_job_log_grid_201211231243_664195168.txt
  9. hive> show tables;
  10. OK
  11. leo1
  12. Time taken: 21.218 seconds
复制代码

直接进入hive命令行shell,后台自动启动metestore服务和hive服务而且这2个服务还是同在一个进程中,不用手工启动,这点和远程模式还是有点不同的。
当你设置的为远程模式时候
  1. [grid@h1 bin]$ ./hive --service hiveserver       这是启动hive服务
  2. Starting Hive Thrift Server
  3. [grid@h1 bin]$ ./hive --service metastore        这是启动元数据存储服务
  4. Starting Hive Metastore Server
复制代码

正常情况是都在这卡住了,相当于前台启动,如果有异常请关闭当前窗口,另开一个窗口重新启动这2个服务。为什么要手动启动这2个服务呢,因为这2个服务都是相互独立的进程,可以部署在不同的机器上
  1. [grid@h1 bin]$ ./hive
  2. Logging initialized using configuration in file:/home/grid/hive-0.8.1/conf/hive-log4j.properties
  3. Hive history file=/tmp/grid/hive_job_log_grid_201211231324_199334603.txt
  4. hive> show tables;                   这里显示创建了2个表,这2个表都是建在HDFS文件系统上的
  5. OK                                   mysql只保存hive用户创建表的元数据
  6. leo1
  7. leo2
  8. Time taken: 0.538 seconds
  9. hive> create table leo2 (x int);
  10. OK
  11. Time taken: 0.317 seconds
复制代码

进入mysql数据库查看hive创建表的元数据
  1. mysql> use hive;
  2. Database changed
  3. mysql> show tables;
  4. +--------------------+
  5. | Tables_in_hive     |
  6. +--------------------+
  7. | bucketing_cols     |
  8. | cds                |
  9. | columns_v2         |
  10. | database_params    |
  11. | dbs                |
  12. | idxs               |
  13. | index_params       |
  14. | part_col_privs     |
  15. | part_privs         |
  16. | partition_key_vals |
  17. | partition_keys     |
  18. | partition_params   |
  19. | partitions         |
  20. | sd_params          |
  21. | sds                |
  22. | sequence_table     |
  23. | serde_params       |
  24. | serdes             |
  25. | sort_cols          |
  26. | table_params       |
  27. | tbl_col_privs      |
  28. | tbl_privs          |
  29. | tbls               |
  30. +--------------------+  红色的就是hive的数据字典表,这些表第一次连接时就已经创建了,后面就会一直存在,只是里面的元数据内容会跟着hive操作而变化。
  31. 23 rows in set (0.00 sec)
  32. mysql> select * from tbls;
  33. +--------+-------------+-------+------------------+-------+-----------+-------+----------+---------------+--------------------+--------------------+
  34. | TBL_ID | CREATE_TIME | DB_ID | LAST_ACCESS_TIME | OWNER | RETENTION | SD_ID | TBL_NAME | TBL_TYPE      | VIEW_EXPANDED_TEXT | VIEW_ORIGINAL_TEXT |
  35. +--------+-------------+-------+------------------+-------+-----------+-------+----------+---------------+--------------------+--------------------+
  36. |      6 |  1353597034 |     1 |                0 | grid  |         0 |     6 | leo1     | MANAGED_TABLE | NULL               | NULL               |
  37. |     11 |  1353648090 |     1 |                0 | grid  |         0 |    11 | leo2     | MANAGED_TABLE | NULL               | NULL               |
  38. +--------+-------------+-------+------------------+-------+-----------+-------+----------+---------------+--------------------+--------------------+
  39. 2 rows in set (0.00 sec)
  40. hive> drop table leo2;        我删除了 leo2表
  41. OK
  42. Time taken: 1.796 seconds
  43. mysql> select * from tbls;     注意提示符
  44. +--------+-------------+-------+------------------+-------+-----------+-------+----------+---------------+--------------------+--------------------+
  45. | TBL_ID | CREATE_TIME | DB_ID | LAST_ACCESS_TIME | OWNER | RETENTION | SD_ID | TBL_NAME | TBL_TYPE      | VIEW_EXPANDED_TEXT | VIEW_ORIGINAL_TEXT |
  46. +--------+-------------+-------+------------------+-------+-----------+-------+----------+---------------+--------------------+--------------------+
  47. |      6 |  1353597034 |     1 |                0 | grid  |         0 |     6 | leo1     | MANAGED_TABLE | NULL               | NULL               |
  48. +--------+-------------+-------+------------------+-------+-----------+-------+----------+---------------+--------------------+--------------------+
  49. 1 row in set (0.00 sec)
复制代码
Leo2表的元数据也跟着没有了

10.使用web浏览器访问hive
  1. [grid@h1 bin]$ ./hive --service hwi           启动hwi服务
  2. 12/11/23 13:50:45 INFO hwi.HWIServer: HWI is starting up
  3. 12/11/23 13:50:45 WARN conf.HiveConf: DEPRECATED: Ignoring hive-default.xml found on the CLASSPATH at /home/grid/hive-0.8.1/conf/hive-default.xml
  4. 12/11/23 13:50:45 INFO mortbay.log: Logging to org.slf4j.impl.Log4jLoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog
  5. 12/11/23 13:50:45 INFO mortbay.log: jetty-6.1.14
  6. 12/11/23 13:50:46 INFO mortbay.log: Extract jar:file:/home/grid/hive-0.8.1/lib/hive-hwi-0.8.1.war!/ to /tmp/Jetty_192_168_2_102_9999_hive.hwi.0.8.1.war__hwi__.4m0x2o/webapp
  7. 12/11/23 13:50:47 INFO mortbay.log: Started SocketConnector@192.168.2.102:9999
  8. 12/11/23 13:51:21 INFO hive.metastore: Trying to connect to metastore with URI thrift://127.0.0.1:9083
  9. 12/11/23 13:51:21 INFO hive.metastore: Connected to metastore.
  10. 12/11/23 13:51:21 ERROR hive.metastore: Unable to shutdown local metastore client
  11. 12/11/23 13:51:21 ERROR hive.metastore: [Ljava.lang.StackTraceElement;@16ef71
  12. Hive history file=/tmp/grid/hive_job_log_grid_201211231351_1262762954.txt
复制代码

这个服务也是前台启动,在这里就卡死了,不用管
启动浏览器在地址栏:http://192.168.2.102:9999/hwi

666.jpg




最后,感谢原作者的分享,转自刘盛


已有(1)人评论

跳转到指定楼层
刚果 发表于 2015-9-11 16:23:48
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条