HDFS的API使用

这篇博客介绍了如何通过Hadoop的HDFS API进行文件系统的初始化、文件上传、下载、创建、删除、获取副本数以及列举所有文件等操作。示例代码详细展示了这些功能的实现,包括设置配置、系统属性以及相关方法的调用。
摘要由CSDN通过智能技术生成

HDFS的API总体来说有两个主要的类。

FileSystem 和Configuration。

导入依赖

<dependencies>

        <dependency>

            <groupId>org.apache.hadoop</groupId>

            <artifactId>hadoop-client</artifactId>

            <version>2.7.3</version>

        </dependency>

    </dependencies>

(1)初始化HDFS

//因为远程提交的情况下如果没有hadoop 的系统环境变量,就会读取当前主机的用户名,所以Hadoop集群的节点中没有该用户名的权限,所以出现的异常。

System.setProperty("HADOOP_USER_NAME","root");

Configuration conf = new Configuration();

        conf.set("fs.defaultFS", "hdfs://master:9000");

        conf.set("dfs.replication", "2");

        FileSystem fs = FileSystem.get(conf);

(2) 上传文件

  fs.copyFromLocalFile(new Path("D:\\abc\\wordcount\\input\\test.txt"),new Path("/wordcount/a.txt"));

(3)下载文件

        fs.copyToLocalFile(new Path("/wordcount/a.txt"),new Path("D:\\abc\\wordcount\\input\\test2.txt"));

(4)创建

        fs.create(new Path("/wordcount/b.txt"));

(5)删除

        fs.delete(new Path("/wordcount/b.txt"));

(6)获取一个文件的副本数

        short i = fs.getDefaultReplication(new Path("/wordcount/b.txt"));

        System.out.println(i);

(7)获取所有的文件

        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"),true);

        while (listFiles.hasNext()){

            LocatedFileStatus fileStatus = listFiles.next();

            Path path = fileStatus.getPath();

            System.out.println(path);

            //获取Block块的信息

            BlockLocation[] blockLocations = fileStatus.getBlockLocations();

            for (BlockLocation blockLocation:blockLocations) {

                String[] hosts = blockLocation.getHosts();

                String[] names = blockLocation.getNames();

                long offset = blockLocation.getOffset();

                System.out.println(offset);

                System.out.println(ArrayUtils.toString(hosts));

                System.out.println(ArrayUtils.toString(names));

            }

        }

        fs.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值