1. 程式人生 > >大資料學習——hdfs客戶端操作

大資料學習——hdfs客戶端操作

package cn.itcast.hdfs;


import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI; public class HdfsClient { FileSystem fs = null; @Before public void init() throws Exception { // 構造一個配置引數物件,設定一個引數:我們要訪問的hdfs的URI // 從而FileSystem.get()方法就知道應該是去構造一個訪問hdfs檔案系統的客戶端,以及hdfs的訪問地址 // new Configuration();的時候,它就會去載入jar包中的hdfs-default.xml
// 然後再載入classpath下的hdfs-site.xml // conf.set("fs.defaultFS", "hdfs://mini1:9000"); /** * 引數優先順序: 1、客戶端程式碼中設定的值 2、classpath下的使用者自定義配置檔案 3、然後是伺服器的預設配置 */ /*conf.set("dfs.replication", "2"); conf.set("dfs.block.size", "64m");*/ // 獲取一個hdfs的訪問客戶端,根據引數,這個例項應該是DistributedFileSystem的例項
// fs = FileSystem.get(conf); // 如果這樣去獲取,那conf裡面就可以不要配"fs.defaultFS"引數,而且,這個客戶端的身份標識已經是root使用者 Configuration conf = new Configuration(); fs = FileSystem.get(new URI("hdfs://mini1:9000"), conf, "root"); } /** * 往hdfs上傳檔案 */ @Test public void testAddFileToHdfs() throws Exception { //要上傳的檔案所在的本地路徑 //要上傳到hdfs的目標路徑*/ Path src = new Path("e:/hello1.txt"); Path dst = new Path("/"); fs.copyFromLocalFile(src, dst); fs.close(); } /** * 從hdfs中複製檔案到本地檔案系統 * * @throws IOException * @throws IllegalArgumentException */ @Test public void testDownloadFileToLocal() throws IllegalArgumentException, IOException { fs.copyToLocalFile(false, new Path("/hello1.txt"), new Path("e:/"), true); fs.close(); } /** * 通過流的形式從hdfs下載資料 * @throws Exception */ @SuppressWarnings("resource") @Test public void testDownloadFileToLocal2() throws Exception { FSDataInputStream in = fs.open(new Path("/hello1.txt")); FileOutputStream out = new FileOutputStream(new File("e:/1.txt")); IOUtils.copy(in, out); fs.close(); } /** * 目錄操作 * * @throws IllegalArgumentException * @throws IOException */ @Test public void testMkdirAndDeleteAndRename() throws IllegalArgumentException, IOException { // 建立目錄 // fs.mkdirs(new Path("/nihao/henhao/yeah")); // // 刪除資料夾 ,如果是非空資料夾,引數2必須給值true // fs.delete(new Path("/nihao/henhao"), true); // // // 重新命名檔案或資料夾 fs.rename(new Path("/nihao"), new Path("/ni")); } /** * 檢視目錄資訊,只顯示檔案 * * @throws IOException * @throws IllegalArgumentException * @throws FileNotFoundException */ @Test public void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException { // 思考:為什麼返回迭代器,而不是List之類的容器 RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true); while (listFiles.hasNext()) { LocatedFileStatus fileStatus = listFiles.next(); System.out.println(fileStatus.getPath().getName()); System.out.println(fileStatus.getBlockSize()); System.out.println(fileStatus.getPermission()); System.out.println(fileStatus.getLen()); BlockLocation[] blockLocations = fileStatus.getBlockLocations(); for (BlockLocation bl : blockLocations) { System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset()); String[] hosts = bl.getHosts(); for (String host : hosts) { System.out.println(host); } } System.out.println("--------------為allen列印的分割線--------------"); } } /** * 檢視檔案及資料夾資訊 * * @throws IOException * @throws IllegalArgumentException * @throws FileNotFoundException */ @Test public void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException { FileStatus[] listStatus = fs.listStatus(new Path("/")); String flag = ""; for (FileStatus fstatus : listStatus) { if (fstatus.isFile()) { flag = "f-- "; } else { flag = "d-- "; } System.out.println(flag + fstatus.getPath().getName()); System.out.println(fstatus.getPermission()); } } }