分散式系統詳解--框架(Hadoop--JAVA操作HDFS檔案)
阿新 • • 發佈:2018-11-10
分散式系統詳解--框架(Hadoop--JAVA操作HDFS檔案)
前面的文章介紹了怎麼將整個集群系統搭建起來,並進行了有效的測試。為了解決登入一臺伺服器登入其他伺服器需要多次輸入密碼的問題,提供了SSH免密碼登入解決方案。還有一些hadoop的簡單操作shell命令。今天我們就結合eclipse來用JAVA語言來讀取和操作我們的hadoop檔案系統中的檔案。
一、POM檔案
1.1 讓我們來先看一下mvnrepository裡面關於hadoop中的JAR包
1.2 需要引進的JAR包。
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.7.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.7.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.7.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-core --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-core</artifactId> <version>2.7.5</version> </dependency> <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> <version>1.8</version> <scope>system</scope> <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> </dependency>
二、程式碼
2.1 前提我先在hdfs dfs 系統根目錄下建立一個test資料夾,將README.txt複製了進去。
2.2 檢視檔案
/** * */ package com.yuyi.hadoop; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; /** * @author mcb * * 2018年10月11日 下午5:49:28 */ public class ReadFileToConSoleTest { public static void main(String[] args) throws Exception { // 該路徑為你想獲取的檔案的路徑,就是想著讀取那一個檔案 String filePath = "/test/README.txt"; readFileToConSole(filePath); // readFileToLocal(filePath); localToHDFS(); } static FileSystem fs = null; static { Configuration conf =new Configuration(); try { fs = FileSystem.get(new URI("hdfs://192.168.71.233:8020"), conf, "root"); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } catch (URISyntaxException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } // 檔案讀取 public static void readFileToConSole(String filePath) throws Exception { // 獲取配置 Configuration conf = new Configuration(); // 配置 conf.set("fs.defaultFS", "hdfs://192.168.71.233:8020"); // 獲取hdfs檔案系統的操作物件 FileSystem fs = FileSystem.get(conf); // 具體對檔案操作 FSDataInputStream fin = fs.open(new Path(filePath)); // 輸入流讀取,讀出到何處,讀出大小和是否關閉 IOUtils.copyBytes(fin, System.out, 4096, true); } // 檔案下載到本地 public static void readFileToLocal(String filePath) throws Exception { FSDataInputStream fin = null; OutputStream out = null; try { // 獲取配置 Configuration conf = new Configuration(); // 配置 // conf.set("fs.defaultFS", "hdfs://192.168.71.233:8020"); // 獲取hdfs檔案系統的操作物件 FileSystem fs = FileSystem.get(new URI("hdfs://192.168.71.233:8020"), conf, "root"); // 具體對檔案操作 fin = fs.open(new Path(filePath)); out = new FileOutputStream(new File("F:\\hadoop\\testout\\a.txt")); // 輸入流讀取,讀出到何處,讀出大小和是否關閉 IOUtils.copyBytes(fin, out, 4096, true); } catch (Exception e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } finally { fin.close(); out.close(); } } // 檔案從本地上傳到hdfs系統中 public static void localToHDFS() throws IllegalArgumentException, IOException { String localPath="F:\\hadoop\\testout"; String hdfsPath="/test/b.txt"; fs.copyFromLocalFile(new Path(localPath), new Path(hdfsPath)); System.out.println("傳輸完成~~"); } }
歡迎訂閱公眾號(JAVA和人工智慧)
獲取更過免費書籍資源視訊資料
知識點超級連結: