hadoop學習筆記(七):Java HDFS API
阿新 • • 發佈:2018-04-26
on() apr name pin package 目錄 except 讀取 play
一、使用HDFS FileSystem詳解
HDFS依賴的第三方包:
hadoop 1.x版本:
commons-configuration-1.6.jar
commons-lang-2.4.jar
commons-loggin-1.1.1.jar
hadoop-core-1.2.1.jar
log4j-1.2.15.jar
hadoop2.x版本:
hadoop-mapreduce-client-core-2.2.0.jar
hadoop-common-2.2.0.jar
hadoop-mapreduce-client-common-2.2.0.jar
hadoop-mapreduce-client-jobclient-2.2.0.jar
註意:可根據自身的情況和版本信息手動添加或maven倉庫依賴。
文件操作
1 工具類的創建:
創建一個工具類HDFSUtils,主要用來加載公共信息:
1 package com.laowang.utils; 2 3 import org.apache.hadoop.conf.Configuration; 4 import org.apache.hadoop.fs.FileSystem; 5 import org.apache.hadoop.hdfs.DistributedFileSystem;HDFSUtils6 import java.io.IOException; 7 import java.net.URI; 8 9 /** 10 * @author laowang 11 * @version v1.0.0 12 * @apiNote HDFS 工具類 13 * @since 2018/4/26 10:36 14 */ 15 public class HDFSUtil { 16 /** 17 * @author laowang 18 * @version v1.0.0 19 * @apiNote 獲取FileSystem 20 * @since 2018/4/26 10:3921 */ 22 public static FileSystem getFileSystem() { 23 FileSystem hdfs = null; 24 //獲取配置文件信息 25 Configuration conf = new Configuration(); 26 conf.set("fs.hdfs.impl",DistributedFileSystem.class.getName()); 27 try { 28 //獲取文件系統 29 hdfs = FileSystem.get(URI.create("hdfs://master01:9000"), conf); 30 } catch (IOException e) { 31 e.printStackTrace(); 32 } 33 return hdfs; 34 } 35 36 /** 37 * 關閉文件流 38 * @param fileSystem 39 */ 40 public static boolean closeFileSystem(FileSystem fileSystem) { 41 boolean flag = false; 42 if (fileSystem != null) { 43 try { 44 fileSystem.close(); 45 flag = true; 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 } 50 return flag; 51 } 52 }
2 上傳文件到HDFS
1 /** 2 * @author laowang 3 * @version v1.0.0 4 * @apiNote 上傳文件 5 * @since 2018/4/26 14:14 6 */ 7 public static void putFile() throws Exception { 8 Path sourcePath = new Path("E:\\port.txt"); 9 //文件名稱 10 Path targetPath = new Path("/test/"); 11 //從本地復制文件到文件系統中去 12 hdfs.copyFromLocalFile(sourcePath,targetPath); 13 }上傳文件
3 讀取文件
1 /** 2 * @author laowang 3 * @version v1.0.0 4 * @apiNote 讀取文件 5 * @since 2018/4/26 10:39 6 */ 7 public static void testRead() throws Exception{ 8 FileSystem hdfs = HDFSUtil.getFileSystem(); 9 //文件名稱 10 Path path = new Path("/test/initNetwork.sh"); 11 //打開文件輸入流 12 FSDataInputStream inputStream = hdfs.open(path); 13 //讀取文件到控制臺 14 IOUtils.copyBytes(inputStream,System.out,4096,false); 15 //關閉流 16 IOUtils.closeStream(inputStream); 17 }讀取文件
1 /** 2 * @author laowang 3 * @version v1.0.0 4 * @apiNote 查看目錄 5 * @since 2018/4/26 14:29 6 */ 7 public static void testList() throws Exception{ 8 FileSystem fileSystem=HDFSUtil.getFileSystem(); 9 Path path = new Path("/test/"); 10 FileStatus[] fileStatuses = hdfs.listStatus(path); 11 for(FileStatus fileStatus : fileStatuses){ 12 Path p = fileStatus.getPath(); 13 String info = fileStatus.isDirectory() ? "目錄" : "文件"; 14 System.out.println(info + ": " + p); 15 } 16 }查看目錄
4 在hadoop fs中新建文件,並寫入
1 /** 2 * 創建文件 3 * @throws Exception 4 */ 5 public static void testDirectory() throws Exception { 6 FileSystem fileSystem = HDFSUtil.getFileSystem(); 7 Path path = new Path("/test/laowang"); 8 boolean mkdirs = hdfs.mkdirs(path); 9 System.out.println("創建:" + (mkdirs ? "成功" : "失敗")); 10 }創建文件
1 /** 2 * 創建文件並寫入內容 3 * @throws Exception 4 */ 5 public static void testCreate() throws Exception{ 6 Path path = new Path("/test/test.txt"); 7 //創建文件 8 FSDataOutputStream outputStream = hdfs.create(path); 9 //寫入內容 10 outputStream.writeUTF("hello hadoop!"); 11 IOUtils.closeStream(outputStream); 12 }創建文件並寫入內容
5 重命名文件
1 /** 2 * 重命名 3 */ 4 public static void testRename() throws Exception{ 5 //原路徑 6 Path srcPath = new Path("/test/test.txt"); 7 //目標路徑 8 Path targetPath = new Path("/test/test2.txt"); 9 boolean rename = hdfs.rename(srcPath, targetPath); 10 System.out.println("重命名:" + (rename ? "成功" : "失敗")); 11 }重命名
6 刪除hadoop fs上的文件
1 /** 2 * 刪除 3 */ 4 public static void testDelete() throws Exception { 5 Path path = new Path("/test/test2.txt"); 6 boolean b = hdfs.deleteOnExit(path); 7 System.out.println("刪除:" + (b ? "成功" : "失敗")); 8 }刪除
HDFS信息
1 查找某個文件在HDFS集群中的位置
1 /** 2 * @author laowang 3 * @version v1.0.0 4 * @apiNote 查找某個文件在HDFS中的位置 5 * @since 2018/4/26 15:31 6 */ 7 public static void testLocation() throws Exception{ 8 Path path = new Path("/test/port.txt"); 9 FileStatus fileStatus = hdfs.getFileStatus(path); 10 BlockLocation[] fileBlockLocations = hdfs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen()); 11 for(BlockLocation blockLocation : fileBlockLocations){ 12 String[] hosts=blockLocation.getHosts(); 13 for (String host : hosts) { 14 System.out.println("端口:" + host); 15 } 16 } 17 }查找某個文件在HDFS中的位置
2 獲取HDFS集群上所有節點名稱信息
1 /** 2 * @author laowang 3 * @version v1.0.0 4 * @apiNote 獲取HDFS集群上所有節點名稱信息 5 * @since 2018/4/26 15:38 6 */ 7 public static void testCluster() throws Exception { 8 DistributedFileSystem distributedFileSystem = (DistributedFileSystem) hdfs; 9 DatanodeInfo[] dataNodeStats = distributedFileSystem.getDataNodeStats(); 10 for (DatanodeInfo datanodeInfo : dataNodeStats) { 11 System.out.println(datanodeInfo.getHostName()); 12 } 13 }獲取HDFS集群上所有節點名稱信息
hadoop學習筆記(七):Java HDFS API