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="atguigu";
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/实验一"));
}
@Test
public void update() throws IOException {
Path path = new Path("/xiyou/实验一/zhaoyi.txt");
//通过fs的append方法实现对文件的追加操作
FSDataOutputStream fos = fs.append(path);
//通过fos写入数据
fos.write("
yinzhengjie".getBytes());
//释放资源
fos.close();
fs.close();
}
//在HDFS文件系统中读取数据
@Test
public void read() throws IOException {
Path path = new Path("/xiyou/实验一/zhaoyi.txt");
//通过fs读取数据
FSDataInputStream fis = fs.open(path);
int len = 0;
fis.seek(2);
byte[] buf = new byte[4096];
while ((len = fis.read(buf)) != -1){
System.out.println(len);
System.out.println(new String(buf, 0, len));
}
}
//上传
@Test
public void testPut() throws IOException {
//参数:1.是否删除原数据,2.是否允许覆盖,3.原数据路径 4.目的地路径
fs.copyFromLocalFile(false,false,new Path("D:\sunwukong.txt"),new Path("/xiyou/huaguoshan"));
}
//文件下载
@Test
public void testGet() throws IOException {
//最后一个参数决定是否进行循环冗余检验,false进行,true不进行
fs.copyToLocalFile(false,new Path("/xiyou/huaguoshan"),new Path("D:\"),false);
}
//文件删除
@Test
public void testRm() throws IOException {
//1.删除路径2.是否递归删除 可删除文件,空/非空目录,删除非空目录要递归删除
fs.delete(new Path("/jdk-8u212-linux-x64.tar.gz"),false);
}
// 文件的更名和移动
@Test
public void testmv() throws IOException {
// 参数解读:参数1 :原文件路径; 参数2 :目标文件路径
// 对文件名称的修改
//fs.rename(new Path("/input/word.txt"), new Path("/input/ss.txt"));
// 文件的移动和更名
//fs.rename(new Path("/input/ss.txt"),new Path("/cls.txt"));
// 目录更名
fs.rename(new Path("/input"), new Path("/output"));
}
// 获取文件详细信息
@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());
}
}
}
}