Java实现hdfs的8个api操作
Java实现hdfs的8个api操作
一、预处理准备
1. 配置本地hadoop3.1.3目录文件
先将Linux上的hadoop解压版(tar -zxvf解压)复制一份,再删除(rm -rf)其share子目录(不然下载的文件比较大)
利用MobaXterm下载到本地(如D:\\othersSofts目录下),改名为hadoop3.1.3,再在linux虚拟机上删除供下载用的hadoop目录文件
在其/bin目录下添加其所需要的Windows依赖,遇到同名文件则替换
2. 配置环境变量
二、Maven项目依赖
创建一个Maven项目,在pom.xml中添加如下依赖:
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>3.1.3</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>3.1.3</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>3.1.3</version></dependency>
三、Java源代码
package org.igeek.hdfsapi;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;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;/* 使用hdfs的api进行操作*/
public class HdfsApiTest {//文件系统对象FileSystem fs = null;/* 初始化文件系统 @throws URISyntaxException* @throws IOException*/@Beforepublic void init() throws URISyntaxException, IOException, InterruptedException {//创建一个配置对象Configuration conf = new Configuration();// hdfs中的NameNode地址//方法一: ip地址:端口
// String hdfsPath ="hdfs://192.168.31.53:8020";//方法二:hostname:端口String hdfsPath = "hdfs://bigdata03:8020";String userName = "root";//获取文件系统对象fs = FileSystem.get(new URI(hdfsPath), conf, userName);System.out.println("hdfs文件系统对象初始化完成!");}/* 关闭文件系统 @throws IOException*/@Afterpublic void close() throws IOException {fs.close();System.out.println("hdfs文件系统已关闭!");}/* 创建hdfs上的路径 @throws IOException*/@Testpublic void createPath() throws IOException {boolean createFlag = fs.mkdirs(new Path("/hdfs_api"));if (createFlag) {System.out.println("路径创建成功");} else {System.out.println("路径创建失败");}}/* 删除hdfs上的路径 @throws IOException*/@Testpublic void deletePath() throws IOException {Path path = new Path("/test");//判断删除路径是否存在if (fs.exists(path)) {//使用递归删除boolean deleteFlag = fs.delete(path, true);if (deleteFlag) {System.out.println("删除路径成功");} else {System.out.println("删除路径失败");}}}/* 创建hdfs文件并写入数据* @throwsIOException*/@Testpublic void createFile() throws IOException {//创建文件路径Path path = new Path("/hdfs_api/add.txt");FSDataOutputStream fos = fs.create(path);//写出的数据String content = "我是通过api写入的数据";//通过流进行数据写出fos.write(content.getBytes());// 流的刷新fos.flush();}/*删除hdfs上的文件*@throws IOException*/
@Test
public void deleteFile() throws IOException{boolean deleteFlag = fs.deleteOnExit(new Path( "/touchz.txt"));if(deleteFlag){System.out. println("删除文件成功");} else {System. out. println("删除文件失败");}
}/* hdfs上的文件移动路径并改名*@throws IOException*/@Testpublic void moveFile() throws IOException {//文件的源路径Path src = new Path("/put.txt");//文件移动后的路径Path dst = new Path("/hdfs_api/put_new.txt");boolean renameFlag = fs.rename(src, dst);if (renameFlag) {System.out.println("文件移动成功");} else {System.out.println("文件移动失败");}}/* 读取hdfs上的文件内容* @throws IOException*/@Testpublic void readFile() throws IOException {FSDataInputStream fis = fs.open(new Path("/hdfs_api/add.txt"));IOUtils.copyBytes(fis, System.out, 2048, false);System.out.println("\\n");}/* 上传windows下的本地文件到hdfs上* @throws IOException*/@Testpublic void uploadEile() throws IOException {//要上传的hdfs路径Path src = new Path("D:\\\\othersofts\\\\hadoop3.1.3\\\\etc\\\\hadoop\\\\core-site.xml");//文件的本地路径Path dst = new Path("/hdfs_api");fs.copyFromLocalFile(true, src, dst);System.out.println("文件从本地上传hdfs成功");}/* 从hdfs上下载文件到本地* @throws IOException*/@Testpublic void downloadFile() throws IOException {// hdfs路径Path src = new Path("/hdfs_api/add.txt");//本地路径Path dst = new Path("D:\\\\");fs.copyToLocalFile(false, src, dst,false);System.out.println("下载文件成功");}}
四、api操作的实现
注意:要在每个注解@Test下右击执行对应的方法,如:
1. 实现前的准备
再在Maven项目中完成初始化文件系统(注解使用@Before)和关闭文件系统(注解使用@After)部分:
//文件系统对象FileSystem fs = null;/* 初始化文件系统 @throws URISyntaxException* @throws IOException*/@Beforepublic void init() throws URISyntaxException, IOException, InterruptedException {//创建一个配置对象Configuration conf = new Configuration();// hdfs中的NameNode地址//方法一: ip地址:端口
// String hdfsPath ="hdfs://192.168.31.53:8020";//方法二:hostname:端口String hdfsPath = "hdfs://bigdata03:8020";String userName = "root";//获取文件系统对象fs = FileSystem.get(new URI(hdfsPath), conf, userName);System.out.println("hdfs文件系统对象初始化完成!");}/* 关闭文件系统 @throws IOException*/@Afterpublic void close() throws IOException {fs.close();System.out.println("hdfs文件系统已关闭!");}
2. 创建hdfs上的路径
/* 创建hdfs上的路径 @throws IOException*/@Testpublic void createPath() throws IOException {boolean createFlag = fs.mkdirs(new Path("/hdfs_api"));if (createFlag) {System.out.println("路径创建成功");} else {System.out.println("路径创建失败");}}
输入hadoop指令上查看创建/hdfs_api的结果:
3. 删除hdfs上的路径
/* 删除hdfs上的路径 @throws IOException*/@Testpublic void deletePath() throws IOException {Path path = new Path("/test");//判断删除路径是否存在if (fs.exists(path)) {//使用递归删除boolean deleteFlag = fs.delete(path, true);if (deleteFlag) {System.out.println("删除路径成功");} else {System.out.println("删除路径失败");}}}
输入hadoop指令上查看删除/test的结果:
4. 创建hdfs文件并写入数据
/* 创建hdfs文件并写入数据* @throwsIOException*/@Testpublic void createFile() throws IOException {//创建文件路径Path path = new Path("/hdfs_api/add.txt");FSDataOutputStream fos = fs.create(path);//写出的数据String content = "我是通过api写入的数据";//通过流进行数据写出fos.write(content.getBytes());// 流的刷新fos.flush();}
输入hadoop指令上查看写入的数据
5. 删除hdfs上的文件
/*删除hdfs上的文件*@throws IOException*/
@Test
public void deleteFile() throws IOException{boolean deleteFlag = fs.deleteOnExit(new Path( "/touchz.txt"));if(deleteFlag){System.out. println("删除文件成功");} else {System. out. println("删除文件失败");}
}
输入hadoop指令发现已有的/touchz.txt之后被删除:
6. hdfs上的文件移动路径并改名
/* hdfs上的文件移动路径并改名*@throws IOException*/@Testpublic void moveFile() throws IOException {//文件的源路径Path src = new Path("/put.txt");//文件移动后的路径Path dst = new Path("/hdfs_api/put_new.txt");boolean renameFlag = fs.rename(src, dst);if (renameFlag) {System.out.println("文件移动成功");} else {System.out.println("文件移动失败");}}
输入hadoop指令查看:
7.读取hdfs上的文件内容
/* 读取hdfs上的文件内容* @throws IOException*/@Testpublic void readFile() throws IOException {FSDataInputStream fis = fs.open(new Path("/hdfs_api/add.txt"));IOUtils.copyBytes(fis, System.out, 2048, false);System.out.println("\\n");}
8.上传windows下的本地文件到hdfs上
/* 上传windows下的本地文件到hdfs上* @throws IOException*/@Testpublic void uploadEile() throws IOException {//要上传的hdfs路径Path src = new Path("D:\\\\othersofts\\\\hadoop3.1.3\\\\etc\\\\hadoop\\\\core-site.xml");//文件的本地路径Path dst = new Path("/hdfs_api");fs.copyFromLocalFile(true, src, dst);System.out.println("文件从本地上传hdfs成功");}
9. 从hdfs上下载文件到本地
/* 从hdfs上下载文件到本地* @throws IOException*/@Testpublic void downloadFile() throws IOException {// hdfs路径Path src = new Path("/hdfs_api/add.txt");//本地路径Path dst = new Path("D:\\\\");fs.copyToLocalFile(false, src, dst,false);System.out.println("下载文件成功");}
下载结果: