Hive:用Java代碼通過JDBC連接Hiveserver


參考https://www.iteblog.com/archives/846.html

1、hive依賴hadoop,將hdfs當作文件存儲介質,那是否意味着hive需要知道namenode的地址?

     實際上在hive的hive-env.sh 中配置了 HADOOP_HOME=/home/install/hadoop-2.5.1

2、hive的本地模式和遠程模式有什么區別?

     hive本質上是將sql語法解析為mapreduce的過程,既然如此它就必須提交mapreduce任務到resoucemanager,那么它如何提交?就是通過hadoop提供的命令hadoop jar命令來提交。

     本地模式:簡單的理解,hive客戶端僅供本地使用,直接使用hive命令,不需要指定IP 端口

     遠程模式:簡單的理解,將hive發布成一個服務進程,通過hive --service hiveserver命令,那么其他hive客戶端就可以連接hive的服務進程

                其他客戶端可以是jdbc方式、hive提供的beeline命令等,既然要連接遠端的hive服務進程,那自然需要指定 IP 端口,這里的IP指的是hive服務進程所在的IP,端口自然也是,也自然與hadoop無關。所以不要混淆

HiveServer2提供了一個新的命令行工具Beeline,它是基於SQLLine CLI的JDBC客戶端。

Beeline工作模式有兩種,即本地嵌入模式和遠程模式。嵌入模式情況下,它返回一個嵌入式Hive(類似於Hive CLI)。而遠程模式則是通過Thrift協議與某個單獨的HiveServer2進程進行連接通信。

hive的三種連接方式

1、hive 命令行模式,直接輸入/hive/bin/hive的執行程序,或者輸入 hive --service cli
用於linux平台命令行查詢,查詢語句基本跟mysql查詢語句類似
2、 hive web界面的 (端口號9999) 啟動方式
hive –service hwi &
       用於通過瀏覽器來訪問hive,感覺沒多大用途
3、 hive 遠程服務 (端口號10000) 啟動方式
hive --service hiveserver &
或者
hive --service hiveserver 10000>/dev/null 2>/dev/null &
    beeline方式連接:beeline -u jdbc:hive2//localhost:10000/default -n root -p 123456
    或者
    java client方式連接
備注:
連接Hive JDBC URL:jdbc:hive://192.168.6.116:10000/default (Hive默認端口:10000 默認數據庫名:default)

第一步:開啟hive 遠程服務

bin/hive --service hiveserver -p 10002
   Starting Hive Thrift Server
第二步:添加依賴
 <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.7.1</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.7.1</version>
    </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>1.2.1</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-metastore</artifactId>
        <version>1.2.1</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>1.2.1</version>
    </dependency>
   
 
第三步:
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
 
public class HiveJdbcTest {
     
    private static String driverName =
                   "org.apache.hadoop.hive.jdbc.HiveDriver";
   
    public static void main(String[] args)
                            throws SQLException {
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
 
        Connection con = DriverManager.getConnection(
                           "jdbc:hive2://localhost:10002/default", "wyp", "");
        Statement stmt = con.createStatement();
        String tableName = "wyphao";
        stmt.execute("drop table if exists " + tableName);
        stmt.execute("create table " + tableName +
                                     " (key int, value string)");
        System.out.println("Create table success!");
        // show tables
        String sql = "show tables '" + tableName + "'";
        System.out.println("Running: " + sql);
        ResultSet res = stmt.executeQuery(sql);
        if (res.next()) {
            System.out.println(res.getString(1));
        }
 
        // describe table
        sql = "describe " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(res.getString(1) + "\t" + res.getString(2));
        }
 
 
        sql = "select * from " + tableName;
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(String.valueOf(res.getInt(1)) + "\t"
                                               + res.getString(2));
        }
 
        sql = "select count(1) from " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(res.getString(1));
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM