Hadoop2.7實戰v1.0之Eclipse+Hive2.0.0的JDBC案例(最詳細搭建)

hackeruncle發表於2016-06-09


.環境搭建(Hadoop+Hive)

window7:  eclipse4.5.1

linux: hadoop-01    192.168.33.01

Hadoop-2.7.2+zookeeper-3.4.6完全分散式環境搭建(HDFSYARN HA) 

Hadoop2.7實戰v1.0Hive-2.0.0+MySQL遠端模式安裝

Hadoop2.7實戰v1.0Hive-2.0.0Hiveserver2服務和beeline遠端除錯 

 

hadoop-01  要有 Hdfs namenode(active),Yarn resourcemanager(active),Hive metastore hiveserver2程式.


.啟動hive metastorehiveserver2服務命令

[root@hadoop-01 bin]# hive --service metastore &

[root@hadoop-01 bin]# hive --service hiveserver2 &


.搭建

1.下載解壓hadoophive

window7D:\Hadoop\hadoop-2.7.2
 window7D:\Hadooop\apache-hive-2.0.0-bin
 

2.啟動eclipse,新建工程HiveCaseProject,匯入需要的jar

 a.匯入hivelib資料夾下所有的包

 b.匯入hadoopD:\Hadoop\hadoop-2.7.2\share\hadoop\common\hadoop-common-2.7.2.jar

 

3.新建hiveserver2Test.class檔案

   

4.hiveserver2Test.class示例程式碼

點選(此處)摺疊或開啟

  1. package com.hadoop.hive;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public class hiveserver2Test {
  9.        public boolean run() {
  10.               Connection con=null;
  11.               Statement stmt =null;
  12.               ResultSet rs=null;
  13.               String tableName="studentinfo";//表名
  14.              
  15.               try {
  16.                    String driver="org.apache.hive.jdbc.HiveDriver";//Hive的驅動字串
  17.                    Class.forName(driver); //載入Hive驅動
  18.                       con = DriverManager.getConnection("jdbc:hive2://192.168.33.01:10000", "root", "");//獲取hive2的jdbc連線,注意預設的資料庫是default
  19.                       stmt = con.createStatement();
  20.                       rs=stmt.executeQuery("select * from "+tableName+" ");//查詢所有,直接執行
  21.                            while(rs.next()){
  22.                            System.out.println("id:"+rs.getString(1)+" "+"name:"+rs.getString(2)+" "+"age:"+rs.getString(3)+" "+"tel:"+rs.getString(4)+" ");
  23.                            }
  24.                   
  25.                    System.out.println("查詢成功!");
  26.                    stmt.close();
  27.                       con.close();
  28.                          
  29.                      return true;
  30.               } catch (Exception e) {
  31.                      e.printStackTrace();
  32.                      System.out.println("error");
  33.                      return false;
  34.              
  35.               }
  36.        }
  37.  
  38.        public static void main(String[] args) throws SQLException {
  39.               hiveserver2Test hiveJdbcClient = new hiveserver2Test();
  40.               hiveJdbcClient.run();
  41.        }
  42.  
  43. }

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.由於hivelib目錄有log4j2相關包,在第2.2步已經匯入,無需再匯入


b.建立resources資料夾,然後在resources資料夾下建立log4j2.xml檔案

log4j2.xml檔案

點選(此處)摺疊或開啟

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="warn">
  3.             <Appenders>
  4.                         <Console name="Console" target="SYSTEM_OUT">
  5.                                     <PatternLayout pattern="%m%n" />
  6.                         </Console>
  7.             </Appenders>
  8.             <Loggers>
  9.                         <Root level="INFO">
  10.                                     <AppenderRef ref="Console" />
  11.                         </Root>
  12.             </Loggers>
  13. </Configuration>

c. resources資料夾新增到class

7.第二次除錯--Success


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30089851/viewspace-2117921/,如需轉載,請註明出處,否則將追究法律責任。

相關文章