溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

三、hive--jdbc的使用

發(fā)布時(shí)間:2020-07-20 21:59:17 來源:網(wǎng)絡(luò) 閱讀:442 作者:隔壁小白 欄目:大數(shù)據(jù)

一、jdbc連接mysql代碼示例

public class TestConnector {
    final static String USER = "root";
    final static String PASSWORD = "wjt86912572";
    final static String URL = "jdbc:mysql://bigdata121:3306/metastore?useSSL=false&serverTimezone=UTC&useUnicode=true";
    //這是 mysql.connector 在8.x版本中新驅(qū)動(dòng),com.mysql.jdbc.Driver在此版本中已棄用
    final static String DRIVER = "com.mysql.cj.jdbc.Driver";

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            //加載驅(qū)動(dòng)類
            Class.forName(DRIVER);
            //獲取連接對(duì)象
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
            //獲取連接的代理對(duì)象,用于傳遞sql請(qǐng)求
            statement = connection.createStatement();
            boolean result = statement.execute("show databases;");
            resultSet = statement.getResultSet();

            //resultSet.next()看返回的結(jié)果是否還有數(shù)據(jù),如果有為true
            while (resultSet.next()) {
                //resultSet提供了很多獲取的方法,getInt,getString等,具體用哪個(gè),看字段的數(shù)據(jù)類型
                System.out.println(resultSet.getString("Database"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {resultSet.close();}
                if (statement != null) {statement.close();}
                if (connection != null) {connection.close();}
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }
}

二、jdbc連接hive代碼示例

首先添加maven依賴:

<!-- 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>

代碼:

package com.zy.hivejdbc;

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

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HiveJDBC {

    //這是hive的driver名字
    private static String driverName="org.apache.hive.jdbc.HiveDriver";
    //數(shù)據(jù)庫連接字符串
    private static String url = "jdbc:hive2://192.168.134.153:10000/mydb";
    private static String user = "root";
    private static String password="root";

    private static Connection conn = null;
    private static Statement stmt = null;
    private static ResultSet rs = null;

    @Before
    public void init() throws Exception{
        Class.forName(driverName);
        conn = DriverManager.getConnection(url, user, password);
        stmt = conn.createStatement();
    }
    @Test
    public void createDatabase() throws Exception{
        String sql = "create database hive_jdbc_test";
        System.out.println("Running: " + sql);
        stmt.executeQuery(sql);
    }
    @Test
    public void dropDatabase() throws Exception {
        String sql = "drop database if exists hive_jdbc_test";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    @Test
    public void showDatabases() throws Exception {
        String sql = "show databases";
        System.out.println("Running: " + sql + "\n");
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1) );
        }
    }

    @Test
    public void createTable() throws Exception {
        String sql = "create table t2(id int ,name String) row format delimited fields terminated by ',';";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    @Test
    public void loadData() throws Exception {
         String filePath = "/usr/tmp/student";
         String sql = "load data local inpath '" + filePath + "' overwrite into table t2";
         System.out.println("Running: " + sql);
         stmt.execute(sql);
    }

    @Test
    public void selectData() throws Exception {
        String sql = "select * from t2";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        System.out.println("編號(hào)" + "\t" + "姓名" );
        while (rs.next()) {
            System.out.println(rs.getInt(1) + "\t" + rs.getString(2));
        }
    }
    @Test
    public static void drop(Statement stmt) throws Exception {
        String dropSQL = "drop table t2";
        boolean bool = stmt.execute(dropSQL);
        System.out.println("刪除表是否成功:" + bool);
        }
    @After
    public void destory() throws Exception {
        if (rs != null) {
            rs.close();
        }
        if (stmt != null) {
            stmt.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

}

基本使用和jdbc連接mysql類似。

向AI問一下細(xì)節(jié)

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

AI