当前位置:   article > 正文

HDFS的API操作 (Eclipse版)

hdfs的api操作

目录

一、环境准备

1.在windows上安装hadoop

2.配置HADOOP_HOME环境变量

3.配置Path变量

 4.创建一个maven工程  HdfsClient

 5.导入相应的依赖坐标

1.点击pom.xml,在里面添加相应的依赖... 

2. 需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入

​编辑

6.创建包名:com.hadoop.hdfs 

7.创建HdfsClient类

 在com.hadoop.hdfs包下,创建HdfsClient类,代码如下:

8.执行程序

二.HDFS的API操作

1.文件上传

2.文件下载

 3.文件删除

4.文件更名

5.文件详情查看

6.判断是文件还是文件夹

三.HDFS的I/O流操作

(一)HDFS文件上传

1.需求分析:要求将本地D盘的文件Test4.txt上传到HDFS根目录

2.编写代码:

(二)HDFS文件下载

1.需求分析:从HDFS上下载banzhang.txt到D盘上

2.编写代码:

(三)定位文件读取

1.需求:分块读取HDFS上的大文件,比如根目录下的/hadoop-2.7.2.tar.gz

2.编写代码:

一、环境准备

1.在windows上安装hadoop

已经准备好的hadoop jar包链接如下,要解压到非中文路径!!!

链接:https://pan.baidu.com/s/1BM4_O-HpDlB5xIgejadURg?pwd=9zuj 
提取码:9zuj

2.配置HADOOP_HOME环境变量

3.配置Path变量

 编辑环境变量,将 %HADOOP_HOME%\bin  添加到Path中去

 4.创建一个maven工程  HdfsClient

打开Eclipse,点击File   >>     New    >>     Maven Project

    

 5.导入相应的依赖坐标

1.点击pom.xml,在里面添加相应的依赖<dependencies>...</dependencies> 

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.logging.log4j</groupId>
  9. <artifactId>log4j-core</artifactId>
  10. <version>2.8.2</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.hadoop</groupId>
  14. <artifactId>hadoop-common</artifactId>
  15. <version>2.7.2</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.hadoop</groupId>
  19. <artifactId>hadoop-client</artifactId>
  20. <version>2.7.2</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.apache.hadoop</groupId>
  24. <artifactId>hadoop-hdfs</artifactId>
  25. <version>2.7.2</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>jdk.tools</groupId>
  29. <artifactId>jdk.tools</artifactId>
  30. <version>1.8</version>
  31. <scope>system</scope>
  32. <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
  33. </dependency>
  34. </dependencies>

2. 需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入

  1. log4j.rootLogger=INFO, stdout
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  4. log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
  5. log4j.appender.logfile=org.apache.log4j.FileAppender
  6. log4j.appender.logfile.File=target/spring.log
  7. log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
  8. log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

6.创建包名:com.hadoop.hdfs 

在项目的src/main/java目录下,创建包名com.hadoop.hdfs 

7.创建HdfsClient类

 在com.hadoop.hdfs包下,创建HdfsClient类,代码如下:

  1. package com.hadoop.hdfs;
  2. import java.io.IOException;
  3. import java.net.URI;
  4. import java.net.URISyntaxException;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.FileSystem;
  7. import org.apache.hadoop.fs.Path;
  8. import org.junit.Test;
  9. public class HdfsClient{
  10. @Test
  11. public void testMkdirs() throws IOException, InterruptedException, URISyntaxException {
  12. Configuration conf = new Configuration();
  13. // conf.set("fs.defaultFS","hdfs://hadoop130:8020");
  14. //1获取hdfs客户端对象
  15. // FileSystem fs =FileSystem.get(conf);
  16. FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"),conf , "hadoop");
  17. //2在hdfs上创建路径
  18. fs.mkdirs(new Path("/fang/Test"));
  19. //3关闭资源
  20. fs.close();
  21. System.out.print("over");
  22. }
  23. }

注:hdfs://hadoop130:8020这里是我自己的端口号和主机名,后面“hadoop”是用户名,这些根据自己的来设置!!!这里要保证自己的集群是开启的

8.执行程序

运行时需要配置用户名称

客户端去操作HDFS时,是有一个用户身份的。默认情况下,HDFS客户端API会从JVM中获取一个参数来作为自己的用户身份:-DHADOOP_USER_NAME=hadoop,hadoop为用户名称。

执行后的结果图:

 

二.HDFS的API操作

1.文件上传

  1. //1.文件上传
  2. @Test
  3. public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
  4. //1获取fs对象
  5. Configuration conf = new Configuration();
  6. FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"),conf,"hadoop");
  7. //2执行上传API
  8. fs.copyFromLocalFile(new Path("D:/Hadoop2.x/shiyan.txt"), new Path("/shiyan.txt"));
  9. //3关闭资源
  10. fs.close();
  11. System.out.print("执行完毕!");
  12. }

2.文件下载

  1. //2.文件下载
  2. @Test
  3. public void testCopyLocalFile() throws IOException, InterruptedException, URISyntaxException {
  4. //1.获取对象
  5. Configuration conf = new Configuration();
  6. FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
  7. //2执行下载操作
  8. fs.copyToLocalFile(new Path("/shiyan.txt"), new Path("D:/Hadoop2.x/shiyan.txt"));
  9. //fs.copyToLocalFile(false, null, null, false);
  10. //3关闭资源
  11. fs.close();
  12. System.out.print("下载完成!");
  13. }

 3.文件删除

  1. //3.文件删除
  2. @Test
  3. public void testDelete() throws IOException, InterruptedException, URISyntaxException {
  4. //1.获取对象
  5. Configuration conf = new Configuration();
  6. FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
  7. //2文件删除
  8. fs.delete(new Path("/0529"), true);
  9. //3关闭资源
  10. fs.close();
  11. System.out.print("删除成功!");
  12. }

4.文件更名

  1. //4文件更名
  2. @Test
  3. public void testRename() throws IOException, InterruptedException, URISyntaxException {
  4. //1.获取对象
  5. Configuration conf = new Configuration();
  6. FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
  7. //2执行更名操作
  8. fs.rename(new Path("/shiyan.txt"), new Path("/Test4.txt"));
  9. //3关闭资源
  10. fs.close();
  11. System.out.print("文件更名成功!");
  12. }

5.文件详情查看

  1. //5文件详情查看
  2. @Test
  3. public void testListFiles() throws IOException, InterruptedException, URISyntaxException {
  4. //1.获取对象
  5. Configuration conf = new Configuration();
  6. FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
  7. //2查看文件详情
  8. RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
  9. while(listFiles.hasNext()) {
  10. LocatedFileStatus fileStatus =listFiles.next();
  11. //查看文件名称,权限,长度,块信息
  12. System.out.println(fileStatus.getPath().getName());//文件名称
  13. System.out.println(fileStatus.getPermission());//文件权限
  14. System.out.println(fileStatus.getLen());
  15. BlockLocation[] blockLocations = fileStatus.getBlockLocations();
  16. for (BlockLocation blockLocation : blockLocations) {
  17. String[] hosts = blockLocation.getHosts();
  18. for (String host : hosts) {
  19. System.out.println(host);
  20. }
  21. }
  22. System.out.println("--------分割线---------");
  23. }
  24. //3关闭资源
  25. fs.close();
  26. }

6.判断是文件还是文件夹

  1. //6判断是文件还是文件夹
  2. @Test
  3. public void testListStatus() throws IOException, InterruptedException, URISyntaxException {
  4. //1.获取对象
  5. Configuration conf = new Configuration();
  6. FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
  7. //2判断操作
  8. FileStatus[] listStatus = fs.listStatus(new Path("/"));
  9. for (FileStatus fileStatus : listStatus) {
  10. if(fileStatus.isFile()) {
  11. //文件
  12. System.out.println("f:"+fileStatus.getPath().getName());
  13. }else {
  14. //文件夹
  15. System.out.println("d:"+fileStatus.getPath().getName());
  16. }
  17. }
  18. //3关闭资源
  19. fs.close();
  20. }

三.HDFS的I/O流操作

(一)HDFS文件上传

1.需求分析:要求将本地D盘的文件Test4.txt上传到HDFS根目录

2.编写代码:

  1. public class HDFSIO {
  2. //把本地d盘上的Test4.txt文件上传到hdfs根目录
  3. @Test
  4. public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException{
  5. //1获取对象
  6. Configuration conf = new Configuration();
  7. FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
  8. //2获取输入流
  9. FileInputStream fis = new FileInputStream(new File("D:/Hadoop2.x/shiyan.txt"));
  10. //3获取输出流
  11. FSDataOutputStream fos = fs.create(new Path("/banzhang.txt"));
  12. //4流的对拷
  13. IOUtils.copyBytes(fis, fos, conf);
  14. //5关闭资源
  15. IOUtils.closeStream(fos);
  16. IOUtils.closeStream(fis);
  17. fs.close();
  18. }

(二)HDFS文件下载

1.需求分析:从HDFS上下载banzhang.txt到D盘上

2.编写代码:

  1. //从HDFS上下载banzhang.txt文件到本地d盘上
  2. @SuppressWarnings("unused")
  3. @Test
  4. public void getFileFormHDFS() throws IOException, InterruptedException, URISyntaxException{
  5. //1获取对象
  6. Configuration conf = new Configuration();
  7. FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
  8. //2获取输入流
  9. FSDataInputStream fis = fs.open(new Path("/banzhang.txt"));
  10. //3获取输出流
  11. FileOutputStream fos = new FileOutputStream(new File("d:/Hadoop2.x/banzhang.txt"));
  12. //4流的对拷
  13. IOUtils.copyBytes(fis, fos, conf);
  14. //5关闭资源
  15. IOUtils.closeStream(fos);
  16. IOUtils.closeStream(fis);
  17. fs.close();
  18. }

(三)定位文件读取

1.需求:分块读取HDFS上的大文件,比如根目录下的/hadoop-2.7.2.tar.gz

2.编写代码:

下载第一块:

  1. //下载第一块
  2. @Test
  3. public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException {
  4. //1获取对象
  5. Configuration conf = new Configuration();
  6. FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
  7. //2获取输入流
  8. FSDataInputStream fis = fs.open(new Path("/hadoop-3.1.3.tar.gz"));
  9. //3获取输出流
  10. FileOutputStream fos = new FileOutputStream(new File("d:/hadoop-3.1.3.tar.gz.part1"));
  11. //4流的对拷(只拷贝128m)
  12. byte[] buf = new byte[1024];
  13. for (int i = 0; i < 1024*1024 ; i++) {
  14. fis.read(buf);
  15. fos.write(buf);
  16. }
  17. //5关闭资源
  18. IOUtils.closeStream(fos);
  19. IOUtils.closeStream(fis);
  20. fs.close();
  21. }

下载第二块:

  1. //下载第二块
  2. @Test
  3. public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException {
  4. //1获取对象
  5. Configuration conf = new Configuration();
  6. FileSystem fs = FileSystem.get(new URI("hdfs://hadoop130:8020"), conf, "hadoop");
  7. //2获取输入法
  8. FSDataInputStream fis = fs.open(new Path("/hadoop-3.1.3.tar.gz"));
  9. //3设置指定读取起点
  10. //重点
  11. fis.seek(1024*1024*128);
  12. //4获取输出流
  13. FileOutputStream fos = new FileOutputStream(new File("d:/hadoop-3.1.3.tar.gz.part2"));
  14. //5流的对拷
  15. IOUtils.copyBytes(fis, fos, conf);
  16. //6关闭资源
  17. IOUtils.closeStream(fos);
  18. IOUtils.closeStream(fis);
  19. fs.close();
  20. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/40463
推荐阅读
  

闽ICP备14008679号