HDFS Java 客户端开发(开发环境:Windows)
2017-08-31 10:06
399 查看
HDFS是一个分布式文件系统,既然是文件系统,就可以对其中的文件进行操作,比如说新建文件夹、上传文件,重命名文件,下载文件,删除文件、列举所有文件等操作。
HDFSClient.java
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
相关文章推荐
- 在Windows上搭建Android的JAVA开发环境图文教程(Eclipse版本)
- windows 配置java开发环境
- 在Windows上搭建Android的JAVA开发环境图文教程(Eclipse版本)
- Linux和windows环境下的Java开发的区别与不同
- 在Windows上搭建Android的JAVA开发环境图文教程(Eclipse版本)
- Windows下的java串口开发环境配置
- Linux和windows环境下的Java开发的区别与不同
- Windows下JAVA开发环境的配置(应用Eclipse)
- Windows下JAVA开发环境的配置(应用Eclipse)
- 在windows开发环境中,java代码中使用linux格式路径的方法需要注意的问题
- Linux环境下Java开发 Linux和Windows比较
- 招聘Windows环境下客户端VC贤者,从事QQ Client端程序开发工作【借首页人气】
- VTK经验分享 2. VTK的Java开发环境的搭建(Windows环境)
- Linux和windows环境下的Java开发的区别与不同
- windows 7 操作系统中Java开发环境的设置
- 在Windows上搭建Android的JAVA开发环境图文教程(Eclipse版本)
- java开发环境搭建(windows)
- Windows下JAVA开发环境的配置(应用Eclipse)
- Windows,Linux下Java开发环境的配置
- Windows环境下搭建Java开发环境