日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDFS常用的Api

發布時間:2024/2/28 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDFS常用的Api 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

HDFS常用的Api:

package com.jxd.hdfs;import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.junit.After; import org.junit.Before; import org.junit.Test;import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays;public class HdfsClient {private FileSystem fs;@Beforepublic void init() throws URISyntaxException, IOException, InterruptedException {URI uri = new URI("hdfs://hadoop102:8020");Configuration configuration = new Configuration();String user = "jxd";fs = FileSystem.get(uri, configuration, user);}@Afterpublic void close() throws IOException {fs.close();}/*** 創建目錄** @throws IOException*/@Testpublic void testMkdir() throws IOException {fs.mkdirs(new Path("/xiyou/huaguoshan"));}/*** 上傳文件* hdfs-default.xml < hdfs-site.xml < 在resources目錄下創建的配置文件 < configuration對象設置** @throws IOException*/@Testpublic void testPut() throws IOException {// 是否刪除原數據 是否覆蓋 原數據路徑 目的路徑fs.copyFromLocalFile(false, false, new Path("G:\\input\\hello.txt"), new Path("hdfs://hadoop102/xiyou/huaguoshan"));}/*** 下載文件** @throws IOException*/@Testpublic void testGet() throws IOException {// 下載后是否刪除hdfs中的文件 原文件的路徑 目的地址 是否開啟本地校驗fs.copyToLocalFile(false, new Path("hdfs://hadoop102/xiyou/huaguoshan"), new Path("G:\\"), false);}/*** 刪除文件** @throws IOException*/@Testpublic void testRm() throws IOException {// 目標路徑 是否遞歸刪除fs.delete(new Path("hdfs://hadoop102/sanguo"), true);}/*** 重命名文件和移動文件*/@Testpublic void renameAndMove() throws IOException {// 原文件路徑 目標文件路徑fs.rename(new Path("hdfs://hadoop102/input/word.txt"), new Path("hdfs://hadoop102/input/word1.txt"));}/*** 獲取文件或文件夾的詳細信息** @throws IOException*/@Testpublic void testLookFileDetail() throws IOException {// 獲取所有的目標文件RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("hdfs://hadoop102/"), true);// 遍歷文件while (files.hasNext()) {LocatedFileStatus fileStatus = files.next();// 獲取文件的詳細信息System.out.println("Path: " + fileStatus.getPath());System.out.println("Permission: " + fileStatus.getPermission());System.out.println("Owner: " + fileStatus.getOwner());System.out.println("Group: " + fileStatus.getGroup());System.out.println("FileSize: " + fileStatus.getLen());System.out.println("ModificationTime: " + fileStatus.getModificationTime());System.out.println("Replication: " + fileStatus.getReplication());System.out.println("BlockSize: " + fileStatus.getBlockSize());System.out.println("Name: " + fileStatus.getPath().getName());// 獲取塊信息BlockLocation[] blockLocations = fileStatus.getBlockLocations();System.out.println("BlockLocation: " + Arrays.toString(blockLocations));}}/*** 判斷是文件還是目錄* @throws IOException*/@Testpublic void TestIsFileOrDir() throws IOException {FileStatus[] listStatus = fs.listStatus(new Path("hdfs://hadoop102/"));for (FileStatus status : listStatus) {if(status.isFile()){System.out.println("文件:" + status.getPath().getName());}else{System.out.println("目錄:" + status.getPath().getName());}}} }

總結

以上是生活随笔為你收集整理的HDFS常用的Api的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。