栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

向HDFS上的hive中load本地文件

向HDFS上的hive中load本地文件

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
        
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/699404.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号