首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Hdfs FileSystem Client

Hdfs FileSystem Client

作者头像
Dlimeng
发布2023-06-30 14:35:25
发布2023-06-30 14:35:25
2790
举报
文章被收录于专栏:开源心路开源心路

一.示例

Java抽象类org.apache.hadoop.fs.FileSystem定义了hadoop的一个文件系统接口。Hadoop中关于文件操作类基本上全部是在"org.apache.hadoop.fs"包中,这些API能够支持的操作包含:打开文件,读写文件,删除文件等。

Hadoop类库中最终面向用户提供的接口类是FileSystem,该类是个抽象类,只能通过来类的get方法得到具体类。

代码语言:javascript
复制
public class HDFSFileSystem {
    public static void main(String[] args) {
        String res = args[0];
        long start,end;
        start = System.currentTimeMillis();
        FileSystem fs = init();
        //String res="/tmp/linkis/hdfs/20201215/6e8fa663-ffa8-4d50-9510-f8cf15518dc0.json";
        try {
            FSDataInputStream in = fs.open(new Path(res));
            IOUtils.copyBytes(in, System.out, 4096, false);

        } catch (IOException e) {
            e.printStackTrace();
        }

        end = System.currentTimeMillis();
        System.out.println("\n start time:" + start+ "; end time:" + end+ "; Run Time:" + (end - start) + "(ms)");
    }

    public static FileSystem init(){
        Configuration conf = new Configuration();
        String hadoopConfDir="/etc/hadoop/conf";
        conf.addResource(new Path(Paths.get(hadoopConfDir,"core-site.xml").toAbsolutePath().toFile().getAbsolutePath()));
        conf.addResource(new Path(Paths.get(hadoopConfDir, "hdfs-site.xml").toAbsolutePath().toFile().getAbsolutePath()));
        conf.addResource(new Path(Paths.get(hadoopConfDir, "yarn-site.xml").toAbsolutePath().toFile().getAbsolutePath()));
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        try {
            FileSystem fs = UserGroupInformation.createRemoteUser("hdfs").doAs(new PrivilegedExceptionAction<FileSystem>() {
                @Override
                public FileSystem run() throws Exception {
                    return FileSystem.get(conf);
                }
            });
            return fs;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

}

结果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-06-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档