Java实现HDFS文件读取和写入操作(Hadoop2.6.0版本)

准备工作:

虚拟机打开,使用start-all.sh命令启动Hadoop。使用jps命令可以查看是否全部启动。
在这里插入图片描述
打开IDEA,创建一个maven项目。在pom.xml里导入依赖,如下:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <hadoop-version>2.6.0</hadoop-version>
  </properties>
<dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>${hadoop-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>${hadoop-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-mapreduce-client-core</artifactId>
      <version>${hadoop-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-mapreduce-client-common</artifactId>
      <version>${hadoop-version}</version>
    </dependency>

不添加properties那一项的话可以直接将下面的version改成2.6.0。
等待maven去下载依赖构建项目。

操作步骤:

准备就绪后,开始编写代码,创建一个Java类(名字随意)。
在类中写两个方法,一个hdfsReadFile(读取),一个hdfsWriterFile(写入)。
编写程序,如下:

 public static void hdfsReadFile(String hdfsFile,String hdfsUrl,String fileName) throws Exception
    {
    	// 创建配置对象实例
        Configuration cfg = new Configuration();
        //设置操作的文件系统是HDFS,并且指定HDFS操作地址
        cfg.set("fs.defaultFS",hdfsUrl);
        //创建FileSystem 对象实例
        FileSystem fileSystem = FileSystem.get(cfg);
        if(!fileSystem.exists(new Path(hdfsFile)))
        {
            throw new Exception("要下载的文件内容不存在。");
        }
        try {
            //读取操作,从hdfs指定的文件读出数据对象
            FSDataInputStream fsdiStream = fileSystem.open(new Path(hdfsFile));

            try {
                FileOutputStream fileOutputStream = new FileOutputStream(fileName);

                try {
                    byte[] buffer = new byte[2048];
                    int count = fsdiStream.read(buffer, 0, 2048);
                    while (count > 0) {
                        fileOutputStream.write(buffer,0,count);
                        count = fsdiStream.read(buffer,0,2048);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    fileOutputStream.close();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            finally {
                fsdiStream.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
public static void hdfsWriterFile(String fileName,String hdfsUrl,String hdfsFile) throws Exception
    {
        Configuration cfg = new Configuration();
        cfg.set("fs.defaultFS",hdfsUrl);
        FileSystem fileSystem = FileSystem.get(cfg);
        if(fileSystem.exists(new Path(hdfsFile)))
        {
            throw new Exception("文件已存在。");
        }

        try {
            //输出流对象,将数据输出到HDFS文件系统
            FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path(hdfsFile));
            try {
                //输出流对象,将本地要上传的文件读取到内存中
                FileInputStream fileInputStream = new FileInputStream(fileName);

                try {
                    byte[] buffer = new byte[2048];
                    int count = fileInputStream.read(buffer, 0, 2048);
                    while (count > 0) {
                        fsDataOutputStream.write(buffer,0,count);
                        count = fileInputStream.read(buffer,0,2048);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    fileInputStream.close();
                }


            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally {
                fsDataOutputStream.close();
            }
        }catch (IOException e)
        {
            e.printStackTrace();
        }
    }

读取和写入方法编写完成后,编写主函数用来测试调用。

public static void main(String[] args) throws Exception {
        hdfsWriterFile("D:\\IDEAprojects\\hadoopstu\\resource\\helloworld.txt","hdfs://192.168.91.137:9000/","hdfs://192.168.91.137:9000/input/hw.txt");
        //hdfsReadFile("hdfs://192.168.91.137:9000/input/hw.txt","hdfs://192.168.91.137:9000/","D:\\1111\\hw.txt");
    }

首先测试写入能不能成功,测试内容是将本地项目包下的一个txt文件写入到HDFS中,点击运行。
在这里插入图片描述
进入到HDFS可视化界面中可以看到写入成功!
接着测试读取是否成功,运行程序。
在这里插入图片描述
进入到D盘1111文件夹下,可以看到已经生成了hw.txt,读取成功!

  • 0
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万家林

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值