当前位置:Gxlcms > 数据库问题 > HiveJdbc

HiveJdbc

时间:2021-07-01 10:21:17 帮助过:5人阅读

Jdbc访问需要启动server服务:
hive --service hiveserver

package cn.sniper.hive.jdbc;

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

public class HiveJdbc {
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";

      /**
     * @param args
     * @throws SQLException
       */ 
    public static void main(String[] args) throws SQLException {
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
        
        Connection con = DriverManager.getConnection("jdbc:hive2://hadoop20:10000/d1", "", "");
        Statement stmt = con.createStatement();
        
        String tableName = "t_user1";
        stmt.execute("drop table if exists " + tableName);
        stmt.execute("create table " + tableName + " (userid int, name string)");
        
        // show tables
        String sql = "show tables ‘" + tableName + "‘";
        System.out.println("Running: " + sql);
        ResultSet res = stmt.executeQuery(sql);
        if (res.next()) {
          System.out.println(res.getString(1));
        }

        sql = "describe " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(res.getString(1) + "\t" + res.getString(2));
        }
    }
}


HiveJdbc

标签:

人气教程排行