前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hadoop API——HDFS

Hadoop API——HDFS

作者头像
羊羽shine
发布2019-05-29 22:32:05
5520
发布2019-05-29 22:32:05
举报
文章被收录于专栏:Golang开发Golang开发Golang开发

mvn 配置

 <dependencies>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

创建文件夹

Configuration config = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.114:8020"),config,"hadoop");
boolean result = fileSystem.mkdirs(new Path("/hdfsapi/test"));

读取文件

 FSDataInputStream in = fileSystem.open(new Path("/hadoop.txt"));
 IOUtils.copyBytes(in,System.out,1024);

创建文件

FSDataOutputStream out = fileSystem.create(new Path("/hello.txt"));
out.writeUTF("hello world");
out.flush();
out.close();

重命名文件

fileSystem.rename(new Path("/hello.txt"),new Path("/new.txt"));

拷贝本地文件到hdfs

 fileSystem.copyFromLocalFile(new Path("./pom.xml"),new Path("/pom.xml"));

下载hdfs文件到本地

fileSystem.copyToLocalFile(new Path("/hadoop.txt"),new Path("hadoop.txt"));

获取文件列表

FileStatus[] fileStatus = fileSystem.listStatus(new Path("/"));
for(FileStatus s: fileStatus){
      System.out.println(s.getPath().toString());
}

递归获取文件

RemoteIterator<LocatedFileStatus> fileStatus = fileSystem.listFiles(new Path("/"),true);
        while (fileStatus.hasNext()){
            System.out.println(fileStatus.next().getPath().toString());
        }

删除文件

fileSystem.delete(new Path("/new.txt"),true);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.04.06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
大数据
全栈大数据产品,面向海量数据场景,帮助您 “智理无数,心中有数”!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档