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(“配置”,“值”)來修改配置項
//創(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設置為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)創(chuàng)建文件夾
fs.mkdirs(new Path("/dir1"));
3)創(chuàng)建文件夾并設置權限為文件所有者可讀可寫,文件所有組可讀可寫,其他人可讀
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)建文件并輸入文本
如果文件存在,默認會覆蓋, 可以通過第二個參數進行控制。第三個參數可以控制使用緩沖區(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多了文本大小,副本系數,塊大小信息
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內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Cloud Alibaba使用Sentinel實現接口限流
這篇文章主要介紹了Spring Cloud Alibaba使用Sentinel實現接口限流,本文詳細的介紹了Sentinel組件的用法以及接口限流,感興趣的可以了解一下2019-04-04全面解析JPA?倉庫repository中的findAll()方法
這篇文章主要介紹了全面解析JPA?倉庫repository中的findAll()方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02基于SpringBoot和Vue3的博客平臺發(fā)布、編輯、刪除文章功能實現
在上一個教程中,我們已經實現了基于Spring?Boot和Vue3的用戶注冊與登錄功能。本教程將繼續(xù)引導您實現博客平臺的發(fā)布、編輯、刪除文章功能,需要的朋友參考一下2023-04-04如何解決java:找不到符號符號:類__(使用了lombok的注解)
在使用IntelliJ IDEA開發(fā)Java項目時,可能遇到通過@lombok注解自動生成get和set方法不生效的問題,解決這一問題需要幾個步驟,首先,確認Lombok插件已在IDEA中安裝并啟用,其次,確保項目中已添加Lombok的依賴,對于Maven和Gradle項目2024-10-10