溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

HIVE JDBC連接詳解

發(fā)布時間:2020-04-14 01:45:18 來源:網(wǎng)絡 閱讀:5321 作者:jethai 欄目:大數(shù)據(jù)


package org.conan.myhadoop.mr;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class HiveJDBCConnection {

    private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
    // Hive 0.11.0版本以后org.apache.hive.jdbc.HiveDriver
    private static String url = "jdbc:hive://localhost:10000/default";
    // Hive 0.11.0版本以后jdbc:hive2://localhost:10000/default
    private static String userName = "";
    private static String passWord = "";

    public static void main(String[] args) {
        try {
            Class.forName(driverName);
            Connection con = DriverManager.getConnection("url", "userName",
                    "passWord");
            Statement stmt = con.createStatement();
            // 如果存在了就刪除
            String tableName = "jdbc_table";
            String sql = "drop table if exists " + tableName;
            stmt.execute(sql);
            // 創(chuàng)建表
            sql = "create table"
                    + tableName
                    + " (key string,value string) row format delimited fields terminated by ','  stored as textfile ";
            stmt.execute(sql);
            //加載數(shù)據(jù)
            String Path="/home/hive_1.txt";
            sql ="load data local inpath '"+Path+"' into table "+tableName;
            stmt.execute(sql);
            // 查詢數(shù)據(jù)
            sql ="select * from "+tableName;
            ResultSet res = stmt.executeQuery(sql);
            while(res.next()){
                System.out.println(res.getString(1)+"\t"+res.getString(1));
                
            }
            
        } catch (ClassNotFoundException e) {
            System.out.println("沒有找到驅動類");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("連接Hive的信息有問題");
            e.printStackTrace();
        }

    }
}

上面是用Java連接HiveServer,而HiveServer本身存在很多問題(比如:安全性、并發(fā)性等);針對這些問題,Hive0.11.0版本提供了一個全新的服務:HiveServer2,這個很好的解決HiveServer存在的安全性、并發(fā)性等問題。

上面的userName和passWord是hive元數(shù)據(jù)mysql的用戶名和密碼


Use Cases: Hive CLI versus Beeline

The following section focuses on the common uses of Hive CLI/HiveServer1 and how you can migrate to Beeline/HiveServer2 in each case.

http://blog.cloudera.com/blog/2014/02/migrating-from-hive-cli-to-beeline-a-primer/?utm_source=tuicool&utm_medium=referral


參考文章:

http://www.iteblog.com/archives/846


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI