每日总结
HDFS 的 API 操作
package com.atguigu.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
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;
import static com.google.gson.internal.bind.TypeAdapters.URI;
public class HdfsClient {
private FileSystem fs;
@Before
public void init() throws URISyntaxException, IOException, InterruptedException {
URI uri=new URI("hdfs://hadoop102:8020");
// 1 获取文件系统
Configuration configuration = new Configuration();
// FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration);
fs = FileSystem.get(uri, configuration,"chenghaixiang");
}
@After
// 3 关闭资源
public void close() throws IOException {
fs.close();
}
@Test
public void testMkdirs() throws IOException, URISyntaxException, InterruptedException {
// 2 创建目录
fs.mkdirs(new Path("/xiyou/huaguoshan/"));
}
@Test
//上传
public void testPut() throws IOException {
//1.表示删除原数据,2.是否覆盖,3.原数据路径,4.目标数据路径
fs.copyFromLocalFile(false,false,new Path("D:\\student1.txt"),new Path("hdfs://hadoop102/xiyou/huaguoshan"));
}
@Test
//文件下载
public void testGet() throws IOException {
//1.原文件是否删除 2.原文件hdfs路径 3.目标地址路径 4.是否不进行crc校验
fs.copyToLocalFile(false,new Path("hdfs://hadoop102/xiyou/huaguoshan"),new Path("D:\\"),false);
}
@Test
//删除
public void testRm() throws IOException {
//1.删除路径 2.是否递归删除
fs.delete(new Path("/xiyou/huaguoshan/student1.txt"),false);
//删除非空目录
/*fs.delete(new Path("/xiyou/huaguoshan/"),true);*/
}
@Test
//文件更名和移动
public void testmv() throws IOException {
//1.原文件路径 2.目标路径
//对文件名修改
/*fs.rename(new Path("/xiyou/huaguoshan/student1.txt"),new Path("/xiyou/huaguoshan/chenghaixiangstudent.txt"));*/
//对文件的移动和修改
fs.rename(new Path("/xiyou/huaguoshan/chenghaixiangstudent.txt"),new Path("/xiyou/huaguoshan1/xx.txt"));
//目录的更名
//fs.rename(new Path("/xiyou/huaguoshan"),new Path("/xiyou/huaguoshanchenghai"));
}
//获取文件详细信息
@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));
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App