亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

hdfs集成springboot使用方法

 更新時(shí)間:2024年03月30日 10:17:25   作者:鴻12321  
這篇文章主要介紹了hdfs集成springboot使用,配置Configuration信息分為兩種方式,每種方式給大家介紹的非常詳細(xì),需要的朋友可以參考下

1.導(dǎo)入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的兩個(gè)配置文件(hdfs-site.xml、core-site.xml)放到resources文件夾下后,新建Configuration的時(shí)候設(shè)置為true會(huì)自動(dòng)讀取,也可以通過conf.set(“配置”,“值”)來修改配置項(xiàng)

//創(chuàng)建配置,是否引用core-site.xml和hdfs-site.xml配置文件,true是引用
Configuration conf = new Configuration(true);
//創(chuàng)建文件連接流,指定namenode、conf和連接的用戶名
FileSystem fs = FileSystem.get(new URI("mycluster"),conf,"hadoop");

2)方法2:將Configuration設(shè)置為false,不加載默認(rèn)配置文件,直接指定namenode對(duì)應(yīng)的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)創(chuàng)建文件夾

fs.mkdirs(new Path("/dir1"));

3)創(chuàng)建文件夾并設(shè)置權(quán)限為文件所有者可讀可寫,文件所有組可讀可寫,其他人可讀

fs.mkdirs(new Path("/dir2"),new FsPermission(FsAction.READ_WRITE,FsAction.READ_WRITE,FsAction.READ));

4)刪除文件夾

fs.delete(new Path("/dir1"),true);

5)創(chuàng)建文件并輸入文本
如果文件存在,默認(rèn)會(huì)覆蓋, 可以通過第二個(gè)參數(shù)進(jìn)行控制。第三個(gè)參數(shù)可以控制使用緩沖區(qū)的大小

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多了文本大小,副本系數(shù),塊大小信息

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)查詢文件塊信息并跳轉(zhuǎn)讀取

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());

到此這篇關(guān)于hdfs集成springboot使用的文章就介紹到這了,更多相關(guān)hdfs集成springboot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論