一、windows 本地系统环境配置
1、解压hadoop的安装包
将linux中的安装包直接进行解压即可
解压后进行系统环境变量设置
1.1、添加HADOOP_HOME
1.2、Path中添加
特别注意:Windows环境安装所需的bin文件包(我们这里选择3.3.6):
1、可以打开地址:https://github.com/cdarlint/winutils,里面选 3.3.6。
2、或者直接下载:https://raw.gitcode.com/cdarlint/winutils/archive/refs/heads/master.zip?path=hadoop-3.3.6/bin
1.3、D:\hadoop\hadoop-3.3.6\etc\hadoop中修改hadoop-env.cmd
set JAVA_HOME=C:\PROGRA~1\Java\jdk1.8.0_221 # C:\Program Files\Java\jdk1.8.0_221由于Program Files中存在空格,故修改为PROGRA~1
二、IDE远程管理HDFS
1、pom文件
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.3.6</version>
</dependency>
<!--https://mnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.3.6</version>
</dependency>
<!--https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>3.3.6</version>
</dependency>
</dependencies>
2、IDE代码配置
private FileSystem fs;
@Before
public void getFileSystem() throws IOException {
/**
* 设置hadoop的操作用户
*/
System.setProperty("HADOOP_USER_NAME", "root");
/**
* 创建配置文件对象,加载配置文件中的配置信息
* 1、默认读取 core-default.xml hdfs-default.xml mapred-default.xml yarn-default.xml
* 2、如果项目中有配置文件,则继续读取项目中的配置文件 core-site.xml hdfs-site.xml mapred-site.xml yarn-site.xml
* 3、读取完成后,也可以在代码中进行配置
* 优先级: 3 > 2 > 1
*/
Configuration conf = new Configuration();
// 配置属性 使用主机名需要在当前机器的host中进行配置
conf.set("fs.defaultFS", "hdfs://hadoop01:9820");
fs = FileSystem.get(conf);
System.out.println(fs.getClass().getName());
}
/**
* 上传文件
*/
@Test
public void uploadFile() throws IOException {
long start = System.currentTimeMillis();
/**
* 本地源文件路径和HDFS的路径
*/
Path src = new Path("C:\\Users\\xazyh\\Desktop\\测试规范.docx");
Path dst = new Path("/");
fs.copyFromLocalFile(src, dst);
System.out.println("上传文件用时:" + (System.currentTimeMillis() - start));
}
/**
* 下载文件
*/
@Test
public void downloadFile() throws IOException {
long start = System.currentTimeMillis();
/**
* HDFS的文件路径和本地存储的文件路径
*/
Path src = new Path("/测试规范.docx");
Path dst = new Path("C:\\Users\\xazyh\\Desktop\\测试规范Hadoop.docx");
/**
* 该方法下载文件会多余下载一个crc文件,该文件用于校验文件是否完整
*/
fs.copyToLocalFile(src, dst);
/**
* 该方法下载文件将不会下载crc文件
*/
//fs.copyToLocalFile(false, src, dst, true);
System.out.println("下载文件用时:" + (System.currentTimeMillis() - start));
}
/**
* 创建文件夹
*/
@Test
public void createDir() throws IOException {
long start = System.currentTimeMillis();
fs.mkdirs(new Path("/input"));
System.out.println("创建文件夹用时:" + (System.currentTimeMillis() - start));
}
/**
* 删除文件夹 和 文件
* Path为需要删除的文件或文件夹路径
*/
@Test
public void deleteDir() throws IOException {
//递归删除所有数据
fs.delete(new Path("/input"), true);
}
/**
* 重命名
*/
@Test
public void renameFile() throws IOException {
fs.rename(new Path("/测试规范.docx"), new Path("/测试规范Rename.docx"));
}
/**
* 判断文件夹或文件是否存在
*/
@Test
public void existsFile() throws IOException {
//判断文件夹或文件是否存在
Path path = new Path("/测试规范Rename.docx");
boolean hasExists = fs.exists(path);
System.out.println(hasExists);
//如果存在则删除文件或文件夹
if (hasExists) {
fs.delete(path, true);
}
}
/**
* IOUtils 上传&&下载 文件
*/
@Test
public void ioUtilsTest() throws IOException {
System.setProperty("HADOOP_USER_NAME", "root");
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://hadoop01:9820");
FileSystem fs = FileSystem.get(conf);
//上传
/* FileInputStream inputStream = new FileInputStream("C:\\Users\\xazyh\\Desktop\\windows_docker.docx");
FSDataOutputStream outputStream = fs.create(new Path("/windows_docker.docx"));*/
//下载
FSDataInputStream inputStream = fs.open(new Path("/windows_docker.docx"));
FileOutputStream outputStream = new FileOutputStream("C:\\Users\\xazyh\\Desktop\\windows_docker11.docx");
IOUtils.copyBytes(inputStream, outputStream, conf);
IOUtils.closeStream(inputStream);
IOUtils.closeStream(outputStream);
}
/**
* 文件状态信息查看
*/
@Test
public void listFileStatus() throws IOException {
//获取每一个文件的状态信息列表,迭代器对象
RemoteIterator<LocatedFileStatus> listLocatedStatus = fs.listLocatedStatus(new Path("/windows_docker.docx"));
//循环迭代器
while (listLocatedStatus.hasNext()) {
LocatedFileStatus fileStatus = listLocatedStatus.next();
System.out.println("基本信息:" + fileStatus);
//获取文件的所有块信息
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
System.out.println("当前块的所有副本信息:" + Arrays.toString(blockLocation.getHosts()));
System.out.println("当前块的大小:" + blockLocation.getLength());
System.out.println("当前块的副本的IP地址:" + Arrays.toString(blockLocation.getNames()));
}
System.out.println("系统块的大小:" + fileStatus.getBlockSize());
System.out.println("当前文件的大小:"+ fileStatus.getLen());
}
}