案例: jdbc 操作hive數據庫

1.依賴jar包:注意版本衝突的問題

hive-exec-0.13.1.jar

hive-jdbc-0.13.1.jar

hive-metastore-0.13.1.jar

hive-service-0.13.1.jar

hadoop-client-1.2.1.jar

hadoop-common-2.3.0.jar

 

2. 編寫 jdbc 代碼:

public class HiveJdbcTest {



    static {
        try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public static void main(String[] args) throws SQLException {
        try {
            Statement stmt = null;
            try {
                Connection con = DriverManager.getConnection("jdbc:hive2://hive數據庫主機:hive數據庫端口/hive數據庫名稱", "用戶名", "密碼");
                stmt = con.createStatement();
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
            String createSQL = "create table table01(id string,name string )";
            boolean res = stmt.execute(createSQL);
            System.out.println("  createTableInHive: end exec !!    res:" + !res);

            // String sql =
            // "create view if not exists 庫名.view03 (id ,name) comment '批量' as select id,name from 庫名.table01";
            // System.out.println("Running3: " + sql);
            // boolean temp = stmt.execute(sql);
            // System.out.println("  createViewInHive: end exec !!    temp:" + !temp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

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