hdfs集成springboot使用方法
1.导入maven依赖
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client-api</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client-runtime</artifactId>
<version>3.3.6</version>
</dependency>2.配置Configuration信息
1)方法1:通过将hdfs的两个配置文件(hdfs-site.xml、core-site.xml)放到resources文件夹下后,新建Configuration的时候设置为true会自动读取,也可以通过conf.set(“配置”,“值”)来修改配置项
//创建配置,是否引用core-site.xml和hdfs-site.xml配置文件,true是引用
Configuration conf = new Configuration(true);
//创建文件连接流,指定namenode、conf和连接的用户名
FileSystem fs = FileSystem.get(new URI("mycluster"),conf,"hadoop");2)方法2:将Configuration设置为false,不加载默认配置文件,直接指定namenode对应的ip和端口如:hdfs://192.168.132.101:8081替换mycluster
Configuration conf = new Configuration(false);
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.132.101:8081"),conf,"hadoop");3.hdfs集成springboot基本命令
1)判断文件是否存在
fs.exists(new Path("/out.txt"))2)创建文件夹
fs.mkdirs(new Path("/dir1"));3)创建文件夹并设置权限为文件所有者可读可写,文件所有组可读可写,其他人可读
fs.mkdirs(new Path("/dir2"),new FsPermission(FsAction.READ_WRITE,FsAction.READ_WRITE,FsAction.READ));4)删除文件夹
fs.delete(new Path("/dir1"),true);5)创建文件并输入文本
如果文件存在,默认会覆盖, 可以通过第二个参数进行控制。第三个参数可以控制使用缓冲区的大小
FSDataOutputStream out = fs.create(new Path("/test.txt"),true, 4096);
out.write("hello hadoop!".getBytes());
out.flush();
out.close();6)读取文本
FSDataInputStream inputStream = fs.open(new Path("/test.txt"));
byte[] contextBytes = new byte[1024];
inputStream.read(contextBytes);
String context = new String(contextBytes,"utf-8");
System.out.println(context);7)文件重命名
boolean result = fs.rename(new Path("/test.txt"), new Path("/testnew.txt"));8)上传文件
fs.copyFromLocalFile(new Path("./data/hello.txt"), new Path("/hdfshello.txt"));9)下载文件
fs.copyToLocalFile(false, new Path("/hdfshello.txt"), new Path("./data/testdata.txt"), true);10)输出所有列表所有文件和文件夹信息
FileStatus[] statuses = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : statuses) {
System.out.println(fileStatus.toString());
}11)递归查询目录所有文件信息,比listStatus多了文本大小,副本系数,块大小信息
RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);
while (files.hasNext()) {
System.out.println(files.next());
}12)查询文件块信息
FileStatus fileStatus = fs.getFileStatus(new Path("/user/master01/data.txt"));
BlockLocation[] blocks = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
for (BlockLocation block : blocks) {
System.out.println(block);
}13)查询文件块信息并跳转读取
FileStatus fileStatus = fs.getFileStatus(new Path("/user/master01/data.txt"));
BlockLocation[] blocks = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
FSDataInputStream input = fs.open(new Path("/user/master01/data.txt"));
input.seek(blocks[1].getOffset());
//input.seek(0)是让指针回到开始
System.out.println(input.readLine());到此这篇关于hdfs集成springboot使用的文章就介绍到这了,更多相关hdfs集成springboot内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
在SpringBoot中实现多种方式登录(通过用户名、手机号、邮箱等)的详细指南
今天,我们将跳进 Spring Boot 的世界,探索如何通过 用户名、手机号、邮箱 等多种方式实现登录,而我们要做的就是为他们提供这些选择,确保他们都能毫无阻碍地进入我们的系统,感兴趣的小伙伴跟着小编一起来看看吧2024-11-11
Spring Boot启动加载流程自动配置的底层原理(专家解读)
Spring Boot这么少的配置却能实现如此丰富的功能?为何启动一个Spring Boot应用可以变得如此简单便捷?在这个看似平凡的启动过程中,其实隐藏着一套精妙的自动化机制和源码逻辑,本文将带你深入探索 Spring Boot 的启动流程,逐步揭开自动配置的幕后奥秘2026-01-01
Springboot整合ShardingJdbc实现分库分表方式
这篇文章主要介绍了Springboot整合ShardingJdbc实现分库分表方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2025-06-06
Java使用list集合remove需要注意的事项(使用示例)
List集合的一个特点是它其中的元素是有序的,也就是说元素的下标是根据插入的顺序来的,在删除头部或者中间的一个元素后,后面的元素下标会往前移动,本文给大家介绍Java使用list集合remove需要注意的事项,感兴趣的朋友一起看看吧2022-01-01


最新评论