搭建Hadoop2.7.3+Hive2.1.1及MySQL(配置Hive+MySQL+Connector)(三)

准备工作下载最新连接器地址

https://dev.mysql.com/downloads/connector/j/

例子:下载mysql-connector-java-5.1.41.tar

1、解压连接器connector文件
1.1、解压

[plain]  view plain  copy
  1. [root@localhost Software]# tar xzfmysql-connector-java-5.1.41.tar.gz  
  2. [root@localhost Software]# cd mysql-connector-java-5.1.41/  

1.2、查看文件夹

[plain]  view plain  copy
  1. [root@localhostmysql-connector-java-5.1.41]# ll  

1.3、Copy到hive/lib路径下

[plain]  view plain  copy
  1. [root@localhost Software]# cpmysql-connector-java-5.1.41/mysql-connector-java-5.1.41-bin.jar/usr/hive/lib/mysql-connector-java-5.1.41-bin.jar  


2、登陆MySQL创建数据库:hive_db(注意配置hive-site.xml时有指定)
2.1、用户名:root 密码:password,另开一个终端登陆MySQL,创建数据库hive_db

[plain]  view plain  copy
  1. [root@localhost hive]# mysql -u root -ppassword  

 mysql> create database hive_db;

3、改配置文件hive-site.xml
以下只列出改动的配置项,其它保留默认
[html]  view plain  copy
  1. <configuration>  
  2.     <property>  
  3.         <name>hive.metastore.warehouse.dir</name>  
  4.         <value>/usr/hive/warehouse</value>  
  5.         <description>location of default database for the warehouse</description>  
  6.     </property>  
  7.     <property>  
  8.         <name>hive.metastore.local</name>  
  9.         <value>true</value>  
  10.         <description>Use false if a production metastore server is used</description>  
  11.     </property>  
  12.     <property>  
  13.         <name>hive.exec.scratchdir</name>  
  14.         <value>/tmp/hive</value>  
  15.         <description>HDFS root scratch dir for Hive jobs which gets created with write all (733) permission. For each connecting user, an HDFS scratch dir: ${hive.exec.scratchdir}/<username> is created, with ${hive.scratch.dir.permission}.</description>  
  16.     </property>  
  17.     <property>  
  18.         <name>javax.jdo.option.ConnectionURL</name>  
  19.         <value>jdbc:mysql://localhost:3306/hive_db?createDatabaseIfNoExist=true</value>  
  20.         <description> Roy  
  21.       JDBC connect string for a JDBC metastore.  
  22.       To use SSL to encrypt/authenticate the connection, provide database-specific SSL flag in the connection URL.  
  23.       For example, jdbc:postgresql://myhost/db?ssl=true for postgres database.  
  24.     </description>  
  25.     </property>  
  26.     <property>  
  27.         <name>javax.jdo.option.ConnectionDriverName</name>  
  28.         <value>com.mysql.jdbc.Driver</value>  
  29.         <description>User-Defined(Roy) Driver class name for a JDBC metastore</description>  
  30.     </property>  
  31.     <property>  
  32.         <name>javax.jdo.option.ConnectionUserName</name>  
  33.         <value>root</value>  
  34.         <description>User-defined(Roy)Username to use against metastore database</description>  
  35.     </property>  
  36.     <property>  
  37.         <name>javax.jdo.option.ConnectionPassword</name>  
  38.         <value>password</value>  
  39.         <description>User-defined(Roy)password to use against metastore database</description>  
  40.     </property>  
  41. </configuration>  

4、使用schematool初始化

[plain]  view plain  copy
  1. [root@localhost hive]# schematool -dbType mysql  -initSchema  

--显示成功

[plain]  view plain  copy
  1. schemaTool completed  

5、启动hive服务端程序
5.1、启动hive服务端

[plain]  view plain  copy
  1. [root@localhost hive]# hive --servicemetastore &  

--屏幕提示信息不显示时,按ctrl+c退出

5.2、查看进程信息

[plain]  view plain  copy
  1. [root@localhost hive]# jps  
--显示进程信息多了(RunJar)


[plain]  view plain  copy
  1. 51280 Jps  
  2. 5985 SecondaryNameNode  
  3. 6226 ResourceManager  
  4. 45766 DataNode  
  5. 5753 NameNode  
  6. 51194 RunJar  
  7. 6348 NodeManager  

5.3、有需要时,可启动hive  远程服务 (端口号10000)

[plain]  view plain  copy
  1. [root@localhost hive]# hive --servicehiveserver &  

6、测试环境配置是否成功

6.1、准备导入文本文件/root/桌面/Test/wc-in/a.txt

格式:

[plain]  view plain  copy
  1. 1,h  
  2. 2,i  
  3. 3,v  
  4. 4,e  

6.2、登陆hive成功后,测试创建表

[plain]  view plain  copy
  1. root@localhost hadoop]# hive  
6.2.1、创建表及指定逗号(,)为分隔符

[plain]  view plain  copy
  1. hive> create table a(id int,name string)  
  2.    > row format delimited fields terminated by ',';  
--显示信息

[plain]  view plain  copy
  1. OK  
  2. Time taken: 0.288 seconds  

6.2.2、导入文件a.txt

[plain]  view plain  copy
  1. hive> load data local inpath '/root/桌面/Test/wc-in/a.txt' into table a;  
--显示信息

[plain]  view plain  copy
  1. Loading data to table default.a  
  2. OK  
  3. Time taken: 0.763 seconds  
6.2.3、查看效果

[plain]  view plain  copy
  1. hive> select * from a;  

--显示信息


[plain]  view plain  copy
  1. OK  
  2. 1     h  
  3. 2     i  
  4. 3     v  
  5. 4     e  
  6. Time taken: 0.309 seconds, Fetched: 4row(s)  

6.3、在Hive内使用dfs命令
6.3.1、查看a表dfs存储路径

[plain]  view plain  copy
  1. hive> dfs -ls /usr/hive/warehouse/a;  
--显示信息
[plain]  view plain  copy
  1. Found 1 items  
  2. -rw-r--r--  1 root supergroup         16 2017-03-08 17:46/usr/hive/warehouse/a/a.txt  

6.3.2、查看文件内容

[plain]  view plain  copy
  1. hive> dfs -cat /usr/hive/warehouse/a/*;  

--显示信息

[plain]  view plain  copy
  1. 1,h  
  2. 2,i  
  3. 3,v  
  4. 4,e  

7、登陆MySQL查看创建表

[plain]  view plain  copy
  1. [root@localhost conf]# mysql -u root -ppassword  
[sql]  view plain  copy
  1. mysql> use hive_db;  
  2. mysql> select TBL_ID, CREATE_TIME,DB_ID, OWNER, TBL_NAME,TBL_TYPE from TBLS;  
--显示信息

[plain]  view plain  copy
  1. +--------+-------------+-------+-------+----------+---------------+  
  2. | TBL_ID | CREATE_TIME | DB_ID | OWNER |TBL_NAME | TBL_TYPE      |  
  3. +--------+-------------+-------+-------+----------+---------------+  
  4. |    37 |  1488966386 |     1 | root | a        | MANAGED_TABLE |  
  5. +--------+-------------+-------+-------+----------+---------------+  
  6. 1 row in set (0.03 sec)  


8、在hdfs查看生成文件(同上步骤[6.3])
8.1、查看a表存储路径

[plain]  view plain  copy
  1. [root@localhost hadoop]# hdfs dfs -ls/usr/hive/warehouse/a  
-- 显示信息
[plain]  view plain  copy
  1. Found 1 items  
  2. -rw-r--r--  1 root supergroup         162017-03-08 17:46 /usr/hive/warehouse/a/a.txt  

8.2、查看内容

[plain]  view plain  copy
  1. [root@localhost hadoop]# hdfs dfs -cat  /usr/hive/warehouse/a/*  

--显示信息

[plain]  view plain  copy
  1. 1,h  
  2. 2,i  
  3. 3,v  
  4. 4,e  


 

常见问题处理:

1、启动hive时报错

[plain]  view plain  copy
  1. [root@localhost hive]# hive  

--显示报错信息

[plain]  view plain  copy
  1. Caused by:org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.hdfs.server.namenode.SafeModeException):Cannot create directory /tmp/hive/root/24f1d91f-f32b-47e1-824d-ba26b02bd13e.Name node is in safe mode.  

原因:hadoop为安全模式

--解决方法:

关闭安全模式

[plain]  view plain  copy
  1. [root@localhost hadoop]# hadoop dfsadmin-safemode leave  
--显示信息


[plain]  view plain  copy
  1. DEPRECATED: Use of this script to executehdfs command is deprecated.  
  2. Instead use the hdfs command for it.  
  3.    
  4. Safe mode is OFF  


2、在导入数据时出错信息

[plain]  view plain  copy
  1. hive> load data local inpath '/root/桌面/Test/wc-in/a.txt' into table a;  

--显示报错信息

[plain]  view plain  copy
  1. FAILED: Execution Error, return code 1 fromorg.apache.hadoop.hive.ql.exec.MoveTask.org.apache.hadoop.ipc.RemoteException(java.io.IOException): File/usr/hive/warehouse/a/a_copy_2.txt could only be replicated to 0 nodes insteadof minReplication (=1).  There are 0datanode(s) running and no node(s) are excluded in this operation.  


原因:hadoop没有启动datanote

解决方法:

[plain]  view plain  copy
  1. [root@localhost hive]# start-dfs.sh  
  2. [root@localhost hive]# jps  

--显示信息

[plain]  view plain  copy
  1. 51152 Jps  
  2. 5985 SecondaryNameNode  
  3. 6226 ResourceManager  
  4. 45766 DataNode  
  5. 5753 NameNode  
  6. 6348 NodeManager  
[plain]  view plain  copy
  1.   

应网友要求测个例子:

--调用HiveServer2客户端和beeline命令用法

--启用服务,信息不动时Ctrl+C退出

[plain]  view plain  copy
  1. [root@localhost bin]# hiveserver2   


[plain]  view plain  copy
  1. [root@localhost bin]# beeline  

显示信息如下:
[plain]  view plain  copy
  1. which: no hbase in (/usr/lib64/qt-3.3/bin:/root/perl5/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/usr/hadoop/bin:/usr/hadoop/bin:/usr/hadoop/sbin:/usr/hive/bin:/usr/java/jdk1.8.0_111/bin:/root/bin:/usr/hadoop/bin:/usr/hadoop/sbin:/usr/hive/bin:/usr/java/jdk1.8.0_111/bin)  
  2. Beeline version 2.1.1 by Apache Hive  
  3. beeline>   
连接和登陆账号密码输入:
[plain]  view plain  copy
  1. Connecting to jdbc:mysql://localhost:3306/hive_db  
  2. Enter username for jdbc:mysql://localhost:3306/hive_db: root  
  3. Enter password for jdbc:mysql://localhost:3306/hive_db: ********  
--测试创建表:

[plain]  view plain  copy
  1. 0: jdbc:mysql://localhost:3306/hive_db> create table Test_beeline(id int);  

显示信息:

[html]  view plain  copy
  1. No rows affected (0.044 seconds)  
--查看创建表

[plain]  view plain  copy
  1. 0: jdbc:mysql://localhost:3306/hive_db> show tables;  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值