package com.lijiankun.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 {
//连接集群的NameNode地址
URI uri = new URI(“hdfs://hadoop02:8020”);
//创建一个配置文件
Configuration configuration = new Configuration();
//测试参数优先级
configuration.set(“dfs.replication”, “2”);
//用户名
String user = “root”;
//获取客户端对象
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/huaguoshan”));
}// 测试上传
@Test
public void testPut() throws IOException {
//参数解读:1.是否删除原数据 2.是否允许覆盖 3.原数据路径 4.目的地路径
fs.copyFromLocalFile(
false,true,
new Path(“D:\sunwukong.txt”),
new Path(“/xiyou/huaguoshan”));
}//测试下载
@Test
public void testGet() throws IOException {
//参数解读:1.是否删除原数据 2.源文件路径(HDFS中的) 3.目标地址路径(Windows中的) 4.是否开启文件校验
fs.copyToLocalFile(
false
,new Path(“/xiyou/huaguoshan/sunwukong.txt”),
new Path(“D:\sunwukong.txt”),
false);
}//测试删除
@Test
public void testRm() throws IOException {
//删除文件: 参数解读:1.文件路径 2.是否递归删除
// fs.delete(
// new Path(“/xiyou/huaguoshan/sunwukong.txt”),
// false);//删除空目录: 参数解读:1.文件路径 2.是否递归删除
// fs.delete(
// new Path(“/xiyou/huaguoshan/”),
// false);
//删除非空目录: 参数解读:1.文件路径 2.是否递归删除
fs.delete(
new Path("/jinguo/"),
true);
}
//文件的更名和移动
@Test
public void testMove() throws IOException {
//文件更名
//参数解读: 参数1:源文件路径 参数2:目标文件路径
// fs.rename(new Path(“/wcinput/word.txt”),new Path(“/wcinput/wordRnamed.txt”));
//文件移动
//参数解读: 参数1:源文件路径 参数2:目标文件路径
// fs.rename(new Path(“/wcinput/wordRnamed.txt”),new Path(“/word.txt”));
//目录的移动和更名
//参数解读: 参数1:源文件路径 参数2:目标文件路径
fs.rename(new Path("/wcinput/"),new Path("/wcinput2/"));
}
//获取文件详情信息
@Test
public void fileDetail() throws IOException {
//获取所有文件信息
//参数解读 1.路径 2. 是否递归查看子目录
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while (listFiles.hasNext()) {
LocatedFileStatus file = listFiles.next();
System.out.println("================"+file.getPath()+"===============");
System.out.println(file.getPermission());
System.out.println(file.getOwner());
System.out.println(file.getGroup());
System.out.println(file.getLen());
System.out.println(file.getModificationTime());
System.out.println(file.getReplication());
System.out.println(file.getBlockSize());
System.out.println(file.getPath().getName());
//获取块信息
BlockLocation[] blockLocations = file.getBlockLocations();
System.out.println(Arrays.toString(blockLocations));
}
}
//判断是文件夹还是文件
@Test
public void testFile() throws IOException {
//参数解读 1.文件路径
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
if (fileStatus.isFile()) {
System.out.println("文件"+fileStatus.getPath().getName());
}else{
System.out.println("目录"+fileStatus.getPath().getName());
}
}
}
}