HIVE-API

用Maven進行項目管理

package com.ny.hive.hivedemo;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

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

public class TestCRUD {
    
    private Connection coon;
    @Before
    public void iniCoon() throws Exception{
    coon = DriverManager.getConnection(//
    "jdbc:hive2://node02:10000/default",//
    "root",
    "123456"//
    );
    }        
    /*
    * create table users
    */

    @Test
    public void create() throws Exception{
    PreparedStatement ppst = coon.prepareStatement("create table default.users(id int,name string,age int)");
    ppst.execute();
    ppst.close();
    coon.close();
    System.out.println("創建成功");
    }
    
    /*
    * 插入多個
    */

    @Test
    public void batchInsert() throws Exception{
    PreparedStatement ppst = coon.prepareStatement("insert into table default.users(id,name,age) values(?,?,?)");
    ppst.setInt(1, 1);
    ppst.setString(2, "tom");
    ppst.setInt(3, 12);
    ppst.executeUpdate();

    ppst.setInt(1, 1);
    ppst.setString(2, "tom2");
    ppst.setInt(3, 13);
    ppst.executeUpdate();

    ppst.setInt(1, 1);
    ppst.setString(2, "tom3");
    ppst.setInt(3, 14);
    ppst.executeUpdate();    

    ppst.close();
    coon.close();
    }

    //統計記錄

    @Test

    public void count() throws Exception{
    PreparedStatement ppst = coon.prepareStatement("select count(*) from default.users");
    ResultSet rs =ppst.executeQuery();
    rs.next();
    System.out.println(rs.getInt(1));
    ppst.close();
    coon.close();
    }
    
    /*

    *刪除表

    */

    @Test
    public void deletetable() throws Exception{
    PreparedStatement ppst = coon.prepareStatement("drop table default.users");
    ppst.execute();
    ppst.close();
    coon.close();
    System.out.println("刪除成功");
    }

}
    
    
    


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章