当前位置: 代码网 > it编程>编程语言>Java > hdfs集成springboot使用方法

hdfs集成springboot使用方法

2024年05月18日 Java 我要评论
1.导入maven依赖<dependency> <groupid>org.apache.hadoop</groupid> <artifactid&

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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com