在hadoo集群搭建(二)中是在shell窗口对hadoop集群进行命令操作。本章将在IDEA代码中与集群进行交互。
1.客户端环境准备
hadoop的Windows 依赖(可在评论中留下邮箱),以下为我的依赖路径
在windows中配置环境变量,以及添加Path路径。(我的配在系统变量中)
验证 Hadoop 环境变量是否正常。双击 winutils.exe,如果报如下错误。说明缺少微软运行库(正版系统往往有这个问题)。再资料包里面有对应的微软运行库安装包双击安装即可。
2. 在 IDEA 中创建一个 Maven 工程 HdfsClientDemo
导入相应的依赖坐标+日志添加
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>
在项目的 src/main/resources 目录下,新建一个文件,命名为“log4j.properties”,在文件 中填入
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
3 创建包名:com.atguigu.hdfs
4 创建 HdfsClient 类
5 操作实例
package com.yang.hdfs; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; /** * 1.获取系统文件对象 * 2.进行操作 * 3.关闭资源 */ public class HdfsClient { private FileSystem fs; @Before // 获取文件系统对象 public void testFileSystem() throws URISyntaxException, IOException, InterruptedException { Configuration configuration = new Configuration(); configuration.set("dfs.replication","2"); fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "yang"); } @After //关闭资源 public void testClose() throws IOException { fs.close(); } // 创建目录 @Test public void testMkdirs() throws IOException { fs.mkdirs(new Path("/xiyou/huaguoshan/")); } // 上传文件 @Test public void testPut() throws IOException { fs.copyFromLocalFile(false, true, new Path("C:\\ZProject\\bigdata\\huaguoshan\\niumowang.txt"), new Path("/xiyou/huaguoshan")); fs.copyFromLocalFile(false, true, new Path("C:\\ZProject\\bigdata\\huaguoshan\\sunwokong.txt"), new Path("/xiyou/huaguoshan")); } @Test //下载文件 public void testGet() throws IOException { fs.copyToLocalFile(false, new Path("/xiyou/huaguoshan"), new Path("C:\\ZProject\\bigdata\\"), true); } @Test //文件更名与移动 public void testRename() throws IOException { fs.rename(new Path("/xiyou/jinguo"), new Path("/")); } //删除 @Test public void testDel() throws IOException { fs.delete(new Path("/jinguo/shuguo.txt"),true); } //查看文件详细信息 @Test public void getDetail() throws IOException { RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/jinguo"), true); while (listFiles.hasNext()) { LocatedFileStatus fileStatus = listFiles.next(); System.out.println("============="+fileStatus.getPath()+"============"); System.out.println(fileStatus.getPermission()); System.out.println(fileStatus.getOwner()); System.out.println(fileStatus.getGroup()); System.out.println(fileStatus.getLen()); System.out.println(fileStatus.getModificationTime()); System.out.println(fileStatus.getReplication()); System.out.println(fileStatus.getBlockSize()); System.out.println(fileStatus.getPath().getName()); //获取块信息 BlockLocation[] blockLocations = fileStatus.getBlockLocations(); System.out.println(Arrays.toString(blockLocations)); } } @Test //查看是否是文件 public void filestatus() throws IOException { FileStatus[] listStatus = fs.listStatus(new Path("/")); for (FileStatus status : listStatus) { if (status.isFile()) { System.out.println("f:"+status.getPath().getName()); }else { System.out.println("d:"+status.getPath().getName()); } } } }