HDFS 作为开源界比较成熟的分布式文件存储系统,适用于海量文件存储,本文介绍了如何使用 Java 操作 HDFS,采用 Maven 管理包。
pom.xml
<dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>2.6.0</version></dependency>
<dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.6.0</version></dependency>
<dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>2.6.0</version>
</dependency>
HadoopUtil.java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import java.net.URI;/*** Hadoop工具类*/
public class HadoopUtil {private static String hdfsPath="hdfs://192.168.91.143:8020";private static String hdfsName="hdfs";/*** 获取HDFS配置信息* @return*/private static Configuration getConfiguration() {Configuration configuration = new Configuration();configuration.set("fs.defaultFS", hdfsPath);return configuration;}/*** 获取HDFS文件系统对象* @return* @throws Exception*/public static FileSystem getFileSystem() throws Exception {// 客户端去操作hdfs时是有一个用户身份的,默认情况下hdfs客户端api会从jvm中获取一个参数作为自己的用户身份 DHADOOP_USER_NAME=hadoop
// FileSystem hdfs = FileSystem.get(getHdfsConfig()); //默认获取
// 也可以在构造客户端fs对象时,通过参数传递进去FileSystem fileSystem = FileSystem.get(new URI(hdfsPath), getConfiguration(), hdfsName);return fileSystem;}
}
HDFSApp.java
import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;import java.io.FileInputStream;
import java.io.InputStream;public class HDFSApp {public static void main(String[] args) throws Exception {FileSystem fs= HadoopUtil.getFileSystem();//目录不存在,创建String path="/javatest";Path dirPath=new Path(path);if(!fs.exists(dirPath)) {boolean isOk = fs.mkdirs(dirPath);fs.close();if (isOk) {System.out.println("create dir success");} else {System.out.println("create dir fail");}}// 上传时默认当前目录,后面自动拼接文件的目录String fileName="text.txt";Path filePath = new Path(path + "/" + fileName);// 打开一个输出流FSDataOutputStream outputStream=null;FileInputStream in=null;try {outputStream = fs.create(filePath);in = new FileInputStream("e:/temp/text.txt");IOUtils.copy(in, outputStream, 4096);}finally {in.close();outputStream.close();}//直接从本地拷贝文件fs.copyFromLocalFile(new Path("e:/temp/text.txt"),new Path(path + "/textcopy.txt" ));//直接下载文件到本地fs.copyToLocalFile(new Path(path + "/textcopy.txt" ),new Path("e:/temp/textcopy.txt"));//文件改名fs.rename(new Path(path + "/textcopy.txt" ),new Path(path + "/textrename.txt" ));//下载文件InputStream inputStream =null;try {inputStream = fs.open(filePath);IOUtils.copy(inputStream, System.out, 4096);}finally {inputStream.close();}//删除文件boolean isOk = fs.deleteOnExit(filePath);fs.close();if (isOk) {System.out.println("delete file success");} else {System.out.println("delete file fail");}fs.close();}
}