1. 程式人生 > >Java API連線Hive

Java API連線Hive

首先導maven包:

        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>1.1.0</version>
        </dependency>

導包:

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

程式碼:

複製貼上修改連線地址即可使用

public class HiveDBUtil {
    public static Connection conn=null;

    public static Connection getConn()throws Exception{
        if(conn==null){
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            conn=  DriverManager.getConnection(
                    "jdbc:hive2://192.168.163.120:10000/hive","hive","hive");
        }
        return conn;
    }

    public static void close(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try{
            System.out.println(getConn());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

希望能幫到有需要的朋友