模塊一:Hadoop核心框架(七)---用IDEA創建一個對hadoopAPI操作demo

上一篇已經對api做了一個簡單的操作,這篇進階操作一下。
上一篇地址:模塊一:Hadoop核心框架(六)—用IDEA創建一個對hadoopAPI簡單操作項目

創建hdfsClient2類

爲了方便測試使用,我們將Configuration和FileSystem定義了在外面。
用@before和@after來初始化對象和關閉流。

    private Configuration configuration;
    private FileSystem fileSystem;
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        configuration=new Configuration();
        fileSystem=FileSystem.get(new URI("hdfs://linux121:9000"),configuration,"root");
    }
    @After
    public void after() throws IOException {
        fileSystem.close();
    }

測試上傳文件

首先hdfs沒有文件
在這裏插入圖片描述
我們在resource下創建一個a.txt文件,裏面寫着hello hdfs。把a.txt文件上傳上去,命名爲log4j.properties。
在這裏插入圖片描述
代碼:

    @Test
    /**
    * @author:yehw
    * @date: 2020/7/4:17:41
    * @paramType:[]
    * @returnType: void
    * @description: 測試本地文件複製到服務器
    */
    public void  testCopyFromLocalFile() throws IOException {
        //本地文件路徑
        Path localPath = new Path("/Users/yehuaiwei/IdeaProjects/client_demo/src/main/resources/log4j.properties");
        //上傳到服務起的路徑
        Path serverPath = new Path("/log4j.properties");
        //上傳
        fileSystem.copyFromLocalFile(true,localPath,serverPath);
        //上一個方法,默認是自動關閉流,所以可以不需要手動關閉
    }

啓動測試去網頁查看是否上傳成功:
在這裏插入圖片描述
可以下載看看,裏面內容就hello dfs
注意:這個方法是會把原文件刪除

測試下載文件

@Test
/**
* @author:yehw
* @date: 2020/7/4:22:02
* @paramType:[]
* @returnType: void
* @description:  測試從服務在下載文件
*/
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{
    //本地文件路徑
    Path localPath = new Path("/Users/yehuaiwei/IdeaProjects/client_demo/src/main/resources/a.txt");
    //要下載的服務器的路徑
    Path serverPath = new Path("/log4j.properties");
    //下載
    //第一個參數:false;代表下載完後不刪除源文件
    //第二份參數:服務器要下載的路徑
    //第三個參數:下載到本地的路徑
    fileSystem.copyToLocalFile(false,serverPath,localPath);
}

刪除文件

@Test
/**
* @author:yehw
* @date: 2020/7/4:22:07
* @paramType:
* @returnType:
* @description:  刪除文件
*/
public void testDelete() throws IOException {
    //服務器上要刪除的文件
    Path serverPath = new Path("/log4j.properties");
    fileSystem.delete(serverPath,true);
}

去服務器查看,文件是否刪除
在這裏插入圖片描述

測試文件信息查看

 @Test
    /**
    * @author:yehw
    * @date: 2020/7/4:22:21
    * @paramType:[]
    * @returnType: void
    * @description:  查看文件夾信息
    */
    public void testListFiles() throws IOException, InterruptedException, URISyntaxException {

        // 2 獲取文件詳情
        RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true);
        while (listFiles.hasNext()) {
            LocatedFileStatus status = listFiles.next();
                // 輸出詳情
                // 文件名稱
               System.out.println(status.getPath().getName());
                // 長度
                System.out.println(status.getLen());
                // 權限
               System.out.println(status.getPermission());
                // 分組
                System.out.println(status.getGroup());
                // 獲取存儲的塊信息
            BlockLocation[] blockLocations = status.getBlockLocations();
            for (BlockLocation blockLocation : blockLocations) {
                // 獲取塊存儲的主機節點
                String[] hosts = blockLocation.getHosts();
                for (String host : hosts) {
                    System.out.println(host);
                }
            }
            System.out.println("-----------華麗的分割線----------");
        }
        // 3 關閉資源
        fileSystem.close();

    }

控制檯輸出:

a.txt
10
rw-r–r--
supergroup
linux123
linux122
linux121
-----------華麗的分割線----------
log4j.properties
442
rw-r–r--
supergroup
linux123
linux122
linux121
-----------華麗的分割線----------

測試輸入輸出流

本地文件上傳的到服務器

@Test
/**
* @author:yehw
* @date: 2020/7/4:22:34
* @paramType:
* @returnType:
* @description:  測試輸入輸出流
*/
public void putFileToHdfs() throws IOException {
    //把a.txt文件通過輸入流然後輸出到服務器根目錄下的a_io.txt文件
    FileInputStream inputStream=new FileInputStream(new File("/Users/yehuaiwei/IdeaProjects/client_demo/src/main/resources/a.txt"));
    FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/a_io.txt"));
    IOUtils.copyBytes(inputStream,fsDataOutputStream,configuration);
    IOUtils.closeStream(inputStream);
    IOUtils.closeStream(fsDataOutputStream);
}

服務器查看:
在這裏插入圖片描述

服務器文件下載到本地

本地另去名字是b.txt

 @Test
/**
* @author:yehw
* @date: 2020/7/4:22:45
* @paramType:
* @returnType:
* @description:  從服務器通過輸入流輸入到本地
*/
public void getFiLeFromHdfs() throws IOException {
    FSDataInputStream open = fileSystem.open(new Path("/a_io.txt"));
    FileOutputStream fileOutputStream = new FileOutputStream(new File("/Users/yehuaiwei/IdeaProjects/client_demo/src/main/resources/b.txt"));
    IOUtils.copyBytes(open,fileOutputStream,configuration);
    IOUtils.closeStream(open);
    IOUtils.closeStream(fileOutputStream);
}

查看文件目錄:
在這裏插入圖片描述

seek定位讀取

   @Test
    /**
    * @author:yehw
    * @date: 2020/7/4:22:52
    * @paramType:
    * @returnType:
    * @description:  seek測試
    */
    public void seekTest() throws IOException {
        FSDataInputStream open = fileSystem.open(new Path("/a_io.txt"));
        IOUtils.copyBytes(open, System.out,  4096,false);
        //從剛開始再讀一遍,這個時候主要要把自動關閉取消
        open.seek(0);
        IOUtils.copyBytes(open, System.out, 4096, false);
        IOUtils.closeStream(open);
    }

查看控制檯
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章