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 junit4.12 compile org.apache.hadoop hadoop-common3.1.0 log4j log4jorg.apache.hadoop hadoop-client3.1.0 org.apache.hive hive-metastore2.1.1 org.apache.hive hive-exec2.1.1 org.apache.hive hive-jdbc2.1.1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)