HDFS的客戶端操作


命令行操作:

-help             

功能:輸出這個命令參數手冊

-ls                  

功能:顯示目錄信息

示例: hadoop fs -ls hdfs://hadoop-server01:9000/

備注:這些參數中,所有的hdfs路徑都可以簡寫

-->hadoop fs -ls /   等同於上一條命令的效果

-mkdir              

功能:在hdfs上創建目錄

示例:hadoop fs  -mkdir  -p  /aaa/bbb/cc/dd

-moveFromLocal            

功能:從本地剪切粘貼到hdfs

示例:hadoop  fs  - moveFromLocal  /home/hadoop/a.txt  /aaa/bbb/cc/dd

-moveToLocal              

功能:從hdfs剪切粘貼到本地

示例:hadoop  fs  - moveToLocal   /aaa/bbb/cc/dd  /home/hadoop/a.txt 

--appendToFile  

功能:追加一個文件到已經存在的文件末尾

示例:hadoop  fs  -appendToFile  ./hello.txt  hdfs://hadoop-server01:9000/hello.txt

可以簡寫為:

Hadoop  fs  -appendToFile  ./hello.txt  /hello.txt

-cat  

功能:顯示文件內容  

示例:hadoop fs -cat  /hello.txt

-tail                 

功能:顯示一個文件的末尾

示例:hadoop  fs  -tail  /weblog/access_log.1

-text                  

功能:以字符形式打印一個文件的內容

示例:hadoop  fs  -text  /weblog/access_log.1

-chgrp

-chmod

-chown

功能:linux文件系統中的用法一樣,對文件所屬權限

示例:

hadoop  fs  -chmod  666  /hello.txt

hadoop  fs  -chown  someuser:somegrp   /hello.txt

-copyFromLocal    

功能:從本地文件系統中拷貝文件到hdfs路徑去

示例:hadoop  fs  -copyFromLocal  ./jdk.tar.gz  /aaa/

-copyToLocal      

功能:從hdfs拷貝到本地

示例:hadoop fs -copyToLocal /aaa/jdk.tar.gz

-cp              

功能:從hdfs的一個路徑拷貝hdfs的另一個路徑

示例: hadoop  fs  -cp  /aaa/jdk.tar.gz  /bbb/jdk.tar.gz.2

-mv                     

功能:在hdfs目錄中移動文件

示例: hadoop  fs  -mv  /aaa/jdk.tar.gz  /

-get              

功能:等同於copyToLocal,就是從hdfs下載文件到本地

示例:hadoop fs -get  /aaa/jdk.tar.gz

-         

功能:合並下載多個文件

示例:getmerge    如hdfs的目錄 /aaa/下有多個文件:log.1, log.2,log.3,...

hadoop fs -getmerge /aaa/log.* ./log.sum

-put                

功能:等同於copyFromLocal

示例:hadoop  fs  -put  /aaa/jdk.tar.gz  /bbb/jdk.tar.gz.2

-rm                

功能:刪除文件或文件夾

示例:hadoop fs -rm -r /aaa/bbb/

-rmdir                 

功能:刪除空目錄

示例:hadoop  fs  -rmdir   /aaa/bbb/ccc

-df               

功能:統計文件系統的可用空間信息

示例:hadoop  fs  -df  -h  /

-du

功能:統計文件夾的大小信息

示例:

hadoop  fs  -du  -s  -h /aaa/*

-count         

功能:統計一個指定目錄下的文件節點數量

示例:hadoop fs -count /aaa/

-setrep                

功能:設置hdfs中文件的副本數量

示例:hadoop fs -setrep 3 /aaa/jdk.tar.gz

補充:查看dfs集群工作狀態的命令

hdfs dfsadmin -report

Java API操作:

package com.study.hdfs;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.Map;

/**
 * @author wangxu
 * @date 2016/12/17
 */
public class HDFSTest {
    
    private static FileSystem fs;
    
    @BeforeClass
    public static void setup() throws URISyntaxException, IOException, InterruptedException {
        //方式1通過配置來獲取fs
        /*Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://node1:9000");
        FileSystem fs = FileSystem.get(conf);
        System.setProperty("HADOOP_USER_NAME", "root");*/
        //方式2直接獲取fs
        Configuration conf = new Configuration();
        conf.set("dfs.replication", "2");
        conf.set("dfs.block.size", "64m");
        fs = FileSystem.get(new URI("hdfs://node1:9000"), conf, "root");
    }
    
    
    @Test
    public void test01() throws IOException {
        fs.copyToLocalFile(new Path("/plans.txt"),new Path("C:/Users/wxisme/Desktop/bigdatatest/"));
    }
    
    @Test
    public void test02() throws IOException {
        FSDataOutputStream out = fs.create(new Path("/plans01.txt"));
        FileInputStream in = new FileInputStream("C:/Users/wxisme/Desktop/bigdatatest/plans.txt");
        
        IOUtils.copy(in, out);
    }
    
    @Test
    public void test03() throws IOException {
        DatanodeInfo[] dataNodeStats = ((DistributedFileSystem)fs).getDataNodeStats();
        for(DatanodeInfo dinfo: dataNodeStats){
            System.out.println(dinfo.getHostName());
        }
    }
    
    @Test
    public void test04() throws IOException {
//        fs.mkdirs(new Path("/wangxu/study"));
//        fs.rename(new Path("/wangxu/study"), new Path("/wangxu/play"));
        fs.delete(new Path("/wangxu/play"), true);
    }
    
    @Test
    public void test05() throws 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("--------------分割線--------------");
            
        }
    }
    
    @Test
    public void test06() throws 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());
            
        }
    }
    
    
    @Test
    public void test07() {
        Configuration conf = new Configuration();
        conf.addResource("test.xml");
        System.out.println(conf.get("xxx.uu"));

        Iterator<Map.Entry<String, String>> it = conf.iterator();

        while(it.hasNext()){

            System.out.println(it.next());

        }
    }
    
    @Test
    public void testDownLoadFileToLocal() throws IllegalArgumentException, IOException{
        
        //先獲取一個文件的輸入流----針對hdfs上的
        FSDataInputStream in = fs.open(new Path("/jdk-7u65-linux-i586.tar.gz"));
        
        //再構造一個文件的輸出流----針對本地的
        FileOutputStream out = new FileOutputStream(new File("c:/jdk.tar.gz"));
        
        //再將輸入流中數據傳輸到輸出流
        org.apache.hadoop.io.IOUtils.copyBytes(in, out, 4096);
        
        
    }
    
    @Test
    public void testUploadByStream() throws Exception{
        
        //hdfs文件的輸出流
        FSDataOutputStream fsout = fs.create(new Path("/aaa.txt"));
        
        //本地文件的輸入流
        FileInputStream fsin = new FileInputStream("c:/111.txt");
        
        org.apache.hadoop.io.IOUtils.copyBytes(fsin, fsout,4096);
        
        
    }
    
    
    
    
    /**
     * hdfs支持隨機定位進行文件讀取,而且可以方便地讀取指定長度
     * 用於上層分布式運算框架並發處理數據
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void testRandomAccess() throws IllegalArgumentException, IOException{
        //先獲取一個文件的輸入流----針對hdfs上的
        FSDataInputStream in = fs.open(new Path("/iloveyou.txt"));
        
        
        //可以將流的起始偏移量進行自定義
        in.seek(22);
        
        //再構造一個文件的輸出流----針對本地的
        FileOutputStream out = new FileOutputStream(new File("d:/iloveyou.line.2.txt"));
        
        org.apache.hadoop.io.IOUtils.copyBytes(in,out,19L,true);
        
    }
    
    
    
    /**
     * 讀取指定的block
     * @throws IOException
     * @throws IllegalArgumentException
     */
    @Test
    public void testCat() throws IllegalArgumentException, IOException{
        
        FSDataInputStream in = fs.open(new Path("/weblog/input/access.log.10"));
        //拿到文件信息
        FileStatus[] listStatus = fs.listStatus(new Path("/weblog/input/access.log.10"));
        //獲取這個文件的所有block的信息
        BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(listStatus[0], 0L, listStatus[0].getLen());
        
        
        //第一個block的長度
        long length = fileBlockLocations[0].getLength();
        //第一個block的起始偏移量
        long offset = fileBlockLocations[0].getOffset();
        
        System.out.println(length);
        System.out.println(offset);
        
        //獲取第一個block寫入輸出流
//        IOUtils.copyBytes(in, System.out, (int)length);
        byte[] b = new byte[4096];
        
        FileOutputStream os = new FileOutputStream(new File("d:/block0"));
        while(in.read(offset, b, 0, 4096)!=-1){
            os.write(b);
            offset += 4096;
            if(offset>length) return;
        };
        
        os.flush();
        os.close();
        in.close();
    }
    
    
    @AfterClass
    public static void shutdown() throws IOException {
        fs.close();
    }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM