一.环境搭建(Hadoop+Hive)
window7: eclipse4.5.1
linux: hadoop-01 192.168.33.01
Hadoop-2.7.2+zookeeper-3.4.6完全分布式环境搭建(HDFS、YARN HA)
Hadoop2.7实战v1.0之Hive-2.0.0+MySQL远程模式安装
Hadoop2.7实战v1.0之Hive-2.0.0的Hiveserver2服务和beeline远程调试
hadoop-01 要有 Hdfs namenode(active),Yarn resourcemanager(active),Hive metastore 和hiveserver2进程.
二.启动hive metastore和hiveserver2服务命令
[root@hadoop-01 bin]# hive --service metastore &
[root@hadoop-01 bin]# hive --service hiveserver2 &
三.搭建
1.下载解压hadoop和hive
hadoop-2.7.2.tar.gz到window7的D:\Hadoop\hadoop-2.7.2
apache-hive-2.0.0-bin.tar.gz 到window7的D:\Hadooop\apache-hive-2.0.0-bin
2.启动eclipse,新建工程HiveCaseProject,导入需要的jar包
a.导入hive的lib文件夹下所有的包
b.导入hadoop的D:\Hadoop\hadoop-2.7.2\share\hadoop\common\hadoop-common-2.7.2.jar
3.新建hiveserver2Test.class文件
4.hiveserver2Test.class示例代码
点击(此处)折叠或打开
- package com.hadoop.hive;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- public class hiveserver2Test {
- public boolean run() {
- Connection con=null;
- Statement stmt =null;
- ResultSet rs=null;
- String tableName="studentinfo";//表名
-
- try {
- String driver="org.apache.hive.jdbc.HiveDriver";//Hive的驱动字符串
- Class.forName(driver); //加载Hive驱动
- con = DriverManager.getConnection("jdbc:hive2://192.168.33.01:10000", "root", "");//获取hive2的jdbc连接,注意默认的数据库是default
- stmt = con.createStatement();
- rs=stmt.executeQuery("select * from "+tableName+" ");//查询所有,直接运行
- while(rs.next()){
- System.out.println("id:"+rs.getString(1)+" "+"name:"+rs.getString(2)+" "+"age:"+rs.getString(3)+" "+"tel:"+rs.getString(4)+" ");
- }
-
- System.out.println("查询成功!");
- stmt.close();
- con.close();
-
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("error");
- return false;
-
- }
- }
-
- public static void main(String[] args) throws SQLException {
- hiveserver2Test hiveJdbcClient = new hiveserver2Test();
- hiveJdbcClient.run();
- }
-
- }
5.第一次调试--Error
错误1: StaticLoggerBinder.class冲突,移除log4j-slf4j-impl-2.4.1.jar即可
错误2: ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
这句意思就是没有找到log4j2的配置文件
6.配置log4j2文件
http://www.cnblogs.com/morvenhuang/p/3958086.html
a.由于hive的lib目录有log4j2相关包,在第2.2步已经导入,无需再导入
b.创建resources文件夹,然后在resources文件夹下创建log4j2.xml文件
log4j2.xml文件
点击(此处)折叠或打开
- <?xml version="1.0" encoding="UTF-8"?>
- <Configuration status="warn">
- <Appenders>
- <Console name="Console" target="SYSTEM_OUT">
- <PatternLayout pattern="%m%n" />
- </Console>
- </Appenders>
- <Loggers>
- <Root level="INFO">
- <AppenderRef ref="Console" />
- </Root>
- </Loggers>
- </Configuration>
c. 把resources文件夹添加到class中