文章目录
3.进入hadoop3.1.0/bin目录中 运行如下所选项
一、Windows中搭建Hadoop依赖环境
软件需求:
-
hadoop3.1.0百度网盘 请输入提取码https://pan.baidu.com/s/1CIOSH1RgrEqYv0at3Qewrg 提取码:216r
-
微软运行库百度网盘 请输入提取码https://pan.baidu.com/s/1VHJ9FG0ZUMxPmhMKu4SQ7w 提取码:7h3q
步骤1:下载所需软件 解压至合适的位置
我的位置:D:\Study Software\hadoop-3.1.0
微软运行库留着备用即可
步骤2:编辑系统环境变量
1.在系统变量中添加如下内容
2.双击系统变量中Path 最后一样加入如下值
3.进入hadoop3.1.0/bin目录中 运行如下所选项
4.最后
如果运行后有一个黑色窗口一闪而过,就可以了,如果出现了报错 缺少什么库(微软),就安装本文顶端要求下载第二个软件即可,然后重新运行winutils.exe
二、使用IDEA编写代码操作HDFS
1.打开IDEA创建新的项目
2.选择Maven项目,next下一步
3.输入项目名称,之后点击finish完成
4.如下界面就是项目创建完成之后的界面,写入所需类库。
5.在pom.xml文件中写入如下内容
<?xml version="1.0" encoding="UTF-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.atguigu</groupId> <artifactId>HDFSClient</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <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> </project>
6.在创建一个java类,写入如下代码
package com.auguigu.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、关闭资源 * HDFS zookeeper */ public class HdfsClient { private FileSystem fs; @Before public void init() throws URISyntaxException, IOException, InterruptedException { // 获取连接集群的地址 URI uri = new URI("hdfs://hadoop102:8020"); // 创建一个配置文件 Configuration configuration = new Configuration(); //设置配置文件中副本的数量 configuration.set("dfs.replication", "2"); // 用户 String user = "student"; //获取到了客户端对象 fs = FileSystem.get(uri, configuration, user); } @After public void close() throws IOException { // 关闭资源 fs.close(); } //========================= 方法开始 ========================= @Test public void testmkdir() throws URISyntaxException, IOException, InterruptedException { // 创建一个文件夹 fs.mkdirs(new Path("/xiyou/huagoushan")); } // 上传操作 /** * 参数优先级 * 代码里面 > 项目资源目录下的配置文件(resources/hdfs-site.xml) > linux中hdfs配置文件 */ @Test public void testPut() throws IOException { // 参数解读:参数一:表示删除元数据 参数二:是否允许覆盖 参数三:元数据路径 参数四:目的地路径 fs.copyFromLocalFile(false, true, new Path("D:\\sunwukong.txt"), new Path("hdfs://hadoop102/xiyou/huaguoshan/")); } //文件下载 @Test public void testGet() throws IOException { /** * 参数解读: 参数一:源文件是否删除 参数二:源文件的路径 参数三:目标路径 参数四:是否关闭校验 */ fs.copyToLocalFile(false, new Path("hdfs://hadoop102/xiyou/huaguoshan"), new Path("D:\\sunwukong2.txt"), true); } //删除 @Test public void testRm() throws IOException { /** * 参数解读: 参数一:要删除的路径 参数二:是否递归删除 */ // 删除文件 //fs.delete(new Path("hdfs://hadoop102/jdk-8u212-linux-x64.tar.gz"),false); // 删除空目录 加不加hdfs一样的 //fs.delete(new Path("/xiyou"),false); //删除非空目录 fs.delete(new Path("hdfs://hadoop102/xiyou"), true); } //文件及文件夹的更名和移动 @Test public void testMv() throws IOException { /** * 参数解读: 参数一:源文件路径 参数二:目标文件的路径 */ //对文件名称的修改 //fs.rename(new Path("hdfs://hadoop102/sanguo/zhangfei.txt"),new Path("hdfs://hadoop102/sanguo/guanyu.txt")); //文件的更名和移动 //fs.rename(new Path("hdfs://hadoop102/sanguo/guanyu.txt"),new Path("hdfs://hadoop102/dianwei.txt")); // 文件夹更名 fs.rename(new Path("/input"), new Path("/output0")); } //获取文件详细信息 @Test public void fileDetail() throws IOException { // 获取所有文件信息 RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), 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 testFile() throws IOException { FileStatus[] listStatus = fs.listStatus(new Path("/")); for (FileStatus status : listStatus) { if (status.isFile()) { System.out.println("文件:" + status.getPath().getName()); } else { System.out.println("目录:" + status.getPath().getName()); } } } }