文章目录
一、Windows中搭建Hadoop依赖环境
步骤1:下载所需软件 解压至合适的位置
步骤2:编辑系统环境变量
1.在系统变量中添加如下内容
2.双击系统变量中Path 最后一样加入如下值
3.进入hadoop3.1.0/bin目录中 运行如下所选项
4.最后
二、使用IDEA编写代码操作HDFS
1.打开IDEA创建新的项目
2.选择Maven项目,next下一步
3.输入项目名称,之后点击finish完成
4.如下界面就是项目创建完成之后的界面,写入所需类库。
5.在pom.xml文件中写入如下内容
6.在创建一个java类,写入如下代码
7.运行测试
一、Windows中搭建Hadoop依赖环境
软件需求:
hadoop3.1.0百度网盘 请输入提取码https://pan.baidu.com/s/1CIOSH1RgrEqYv0at3Qewrg 提取码:216r
微软运行库百度网盘 请输入提取码https://pan.baidu.com/s/1VHJ9FG0ZUMxPmhMKu4SQ7w 提取码:7h3q
步骤1:下载所需软件 解压至合适的位置
我的位置:D:Study Softwarehadoop-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文件中写入如下内容
4.0.0
com.atguigu
HDFSClient
1.0-SNAPSHOT
8
8
org.apache.hadoop
hadoop-client
3.1.3
junit
junit
4.12
org.slf4j
slf4j-log4j12
1.7.30
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;
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"));
}
// 上传操作
@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 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());
}
}
}
}
7.运行测试
4.0.0 com.atguigu HDFSClient1.0-SNAPSHOT 8 8 org.apache.hadoop hadoop-client3.1.3 junit junit4.12 org.slf4j slf4j-log4j121.7.30
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;
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"));
}
// 上传操作
@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 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());
}
}
}
}
7.运行测试



