學習筆記:從0開始學習大資料-5.hadoop hdfs檔案讀寫api操作
阿新 • • 發佈:2018-12-05
學習測試,網上下的程式碼,測試通過,助於理解讀寫程式流程
package com.linbin.testmaven; import java.net.URI; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.LocatedFileStatus; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.RemoteIterator; import org.apache.hadoop.fs.permission.FsPermission; import org.apache.hadoop.hdfs.DistributedFileSystem; /** * Hello world! * */ public class App { public static DistributedFileSystem dfs=new DistributedFileSystem();; public static String nameNodeUri="hdfs://centos7:8020"; public static void main( String[] args ) throws Exception { System.out.println( "Hello World! how are you?" ); App fot = new App(); fot.initFileSystem(); fot.testMkDir(); fot.testFileList(); } public void initFileSystem() throws Exception{ System.out.println("初始化hadoop客戶端"); //設定hadoop的登入使用者名稱 System.setProperty("HADOOP_USER_NAME", "root"); //dfs=new DistributedFileSystem(); dfs.initialize(new URI(nameNodeUri), new Configuration()); System.out.println("客戶端連線成功"); Path workingDirectory = dfs.getWorkingDirectory(); System.out.println("工作目錄:"+workingDirectory); } /** * 建立資料夾 * @throws Exception */ public void testMkDir() throws Exception{ boolean res = dfs.mkdirs(new Path("/tmp/bbb")); System.out.println("目錄建立結果:"+(res?"建立成功":"建立失敗")); } /** * 獲取指定目錄下所有檔案(忽略目錄) * @throws Exception * @throws IllegalArgumentException * @throws FileNotFoundException */ public void testFileList() throws Exception{ RemoteIterator<LocatedFileStatus> listFiles = dfs.listFiles(new Path("/"), true); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); while (listFiles.hasNext()) { LocatedFileStatus fileStatus = (LocatedFileStatus) listFiles.next(); FsPermission permission = fileStatus.getPermission(); String owner = fileStatus.getOwner(); String group = fileStatus.getGroup(); long len = fileStatus.getLen(); long modificationTime = fileStatus.getModificationTime(); Path path = fileStatus.getPath(); System.out.println("-------------------------------"); System.out.println("path:"+path); System.out.println("permission:"+permission); System.out.println("owner:"+owner); System.out.println("group:"+group); System.out.println("len:"+len); System.out.println("modificationTime:"+sdf.format(new Date(modificationTime))); } } }
測試結果,成功讀寫hadoop目錄資訊