Hadoop----java API檔案
阿新 • • 發佈:2018-12-11
java API操作HDFS檔案
package com.immoc.hadoop.hdfs; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.util.Progressable; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; import java.net.URI; /** * Hadoop HDFS java API操作 */ public class HDFSApp { FileSystem fileSystem=null;//檔案系統類 Configuration configuration=null;//配置類 public static final String HDFS_PATH="hdfs://hadoop000:8020"; /** * 準備環境 * 單元測試之前 */ @Before public void setUp() throws Exception { configuration=new Configuration(); fileSystem=FileSystem.get(new URI(HDFS_PATH), configuration,"hadoop"); System.out.println("HDFSApp.setUp"); } /** * 建立HDFS目錄 */ @Test public void mkdir() throws Exception { //建立一個新的目錄,引數為路徑 fileSystem.mkdirs(new Path("/hdfsapi/test")); } /** * 建立檔案 */ @Test public void create()throws Exception{ FSDataOutputStream output=fileSystem.create(new Path("/hdfsapi/test/a.txt/"));//資料夾路徑 output.write("hello hadoop".getBytes());//將內容輸出到資料夾中 output.flush();//重新整理 output.close();//關閉 } /** * 開啟檔案 */ @Test public void cat() throws Exception { FSDataInputStream in=fileSystem.open(new Path("/hdfsapi/test/a.txt"));//資料夾路徑 IOUtils.copyBytes(in,System.out,1024);//輸出到控制檯 in.close();//關閉 } /** * 重新命名檔案 */ @Test public void rename()throws Exception{ Path oldPath=new Path("/hdfsapi/test/a.txt"); Path newPath=new Path("/hdfsapi/test/b.txt"); fileSystem.rename(oldPath,newPath ); } /** * 上傳到HDFS */ @Test public void copyFromLocalFile()throws Exception{ Path localPath=new Path("C:\\Learning\\bigData\\10 hours to get started with big data\\hello.txt"); Path hdfsPath=new Path("/hdfsapi/test/"); fileSystem.copyFromLocalFile(localPath,hdfsPath); } /** * 上傳大檔案到HDFS */ @Test public void copyFromLocalFileWithProgress()throws Exception{ Path localPath=new Path("C:\\Learning\\java\\idea\\jdk-8u181-windows-x64-demos.zip");//大資料夾路徑 Path hdfsPath=new Path("/hdfsapi/test/"); fileSystem.copyFromLocalFile(localPath,hdfsPath); //大資料夾通過流來處理 InputStream in=new BufferedInputStream(new FileInputStream(String.valueOf(localPath)));//本地資料夾輸入流 FSDataOutputStream output=fileSystem.create(new Path("/hdfsapi/test/jdk"), new Progressable() {//輸出流 @Override public void progress() { System.out.println(".");//帶進度條提醒資訊 } }); IOUtils.copyBytes(in,output,4096);//輸出到資料夾中 } /** * 下載HDFS檔案 */ @Test public void copyToLocalFile() throws Exception{ Path localPath=new Path("C:\\Learning\\bigData\\b.txt"); Path hdfsPath=new Path("/hdfsapi/test/b.txt"); fileSystem.copyToLocalFile(false,hdfsPath,localPath,true); } /** * 檢視某個目錄下的所有檔案 */ @Test public void listFiles() throws Exception{ FileStatus []fileStatuses=fileSystem.listStatus(new Path("/hdfsapi/test/")); for(FileStatus fileStatus:fileStatuses){ String isDir=fileStatus.isDirectory() ? "資料夾" : "檔案";//判斷是檔案還是資料夾 short replication=fileStatus.getReplication();//檢視副本數量 long len=fileStatus.getLen();//檢視檔案大小 String path=fileStatus.getPath().toString();//檢視檔案路徑 System.out.println(isDir+"\t"+replication+"\t"+len+"\t"+path); } } /** * 刪除檔案 */ @Test public void delete() throws Exception{ fileSystem.delete(new Path("/hdfsapi/test/"),true); } /** * 清理資原始檔和資原始檔的關閉操作 * 釋放資源 * 單元測試之後 */ @After public void tearDown() { configuration = null; fileSystem = null; System.out.println("HDFSApp.tearDown"); } }
通過Java API
上傳:檔案 3
0 hdfs://hadoop000:8020/hdfsapi/test
通過hdfs shell
上傳:檔案 1
40762 hdfs://hadoop000:8020/install.log
我們已經在hdfs-site.xml中設定了副本系數為1,但是當使用Java API
上傳,則副本系數為3,因為在本地並沒有手工設定副本系數,所以採用的是hadoop預設的副本系數3