HDFS Java 客户端开发(开发环境:Windows)

xiaoxiao2021-02-28  65

HDFS是一个分布式文件系统,既然是文件系统,就可以对其中的文件进行操作,比如说新建文件夹、上传文件,重命名文件,下载文件,删除文件、列举所有文件等操作。

HDFSClient.java

package com.looc.client; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.LocatedFileStatus; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.RemoteIterator; import org.junit.Before; import org.junit.Test; public class HDFSClient { private FileSystem fileSystem = null; @Before public void getFileSystem() throws IOException { // 配置参数的封装对象 // 构造函数对xxx-site.xml文件进行解析,真实项目中需要将xxx-site.xml文件加载到工程中。 Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", "hdfs://looc:9000"); // 获取文件系统的客户端实例对象,根据configuration中的相关参数决定。 fileSystem = FileSystem.get(configuration); } /** * 创建文件夹 * * @throws IllegalArgumentException * @throws IOException */ @Test public void testMkdir() throws IllegalArgumentException, IOException { fileSystem.mkdirs(new Path("/a/b/c")); } /** * 上传文件 * * @throws IllegalArgumentException * @throws IOException */ @Test public void testUpload() throws IllegalArgumentException, IOException { fileSystem.copyFromLocalFile(new Path("E:\\MapReduce\\WordCount.txt"), new Path("/a/b/c")); } /** * 重命名文件 * * @throws IllegalArgumentException * @throws IOException */ @Test public void testRename() throws IllegalArgumentException, IOException { fileSystem.rename(new Path("/a/b/c/WordCount.txt"), new Path("/a/b/c/WordCount.txt.rename")); } /** * 下载文件 * * @throws IllegalArgumentException * @throws IOException */ @Test public void testDownload() throws IllegalArgumentException, IOException { fileSystem.copyToLocalFile(new Path("/a/b/c/WordCount.txt.rename"), new Path("E:\\MapReduce")); } /** * 删除文件 * * @throws IllegalArgumentException * @throws IOException */ @Test public void testRmfile() throws IllegalArgumentException, IOException { boolean result = fileSystem.delete(new Path("/a/b/c/WordCount.txt.rename"), true); System.out.println(result ? "DELETE IS SUCCESSFULLY!" : "DELETE IS FAILD!"); } /** * 列举所有文件 * * @throws FileNotFoundException * @throws IllegalArgumentException * @throws IOException */ @Test public void testListFile() throws FileNotFoundException, IllegalArgumentException, IOException { RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true); while (listFiles.hasNext()) { System.out.println(listFiles.next().getPath().getName()); } } }

Over

转载请注明原文地址: https://www.6miu.com/read-23353.html

最新回复(0)