5.hdfs的文件系统基本操作

文件系统上的文件只有这两个位于/目录下。
    -rw-r--r--   1 root supergroup  124191203 2016-07-19 16:44 /hadoop-2.4.1-x64.tar.gz
    -rw-r--r--   3 root supergroup  124191203 2016-07-20 15:53 /test.tgz
0.拿到FileSystem对象的方法。
    A:通过conf拿到文件系统
        Configuration conf =new Configuration();
        conf.set("fs.defaultFS","hdfs://linux-hadoop:9000");
        FileSystem fs = FileSystem.get(conf);
    B:通过URI拿到文件系统的方法已过时。
    C:在
        
1.下载一个文件
    fs.copyToLocalFile(new Path("/hadoop-2.4.1-x64.tar.gz"), new Path("/root/test.tgz"));
    下载hdfs /目录下的hadoop-2.4.1-x64.tar.gz文件到 /root下 ,并命名为test.tgz
2.上传一个文件
    fs.copyFromLocalFile(new Path("/root/test1.tgz"), new Path("/"));
    上传本地文件/root/test1.tgz到hdfs文件系统的根目录。
3.用流的方式copy下载一个文件
    FSDataInputStream is =  fs.open(new Path("/test.tgz"));
    FileOutputStream os = new FileOutputStream("/bb.tgz");
    //将出入流的内容copy到输出流,会自动关闭流。
    IOUtils.copy(is, os);
    //也可以手动关闭
    IOUtils.closeQuietly(is);
    IOUtils.closeQuietly(os);
4.删除文件
    //第一个是路径,第二个是是否递归删除的选择符号
    fs.delete(new Path("/test1.tgz"),true);
    fs.close();
5.如果是可查看的文件,可以设置从文件中的某个字节开始复制。
    FSDataInputStream is =  fs.open(new Path("/aa"));
    FileOutputStream os = new FileOutputStream("/aacopy");
    //将出入流的内容copy到输出流,会自动关闭流。
    //从第4个字节开始copy,索引从0开始
    is.seek(4);
    IOUtils.copy(is, os);
    //也可以手动关闭
    IOUtils.closeQuietly(is);
    IOUtils.closeQuietly(os);

6.用流的方式上传一个文件

  FSDataOutputStream os = fs.create(new Path("/aacopy"));
        FileInputStream is = new FileInputStream("/root/Desktop/aa");
        //将出入流的内容copy到输出流,会自动关闭流。
        IOUtils.copy(is, os);
        //也可以手动关闭

package com.xws.hdfs;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;

public class GetFs {
    Configuration conf =null;
    FileSystem fs = null;
    @Before
    public void init() throws IOException{
         conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://linux-hadoop:9000");
        fs = FileSystem.get(conf);
    }
    @Test
    public void deleteFs() throws IllegalArgumentException, IOException{
        //第一个是路径,第二个是是否递归删除的选择符号
        fs.delete(new Path("/test1.tgz"),true);
        fs.close();
    }
    //此方法仅适用与将hdfs的文件copy到本地
    @Test
    public void IODownload() throws IllegalArgumentException, IOException{
        FSDataInputStream is =  fs.open(new Path("/aa"));
        FileOutputStream os = new FileOutputStream("/aacopy");
        //将出入流的内容copy到输出流,会自动关闭流。
        //从第4个字节开始copy,索引从0开始
        is.seek(4);
        IOUtils.copy(is, os);
        //也可以手动关闭
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
    @Test
    public void IOUpload() throws IllegalArgumentException, IOException{
        FSDataOutputStream os = fs.create(new Path("/aacopy"));
        FileInputStream is = new FileInputStream("/root/Desktop/aa");
        //将出入流的内容copy到输出流,会自动关闭流。
        IOUtils.copy(is, os);
        //也可以手动关闭
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}

 

 

 

 

 

 

 

 

 

 

 


        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);

posted @ 2016-07-22 11:07  博智星  Views(222)  Comments(0)    收藏  举报