赞
踩
先将Linux上的hadoop解压版(tar -zxvf解压)复制一份,再删除(rm -rf)其share子目录(不然下载的文件比较大)
利用MobaXterm下载到本地(如D:\othersSofts目录下),改名为hadoop3.1.3,再在linux虚拟机上删除供下载用的hadoop目录文件
在其/bin目录下添加其所需要的Windows依赖,遇到同名文件则替换
创建一个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>
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 */ @Before public 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 */ @After public void close() throws IOException { fs.close(); System.out.println("hdfs文件系统已关闭!"); } /** * 创建hdfs上的路径 * * @throws IOException */ @Test public void createPath() throws IOException { boolean createFlag = fs.mkdirs(new Path("/hdfs_api")); if (createFlag) { System.out.println("路径创建成功"); } else { System.out.println("路径创建失败"); } } /** * 删除hdfs上的路径 * * @throws IOException */ @Test public 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 */ @Test public 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 */ @Test public 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 */ @Test public 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 */ @Test public 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 */ @Test public 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("下载文件成功"); } }
注意:要在每个注解@Test下右击执行对应的方法,如:
再在Maven项目中完成初始化文件系统(注解使用@Before)和关闭文件系统(注解使用@After)部分:
//文件系统对象 FileSystem fs = null; /** * 初始化文件系统 * * @throws URISyntaxException * @throws IOException */ @Before public 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 */ @After public void close() throws IOException { fs.close(); System.out.println("hdfs文件系统已关闭!"); }
/** * 创建hdfs上的路径 * * @throws IOException */ @Test public void createPath() throws IOException { boolean createFlag = fs.mkdirs(new Path("/hdfs_api")); if (createFlag) { System.out.println("路径创建成功"); } else { System.out.println("路径创建失败"); } }
输入hadoop指令上查看创建/hdfs_api的结果:
/** * 删除hdfs上的路径 * * @throws IOException */ @Test public 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的结果:
/** * 创建hdfs文件并写入数据 * @throwsIOException */ @Test public 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指令上查看写入的数据
/**
*删除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之后被删除:
/** * hdfs上的文件移动路径并改名 *@throws IOException */ @Test public 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指令查看:
/**
* 读取hdfs上的文件内容
* @throws IOException
*/
@Test
public 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 */ @Test public 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 */ @Test public 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("下载文件成功"); }
下载结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。