1、环境准备
1.1、下载window版本的hadoop-3.1.0
1.2、配置HADOOP_HOME环境变量
1.2、配置Path环境变量
2、代码演示
2.1、创建maven项目,导入pom坐标
org.apache.hadoop
hadoop-client
3.1.3
junit
junit
4.12
org.slf4j
slf4j-log4j12
1.7.30
2.2、日志配置
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
2.3、代码实现
package com.song.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 {
// hdfs://hadoop102:8020 是nameNode的通信地址
URI uri = new URI("hdfs://hadoop102:8020");
// 获取文件系统
Configuration configuration = new Configuration();
// 定义具有操作权限的用户
String user = "song";
// 获取客户端对象
fs = FileSystem.get(uri, configuration, user);
}
@After
public void close() throws IOException {
// 关闭资源
fs.close();
}
//测试创建目录
@Test
public void testMkdirs() throws IOException, URISyntaxException, InterruptedException {
// 业务操作
fs.mkdirs(new Path("/xiyou/huaguoshan/"));
}
//测试上传
@Test
public void testPut() throws IOException, URISyntaxException, InterruptedException {
// 业务操作
fs.copyFromLocalFile(false, true, new Path("D:\test_data\sunwukong.txt"), new Path("/xiyou/huaguoshan"));
}
//测试下载
@Test
public void testGet() throws IOException, URISyntaxException, InterruptedException {
// 业务操作
fs.copyToLocalFile(false, new Path("/xiyou/huaguoshan/sunwukong"), new Path("D:\test_data\sunwukong"), false);
}
//测试删除
@Test
public void testRm() throws IOException, URISyntaxException, InterruptedException {
// 业务操作
fs.delete(new Path("/xiyou"), true);
}
//测试删除
@Test
public void testMv() throws IOException, URISyntaxException, InterruptedException {
// 业务操作
fs.rename(new Path("/jinguo/shuguo.txt"), new Path("/shuguo1.txt"));
}
// 获取文件详情
@Test
public void testListFiles() 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 testListStatus() throws IOException, InterruptedException, URISyntaxException{
// 2 判断是文件还是文件夹
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
// 如果是文件
if (fileStatus.isFile()) {
System.out.println("文件:"+fileStatus.getPath().getName());
}else {
System.out.println("目录:"+fileStatus.getPath().getName());
}
}
}
}