1. 程式人生 > >FS Shell命令與JAVA實現操作HDFS檔案

FS Shell命令與JAVA實現操作HDFS檔案

HDFS Shell命令

建立目錄

shell>hadoop fs –mkdir /user

列表

shell>hadoop fs –ls /

檢視檔案內容

shell>hadoop fs –cat /input/file.txt

上傳檔案

shell>hadoop fs –put 本地檔案 遠端目錄

獲取檔案

shell> hadoop fs –get 遠端檔案 本地目錄

移動檔案

shell>hadoop fs –mv遠端檔案1 … 遠端檔案n 遠端目錄

複製檔案

shell>hadoop fs –cp 遠端檔案1 … 遠端檔案n 遠端目錄

刪除檔案

shell>hadoop fs –rmr 目錄/檔案

測試檔案命令
檔案是否存在

shell>hadoop fs –test –e 檔案   // 存在返回0

檔案是否0位元組

shell>hadoop fs –test –z 檔案   //是0返回0

檔案是否為目錄

shell>hadoop fs –test –d 檔案 //是目錄返回1,否則返回0  

顯示目錄中所有檔案大小

shell>hadoop fs –du 目錄/檔案  //目錄統計總大小,檔案統計大小

清空回收站

shell>hadoop fs -expunge 

JAVA操作HDFS的API

hadoop檔案系統API說明與程式設計步驟

Hadoop提供的檔案系統的API

1、 Configuration 類(org.apache.hadoop.conf包):封裝了一個客戶端或伺服器的配置檔案

Configuration():

預設載入core-default.xml,該配置檔案設定從本地檔案讀取。
預設也會載入自定義的core-site.xml,設定配置檔案從hdfs中讀取,但是此檔案需要放到 eclipse的 src目錄,系統也會自動讀取。最好也放log4j.properties 

注:如果其它的配置檔案如hdfs-site.xml,mapred-site.xml需要手動載入讀取,記載方法是conf.addResource(“hdfs-site.xml”)格式。如果單獨改變引數,conf.set(引數名,值)

2、 IOUtils類(org.apache.hadoop.io包):Hadoop提供的IO工具類

copyBytes(InputStream 輸入流, OutputStream 輸出流, int 快取大小, boolean  是否關閉輸入輸出流):從輸入流中讀取資料到輸出流,關閉輸入輸出流也可以使用IOUtils工具提供的closeStream(in)方法來關閉。

3、Path類(org.apache.hadoop.fs):一個檔案被視為Path物件,該物件由檔案的URI例項化。

Path(String uri):構建一個path物件

4、FileSystem類(org.apache.hadoop.fs包):代表一個檔案系統,可以對檔案進行操作。

static FileSystem get(Configuration conf):建立一個帶有配置物件conf的FileSystem物件

static FileSystem get(URI uri, Configuration conf):建立帶有URI和Conf的FileSystem物件 

注:如果在uri中指定hdfs的字首,那麼就不需要在eclipse中載入core-site.xml檔案了。

例:

FileSystem.get(URI.create(“hdfs://hfbin1:9000”),conf)
InputStream open(path path):開啟一個檔案作為輸入位元組流 
FSDataOutputStream create(Path path):建立一個檔案,返回值是一個輸出流。
void copyFromLocalFile(Path 本地檔案,Path hdfs目錄):從本地上傳到hdfs
copyToLocalFil e(Path hdfs檔案,Path hdfs檔案)
boolean delete(Path path,Boolean 是否遞迴刪除):刪除檔案/目錄,通常引數2為true
boolean rename(path from, path to):重新命名,等價於fs shell命令的mv
FileStatus getFileStatus(Path f):查詢檔案的詳細資訊,包括大小,許可權,塊大小等。
FileStatus[] listStatus(Path f):列出目錄的內容,返回檔案的詳細資訊陣列。

5、FileStatus類():代表檔案的詳細資訊

檔案全路徑: fileStatus.getPath()
檔案塊大小: fileStatus.getBlockSize()
檔案長度: fileStatus.getLen()
副本數量: fileStatus.getReplication()
使用者: fileStatus.getOwner()
使用者組: fileStatus.getGroup()
許可權: fileStatus.getPermission().toString()

6、java.net.URI

create(String prefix):建立一個帶有prefix字首的uri物件

使用FileSystem API程式設計步驟

1、 獲取Configuration物件

Configuration conf = new Configuration();   //預設載入core-default.xml
//conf.addResource("core-site.xml");        //預設從eclipse的src載入core-site.xml(這個檔案可以到hadoop裡面配置檔案copy出來)
//conf.set(“fs.defaultFS”, “hdfs://hfbin1:9000/”); //也可以這麼寫不需要載入

2、 獲取檔案系統的例項FileSystem物件

FileSystem fs = FileSystem.get(conf);
// FileSystem.get(URI.create("hdfs://hfbin1:9000"), conf);//如果複製core-site.xml到src下了,需要的路徑時不需要指定字首,若core-site.xml沒有加到src下則需要字首。

3、 使用FileSystem物件操作檔案

開啟 open
建立 create
上傳 copyFromLocalFile
下載  copyToLocalFile
刪除 delelte
重新命名 rename
檔案詳細資訊 getFileStatus
使用hadoop提供的檔案系統API操作資料(讀增刪改名複製和檔案詳細資訊)

讀取檔案內容列印在控制檯

package cn.hfbin;
import java.io.IOException;
import java.io.InputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class FileSystemCat {
    public static void main(String[] args) {
        //1、獲取Configuration物件
        Configuration conf = new Configuration();
        //conf.addResource("core-site.xml");//已經自動從本地src目錄加了。
        InputStream in = null;
        try {
            //2、獲取檔案系統的例項FileSystem物件
            //FileSystem fs = FileSystem.get(URI.create("hdfs://node1:9000"), conf);//不需要core-site.xml制定字首
            FileSystem fs = FileSystem.get(conf);//需要core-site.xml制定字首
            //3、使用FileSystem物件操作檔案:開啟一個檔案作為輸入位元組流
            in = fs.open(new Path("/music/music1.txt"));    //例如:/input2/file4.txt
            IOUtils.copyBytes(in, System.out, 4096, false);
        } catch (IOException e) {           
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(in);
        }
    }
}

都是按照步驟來操作的上面都有備註,在這裡我就另外說一下IOUtils.copyBytes()方法引數的意思:

第一個引數 in:  是FSDataInputStream類的物件,是有關讀取檔案的類,也就是所謂“輸入流”
第二個引數 out:  是FSDataOutputStream類的物件,是有關檔案寫入的類,也就是“輸出流”(上面我是使用System.out將內容在控制檯輸出)
第三個引數 4096  表示用來拷貝的buffer大小(buffer是緩衝區)
第四個引數 false  表明拷貝完成後我們並不關閉拷貝源可拷貝目的地

建立檔案並將Hello World寫進檔案中

public class FileSystemCreate { 
    public static void main(String[] args) throws IOException {     
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);       
        String filename = "/input2/file.txt";
        FSDataOutputStream outputStream = fs.create(new Path(filename));//建立一個檔案
        InputStream in = new ByteArrayInputStream("Hello World".getBytes("utf-8"));
        IOUtils.copyBytes(in, outputStream, 4096, true);        
    }
}

將本檔案上傳到hdfs

public class FileSystemCopy {
    public static void main(String[] args) throws IOException {     
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);       
        fs.copyFromLocalFile(new Path("/home/hduser/file/file1"), new Path("/output"));//從本地上傳到hdfs  (第一個引數是本地路徑,第二個引數是hdf檔案路徑)
    }
}

刪除一個檔案

public class FileSystemDelete {
    public static void main(String[] args) throws IOException {
        String uri="/input/file1";
        Configuration conf = new Configuration();       
        FileSystem fs = FileSystem.get(conf);
        Path f=new Path(uri);   
        fs.delete(f,true);
}

重新命名/移動檔案

public class FileSystemRename {
    public static void main(String[] args) throws IOException {
        String fromFile="/input2/file1";//
        String toFile="/input2/file1new";//
        Configuration conf = new Configuration();       
        FileSystem fs = FileSystem.get(conf);
        Path fromPath=new Path(fromFile);
        Path toPath=new Path(toFile);       
        fs.rename(fromPath, toPath);
    }
}

獲取檔案資訊

public class FileSystemGetStatus {
    public static void main(String[] args) throws IOException {
        String uri=args[0];//如:/input2/file1.txt
        Configuration conf = new Configuration();       
        FileSystem fs = FileSystem.get(conf);
        Path f=new Path(uri);
        FileStatus stat=fs.getFileStatus(f);
        System.out.println("檔案路徑:"+stat.getPath());             
        System.out.println("檔案塊大小:"+stat.getBlockSize());
        System.out.println("檔案大小:"+stat.getLen());
        System.out.println("副本數量:"+stat.getReplication());
        System.out.println("使用者:"+stat.getOwner());
        System.out.println("使用者組:"+stat.getGroup());
        System.out.println("許可權:"+stat.getPermission().toString());
    }
}

到這就講解完畢了!!!感謝各位老鐵的閱讀