ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

HDFS文件常用操作之java api

2021-11-18 12:02:36  阅读:181  来源: 互联网

标签:HDFS fs java FileSystem api new Path configuration


环境准备

1. 本地客户机

  1. idea新建maven工程
  2. 配置maven依赖
<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
  1. 配置的虚拟机hostname, mac环境在/etc/host中配置

2. 虚拟机

  1. 本人集群部署NameNode在hadoop102上

文件操作基本流程

Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102"), configuration, "pitaya");
fs.close()
  1. 新建configuration对象
  2. 新建DistributedFileSystem对象(多态,其父类为FileSystem,可查看源码)
  3. 关闭文件流

HDFS文件上传

@Test
public void testCopyFromLocalFIle() throws IOException, URISyntaxException, InterruptedException {
  Configuration configuration = new Configuration();
  //统一资源标识符(Uniform Resource Identifier,URI)
  FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "pitaya");

  fs.copyFromLocalFile(new Path("/Users/eric/blogs/code/uploadFiles/test.txt"),
                       new Path("/"));
  fs.close();
}

说明

  1. pitaya 选择与文件拥有者相同的身份,否则报错(官方文档:Permissions and HDFS)

Permission denied: user=ptaya, access=WRITE, inode="/":pitaya:supergroup:drwxr-xr-x

HDFS文件下载

@Test
public void testDownload() throws IOException, URISyntaxException, InterruptedException {
    Configuration configuration = new Configuration();
    FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102"), configuration, "pitaya");
    fs.copyToLocalFile(true,new Path("/test.txt"),new Path("/Users/eric/blogs/code/downloadFiles"));
    fs.close();
}

HDFS文件更名或移动

fs.rename(new Path("/test.txt"),new Path("/success.txt"));

HDFS文件或目录删除

fs.delete(new Path("/success.txt"),true);

说明

  1. 使用时发现FileSystem.get()返回的其实是DistributedFileSystem

    public class DistributedFileSystem extends FileSystem implements KeyProviderTokenIssuer {
    }
    
    //FileSystem里定义
    public abstract boolean delete(Path var1, boolean var2) throws IOException;
    

HDFS文件详情查看

RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fs.listFiles(new Path("/"), true);
while(locatedFileStatusRemoteIterator.hasNext()){
    LocatedFileStatus fileStatus = locatedFileStatusRemoteIterator.next();
    System.out.println("======="+fileStatus.getPath()+"======");
    System.out.println(fileStatus.getOwner());
    System.out.println(fileStatus.getModificationTime());
    System.out.println(fileStatus.getBlockSize());
    System.out.println(Arrays.toString(fileStatus.getBlockLocations()));

}

HDFS文件夹和文件创建

fs.mkdirs(new Path("newcreate"));
FSDataOutputStream fsDataOutputStream = fs.create(new Path("/newcreate"));

说明

  1. 创建文件夹时发现只能在当前用户目录下进行,而创建文件则没有此要求

标签:HDFS,fs,java,FileSystem,api,new,Path,configuration
来源: https://www.cnblogs.com/pitaya01/p/15571630.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有