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)); } } }
时间: 2024-10-13 00:56:37