Java操作Hdfs(分布式文件系统)
文件上传实现
- 创建Maven项目,项目命名Hadoop_hdfs
- 导入pom.xml依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hadoop</groupId>
<artifactId>hadoop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hadoop_learn</name>
<dependencies>
<!- hadoop配置jar包-->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
</project>
package com.learn_hdfs;
import java.io.FileInputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class HdfsOp {
public static void main(String[] args) throws Exception{
Configuration config = new Configuration();
config.set("fs.defaultFS", "hdfs://192.168.72.100:9000");
FileSystem fileSystem = FileSystem.get(config);
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Documents\\learn.txt");
FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/learn.txt"));
IOUtils.copyBytes(fileInputStream, fsDataOutputStream, 1024, true);
}
}
完成上传!
文件下载操作
- 编写下载文件操作类FileHdfsDownLoad类进行实现
package com.learn_hdfs;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class FileHdfsDownLoad{
public static void main(String[] args) throws Exception{
Configuration config = new Configuration();
config.set("fs.defaultFS", "hdfs://192.168.72.100:9000");
FileSystem fileSystem = FileSystem.get(config);
downloadFile(fileSystem);
}
private static void downloadFile(FileSystem fileSystem) throws Exception{
FSDataInputStream fsDataInputStream = fileSystem.open(new Path("/learn.txt"));
FileOutputStream foFileOutputStream = new FileOutputStream("C:\\Users\\Documents\\learn.txt");
IOUtils.copyBytes(fsDataInputStream, foFileOutputStream, 1024, true);
}
}
- 在启动集群后进行执行程序进行验证是否下载了learn.txt文件
下载成功!
删除文件操作
package com.learn_hdfs;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class HdfsOp {
public static void main(String[] args) throws Exception{
Configuration config = new Configuration();
config.set("fs.defaultFS", "hdfs://192.168.72.100:9000");
FileSystem fileSystem = FileSystem.get(config);
deleteFile(fileSystem);
}
private static void deleteFile(FileSystem fileSystem) throws Exception{
boolean result = fileSystem.delete(new Path("/learn.txt"));
if(result) {
System.out.print("删除成功");
}else {
System.out.print("删除失败");
}
}
}
- 确保hdfs中存在要删除的文件learn.txt
执行代码 - 检查hdfs中是否还存在该文件
不存在该文件,因此删除完毕。
完整代码如下:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class HdfsOp {
public static void main(String[] args) throws Exception{
Configuration config = new Configuration();
config.set("fs.defaultFS", "hdfs://192.168.72.100:9000");
FileSystem fileSystem = FileSystem.get(config);
putFile(fileSystem);
downloadFile(fileSystem);
deleteFile(fileSystem);
}
private static void putFile(FileSystem fileSystem) throws Exception{
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\dang\\Documents\\learn.txt");
FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/learn.txt"));
IOUtils.copyBytes(fileInputStream, fsDataOutputStream, 1024, true);
}
private static void downloadFile(FileSystem fileSystem) throws Exception{
FSDataInputStream fsDataInputStream = fileSystem.open(new Path("/learn.txt"));
FileOutputStream foFileOutputStream = new FileOutputStream("C:\\Users\\dang\\Documents\\learn.txt");
IOUtils.copyBytes(fsDataInputStream, foFileOutputStream, 1024, true);
}
private static void deleteFile(FileSystem fileSystem) throws Exception{
boolean result = fileSystem.delete(new Path("/learn.txt"));
if(result) {
System.out.print("删除成功");
}else {
System.out.print("删除失败");
}
}
}
|