HIVE JDBC串連詳解

來源:互聯網
上載者:User

標籤:hive jdbc串連詳解


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.hadoop.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);            // 建立表            sql = "create table"                    + tableName                    + " (key string,value string) row format delimited fields terminated by ‘,‘  stored as textfile ";            stmt.execute(sql);            //載入資料            String Path="/home/hive_1.txt";            sql ="load data local inpath ‘"+Path+"‘ into table "+tableName;            stmt.execute(sql);            // 查詢資料            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本身存在很多問題(比如:安全性、並發性等);針對這些問題,Hive0.11.0版本提供了一個全新的服務:HiveServer2,這個很好的解決HiveServer存在的安全性、並發性等問題。

上面的userName和passWord是hive中繼資料mysql的使用者名稱和密碼


參考文章:

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


本文出自 “一無所有 QQ:934033381” 部落格,請務必保留此出處http://tianxingzhe.blog.51cto.com/3390077/1698639

HIVE JDBC串連詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.