HDFS文件常用操作之java api
2021/11/18 12:09:46
本文主要是介绍HDFS文件常用操作之java api,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
环境准备
1. 本地客户机
- idea新建maven工程
- 配置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>
- 配置的虚拟机hostname, mac环境在/etc/host中配置
2. 虚拟机
- 本人集群部署NameNode在hadoop102上
文件操作基本流程
Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102"), configuration, "pitaya"); fs.close()
- 新建configuration对象
- 新建DistributedFileSystem对象(多态,其父类为FileSystem,可查看源码)
- 关闭文件流
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(); }
说明
- 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);
说明
-
使用时发现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"));
说明
- 创建文件夹时发现只能在当前用户目录下进行,而创建文件则没有此要求
这篇关于HDFS文件常用操作之java api的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)