package com.hive.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 开启hive server服务 * * nohup bin/hive --service hiveserver >> hiveserver.log 2>&1 & * * */ public class HiveJDBCConnection { private static String dirvername = "org.apache.hadoop.hive.jdbc.HiveDriver"; private static String url = "jdbc:hive://192.168.249.134:10000/default"; private static String username = ""; private static String password = ""; public static void main(String[] args) { try { Class.forName(dirvername); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { Connection conn = DriverManager.getConnection(url, username, password); Statement stmt = conn.createStatement(); String tablename = "jdbc_table"; String sql = "drop table " + tablename; stmt.execute(sql); // 创建表 sql = "create table " + tablename + " (key String ,value String) ROW FORMAT DELIMITED FIELDS TERMINATED BY ','"; stmt.execute(sql); // load数据到jdbc_table表中 String filepath = "/lab/testdata/hive_1.txt"; sql = "load data local inpath '" + filepath + "' into table jdbc_table "; stmt.execute(sql); // 查询表数据 sql = "select * from jdbc_table"; ResultSet res = stmt.executeQuery(sql); while (res.next()) { String key = res.getString(1); String value = res.getString(2); System.out.println(key + " " + value); } //System.out.println(stmt); } catch (SQLException e) { e.printStackTrace(); } } }