ideaIJ java代码在hdfs中的操作

运行不通过请检查一下几个问题

  • 虚拟机hadoop和hdfs是否打开
  • jps命令,节点集群是否全面
  • datanode节点是否缺失
  • 文件已经存在报错等。

package edu.sugon;

import org.apache.hadoop.conf.Configuration;
        import org.apache.hadoop.fs.FileSystem;
        import org.apache.hadoop.fs.FileStatus;
        import org.apache.hadoop.fs.Path;
        import org.apache.hadoop.fs.FSDataInputStream;
        import org.apache.hadoop.fs.FSDataOutputStream;
        import java.io.BufferedReader;
        import java.io.InputStreamReader;
        import java.io.IOException;
        import java.io.OutputStreamWriter;
        import java.io.BufferedWriter;
        import java.net.URI;

public class HDFSOpt1 {

    private static final String hdfsUrl = "hdfs://192.168.10.100";
    private static final String filePath = "/test/hello.txt";
    private static final String filePath2 = "/test/pom.xml";
    public static void main(String[] args) throws IOException {
        Configuration conf = new Configuration();
        conf.addResource("core-site.xml");
        conf.addResource("hdfs-site.xml");
        System.setProperty("user", "hadoop");
        FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);

        Path file2 = new Path(filePath2);
        // 创建文件
        Path file = new Path(filePath);

        if (!fs.exists(file)) {
            fs.createNewFile(file);
            System.out.println("Create file: " + filePath);
        }

        // 删除文件
        if (fs.delete(file, true)) {
            System.out.println("Delete file: " + filePath);
        }

        // 列出根目录下所有文件
        FileStatus[] files = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : files) {
            System.out.println("File: " + fileStatus.getPath());
        }

        // 上传文件
        Path localPath = new Path("D:\\Hadoop\\ideaIJ\\studyspace\\hadoop\\hadoop\\hdfs\\pom.xml");
        Path hdfsPath = new Path("/test/pom.xml");
        fs.copyFromLocalFile(localPath, hdfsPath);
        System.out.println("Uploaded file from " + localPath + " to " + hdfsPath);
 

        // 下载文件

        Path localFile = new Path("D:\\pom2.xml");

        Path hdfsFile = new Path("/test/pom.xml");

        fs.copyToLocalFile(hdfsFile, localFile);

        System.out.println("Downloaded file from " + hdfsFile + " to " + localFile);

 

        // 追加文件

        FSDataOutputStream appendStream = fs.append(file2);

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(appendStream));

        writer.write("Appending new line...");

        writer.newLine();

        writer.close();

        appendStream.close();

        System.out.println("Append content to " + filePath2);

 

        // 读文件

        FSDataInputStream inputStream = fs.open(file2);

        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        String line = null;

        while ((line = reader.readLine()) != null) {

            System.out.println(line);

        }

        reader.close();

        inputStream.close();

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值