public static void loadData2Hive(String dst,String dateString) {
String JDBC_DRIVER = "org.apache.hive.jdbc.HiveDriver";
String CONNECTION_URL = "jdbc:hive2://xx.xx.xx.xx:10000";
String username = "";
String password = "";
Connection con = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
con = (Connection) DriverManager.getConnection(CONNECTION_URL,username,password);
stmt = con.createStatement();
String sql = "load data local inpath '" + dst + "' into table test_databse.test_table PARTITION (dt = '"+dateString+"')";
stmt.execute(sql);
System.out.println("loadData到Hive表成功!");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
// 关闭stmt和con
if (stmt!= null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
直接替换你的host的ip与你的表名和库名就可以了
需要用到的依赖,注意hive的版本hadoop的版本要对应,不然会报错
junit junit 4.12 compile org.apache.hadoop hadoop-common 3.1.0 log4j log4j org.apache.hadoop hadoop-client 3.1.0 org.apache.hive hive-metastore 2.1.1 org.apache.hive hive-exec 2.1.1 org.apache.hive hive-jdbc 2.1.1



