日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Hadoop 核心编程之 HDFS 的文件操作

發布時間:2025/3/20 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hadoop 核心编程之 HDFS 的文件操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

本文并沒有打算介紹 HDFS 的讀寫流程,雖然這是一塊比較重要的內容。如果你感興趣,可以去搜索相關資料。如果一遍沒有看懂,請看第二遍。
本文還是以代碼為主,并附以簡短的說明,幫助你理解代碼的邏輯,以及一些注意事項。你可以將本文的代碼封裝成一個工具類,這樣以后需要調用時候,就可以復用了。


版權說明

著作權歸作者所有。
商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
本文作者:Q-WHai
發表日期: 2016年6月21日
本文鏈接:https://qwhai.blog.csdn.net/article/details/51728359
來源:CSDN
更多內容:分類 >> 大數據之 Hadoop


HDFS 讀寫 API

上傳本地文件到 HDFS

public static void uploadFileFromLocal(String localPath, String hdfsPath) throws IOException {InputStream in = new BufferedInputStream(new FileInputStream(localPath));FileSystem fileSystem = FileSystem.get(URI.create(hdfsPath), new Configuration());OutputStream out = fileSystem.create(new Path(hdfsPath));IOUtils.copyBytes(in, out, 4096, true);fileSystem.close(); }

此處使用了一個十分方便的方法 IOUtils.copyBytes()。調用這個文件,你可以很方便地將輸入流寫入到輸出流,而且不需要你人為去控制緩沖區,也不需要人為控制循環讀取輸入源。IOUtils.copyBytes() 中的第 4 個參數表示是否關閉流對象,也就是輸入輸出流對象。一般來說打成 true 就好了。


從 HDFS 下載文件到本地

通過上面上傳文件的例子,我們可以很容易地寫出下載文件的代碼。如下:

public static void downloadFileToLocal (String hdfsPath, String localPath) throws IOException {FileSystem fileSystem = FileSystem.get(URI.create(hdfsPath), new Configuration());FSDataInputStream in = fileSystem.open(new Path(hdfsPath));OutputStream out = new FileOutputStream(localPath);IOUtils.copyBytes(in, out, 4096, true);fileSystem.close(); }

從 HDFS 下載文件到本地

上面是的下載文件已經很好用了,現在再來看看另外一種下載文件的方法。調用的是 FileUtil.copy() 方法。

public static void downloadFileToLocalNew (String hdfsSourceFileFullName, String localFileFullName) throws IOException {Configuration config = new Configuration();FileSystem fileSystem = FileSystem.get(URI.create(hdfsSourceFileFullName), config);FileUtil.copy(fileSystem, new Path(hdfsSourceFileFullName), new File(localFileFullName), false, config);fileSystem.close(); }

按行讀取 HDFS 文件內容

在 HDFS 里面應該是沒有直接提供按行讀取文件的 API(如果有,后面我們再更新),但是 JDK 中提供相關的 API,那就是 BufferedReader。這里你可以結合剛學 Java 時使用的用戶在控制臺向程序輸入,當時除了 Scanner 就是 BufferedReader 了,很方便。

public static List<String> readFileHDFSByLine (Configuration config, String hdfsFileFullName) throws IOException {List<String> result = new ArrayList<>();FileSystem fileSystem = FileSystem.get(URI.create(hdfsFileFullName), config);FSDataInputStream dataInputStream = fileSystem.open(new Path(hdfsFileFullName)); BufferedReader reader = null;String line;try {reader = new BufferedReader(new InputStreamReader(dataInputStream, "UTF-8"));while ((line = reader.readLine()) != null) {result.add(line);}} finally {if (reader != null) {reader.close();}}return result; }

向 HDFS 中的文件追加內容

public static void appendLabelToHDFS(String hdfsPath, String content) throws IOException {Configuration config = new Configuration();config.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");config.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true");FileSystem fileSystem = FileSystem.get(URI.create(hdfsPath), config);FSDataOutputStream out = fileSystem.append(new Path(hdfsPath));int readLen = content.getBytes().length;if (-1 != readLen) {out.write(content.getBytes(), 0, readLen);}out.close();fileSystem.close(); }

此處,如果你不想動態設置 Configuration,那么你就需要在配置文件中配置此兩項內容。
補充說明
如果你需要對文件進行追加內容操作,那么在 hdfs-site.xml 配置文件中需要設置如下屬性。

<property><name>dfs.support.append</name><value>true</value> </property>

向 HDFS 中的文件追加文件

通過上面追加字符串的操作,你可能會想到這里可以先讀取文件內容到字符串,再進行追加字符串操作。這樣的確是可以的。不過可以看到上的輸出是一個輸出流,那么這里就不需要再讀取到字符串了。文件是可以直接對到文件流上的嘛。所以向 HDFS 文件中追加文件的操作如下:

public static void appendFileToHDFS(String hdfsPath, String localFilePath) throws IOException {Configuration config = new Configuration();config.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");config.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true");FileSystem fileSystem = FileSystem.get(URI.create(hdfsPath), config);InputStream in = new BufferedInputStream(new FileInputStream(localFilePath));FSDataOutputStream out = fileSystem.append(new Path(hdfsPath));IOUtils.copyBytes(in, out, 4096, true);fileSystem.close(); }

向 HDFS 文件中寫入內容

此處對 HDFS 文件的更改是覆蓋式的,也就是會把之前的內容全部刪除。

public static void writeLabelToHDFS(String hdfsPath, String content) throws IOException {FileSystem fileSystem = FileSystem.get(URI.create(hdfsPath), new Configuration());FSDataOutputStream out = fileSystem.create(new Path(hdfsPath));int readLen = content.getBytes().length;if (-1 != readLen) {out.write(content.getBytes(), 0, readLen);}out.close();fileSystem.close(); }

刪除 HDFS 中文件

此刪除操作是刪除一個已存在的文件,從代碼中的方法命名就可以看出來。不過,如果 HDFS 中不存在此文件,也不會拋出異常。

public static void deleteFileFromHDFS(String hdfsPath) throws IOException {FileSystem fileSystem = FileSystem.get(URI.create(hdfsPath), new Configuration());fileSystem.deleteOnExit(new Path(hdfsPath));fileSystem.close(); }

讀取 HDFS 某一目錄下的所有文件

這里只是讀取目錄下的文件,并不包含目錄。

public static void readFilesOnlyInDirectoryFromHDFS(String hdfsFolderName) throws IOException {FileSystem fileSystem = FileSystem.get(URI.create(hdfsFolderName), new Configuration());FileStatus fileList[] = fileSystem.listStatus(new Path(hdfsFolderName));for (FileStatus fileStatus : fileList) {if (fileStatus.isDirectory()) {continue;}System.out.println("FileName: " + fileStatus.getPath().getName() + "\t\tSize: " + fileStatus.getLen());}fileSystem.close(); }

讀取 HDFS 某一目錄下的所有文件

此方法是參考上面的 readFilesOnlyInDirectoryFromHDFS() 方法來的,只是這里也會去讀取子目錄下的所有文件。所以使用了一個遞歸,并且為了更好地封裝,這里將遞歸的邏輯與調用分開了,這樣做的目的是避免產生過多的 Configuration 對象。

public static void listHDFSFiles (String hdfsFileFullName) throws IOException {Configuration config = new Configuration();listHDFSFiles(config, hdfsFileFullName); }private static void listHDFSFiles (Configuration config, String hdfsFileFullName) throws IOException {FileSystem fileSystem = FileSystem.get(URI.create(hdfsFileFullName), config);FileStatus[] fileStatus = fileSystem.listStatus(new Path(hdfsFileFullName));for (FileStatus statusItem : fileStatus) {if (statusItem.isDirectory()) {listHDFSFiles(config, statusItem.getPath().toString());}System.out.println("FileName: " + statusItem.getPath() + "\t\tSize: " + statusItem.getLen());}fileSystem.close(); }

獲取某一文件在 HDFS 中實際保存的節點

此方法可以展示 HDFS 中的某一個文件在 HDFS 文件系統中被保存的所有 DataNode。

public static void getFileLocal(String hdfsFileFullName) throws IOException {FileSystem fileSystem = FileSystem.get(URI.create(hdfsFileFullName), new Configuration());FileStatus status = fileSystem.getFileStatus(new Path(hdfsFileFullName));BlockLocation[] locations = fileSystem.getFileBlockLocations(status, 0, status.getLen());for (int i = 0; i < locations.length; i++) {String[] hosts = locations[i].getHosts();for (String host : hosts) {System.out.println("block_" + i + "_location:" + host);}} }

獲得 HDFS 中所有的節點信息

如果你不知道 HDFS 文件系統中有哪些文件,單純的想知道我的 HDFS 文件系統中有哪些 DataNode。那么可以把上面的 hdfsFileFullName 寫成 HDFS 的根目錄就可以了。比如我的設置如下:

public static void getHDFSNode() throws IOException {FileSystem fileSystem = FileSystem.get(URI.create("hdfs://master:9000/"), new Configuration());DistributedFileSystem distributedFileSystem = (DistributedFileSystem) fileSystem;DatanodeInfo[] dataNodeStats = distributedFileSystem.getDataNodeStats();for (int i = 0; i < dataNodeStats.length; i++) {System.out.println("DataNode_" + i + "_Node:" + dataNodeStats[i].getHostName());} }

總結

以上是生活随笔為你收集整理的Hadoop 核心编程之 HDFS 的文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。

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