Windows下IDEA整合Hadoop3.x

1. hadoop开发包下载

1.1 windows系统上下载对应的hadoop开发包,例如:hadoop3.3.5版本

1.1 解压开发包并重命名

2. 打补丁

2.1 由于是windows开发系统,需要安装一些补丁才能正常运行,在hadoop的bin目录下安装

将该三个补丁,复制到hadoop的bin目录下即可

3.配置hadoop的环境变量

3.1 前提JDK已安装及环境变量已配置

3.1 添加两个变量:HADOOP_HOME、PATH

4 . 打开IDEA创建Maven项目

4.1 项目创建

4.2. 在pom.xml文件中引入对应依赖

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-common</artifactId>
    <version>3.3.5</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>3.3.5</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-hdfs</artifactId>
    <version>3.3.5</version>
</dependency>

4.3. 从hadoop服务器上下载配置文件,导入到项目的src/resources下

4.4. 使用硬编码方式连接hdfs伪分布式(注意:服务器的防火墙一定要关闭

适用于单机模式与伪分布集群:因为很清楚namenode是哪台服务器

方式一:

        try{
            //-适用于单机模式与伪分布集群:因为都是固定的服务器地址
            //-此处的地址要与core-site.xml中配置的fs.defaultFS内容相同
            URI uri = new URI("hdfs://hadoop:9000");
            Configuration conf = new Configuration();
            String user = "root";
            FileSystem fs = FileSystem.get(conf);
            System.out.println("连接成功");
            //-判断hdfs中某个目录是否存在
            System.out.println(fs.exists(new Path("/wordcount")));
            fs.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }

方式二:

        try{
            //-适用于单机模式与伪分布集群:因为都是固定的服务器地址
            //-配置项
            Configuration conf = new Configuration();
            //-此处的地址要与core-site.xml中配置的fs.defaultFS内容相同
            conf.set("fs.defaultFS","hdfs://hadoop:9000");
            //-设置当前操作系统环境变量
            System.setProperty("HADOOP_USER_NAME","ROOT");
            FileSystem fs = FileSystem.get(conf);
            System.out.println("连接成功");
            //-判断hdfs中某个目录是否存在
            System.out.println(fs.exists(new Path("/user")));
            fs.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }

4.5. 连接hadoop高可用集群

4.5.1 将hadoop集群服务器中的如下配置文件,下载到本地并复制到项目的src/resources目录下

4.5.2 连接高可用集群代码

        try{
            System.setProperty("HADOOP_USER_NAME","ROOT");
            Configuration conf = new Configuration();
            DistributedFileSystem dfs = new DistributedFileSystem();
            //-获取集群服务器的名字,参数是在hdfs-site.xml中进行配置的
            String nameService = conf.get("dfs.nameservices");
            String hdfsRPCUrl = "http://"+nameService+":"+8020;
            dfs.initialize(URI.create(hdfsRPCUrl),conf);
            //-:获取hdfs中根目录下所有文件路径
            FileStatus[] list = dfs.listStatus(new Path("/"));
            for(FileStatus file:list){
                System.out.println(file.getPath());
            }
            dfs.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }

4.5.3 自己在服务器上创建目录

        try{
            System.setProperty("HADOOP_USER_NAME","ROOT");
            Configuration conf = new Configuration();
            DistributedFileSystem dfs = new DistributedFileSystem();
            //-获取集群服务器的名字,参数是在hdfs-site.xml中进行配置的
            String nameService = conf.get("dfs.nameservices");
            String hdfsRPCUrl = "http://"+nameService+":"+8020;
            dfs.initialize(URI.create(hdfsRPCUrl),conf);
            //-:创建目录
            Path path = new Path("/ceshi");
            if(!dfs.exists(path)){
                System.out.println(dfs.mkdirs(path)?"创建成功":"创建失败");
            }else{
                System.out.println(path.getName()+"已经存在无需创建");
            }
            dfs.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }

4.5.4 修改文件名

        try{
            System.setProperty("HADOOP_USER_NAME","ROOT");
            Configuration conf = new Configuration();
            DistributedFileSystem dfs = new DistributedFileSystem();
            //-获取集群服务器的名字,参数是在hdfs-site.xml中进行配置的
            String nameService = conf.get("dfs.nameservices");
            String hdfsRPCUrl = "http://"+nameService+":"+8020;
            dfs.initialize(URI.create(hdfsRPCUrl),conf);
            //-:创建目录
            Path src = new Path("/ceshi");
            Path dst = new Path("/测试");
            if(dfs.exists(src)){
                System.out.println(dfs.rename(src,dst)?"修改成功":"修改失败");
            }else{
                System.out.println(src.getName()+"文件不存在");
            }
            dfs.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }

4.5.5 删除目录或文件

        try{
            System.setProperty("HADOOP_USER_NAME","ROOT");
            Configuration conf = new Configuration();
            DistributedFileSystem dfs = new DistributedFileSystem();
            //-获取集群服务器的名字,参数是在hdfs-site.xml中进行配置的
            String nameService = conf.get("dfs.nameservices");
            String hdfsRPCUrl = "http://"+nameService+":"+8020;
            dfs.initialize(URI.create(hdfsRPCUrl),conf);
            //-:创建目录
            Path path = new Path("/测试");
            if(dfs.exists(path)){
                System.out.println(dfs.delete(path,true)?"删除成功":"删除失败");
            }else{
                System.out.println(path.getName()+"文件不存在");
            }
            dfs.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }

4.5.6 文件上传

        try{
            System.setProperty("HADOOP_USER_NAME","ROOT");
            Configuration conf = new Configuration();
            DistributedFileSystem dfs = new DistributedFileSystem();
            //-获取集群服务器的名字,参数是在hdfs-site.xml中进行配置的
            String nameService = conf.get("dfs.nameservices");
            String hdfsRPCUrl = "http://"+nameService+":"+8020;
            dfs.initialize(URI.create(hdfsRPCUrl),conf);
            //-:创建目录
            Path src = new Path("hadoop教程.txt");
            Path dst = new Path("/study/hadoop教程.txt");
            Path path = new Path("/study");
            if(dfs.exists(path)){
                dfs.copyFromLocalFile(src,dst);
                System.out.println(dfs.exists(dst)?"上传成功":"上传失败");
            }else{
                System.out.println(path.getName()+"目标目录不存在无法上传");
            }
            dfs.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }

  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值