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