Fork me on GitHub

使用hadoop的类操作HDFS

1. 创建文件夹

    private static final String PATH = "hdfs://hadoop:9000/";
    private static final String DIR = "/d2";
    
    public static void main(String[] args) throws Exception 
    {
        FileSystem fileSystem = FileSystem.get(new URI(PATH), new Configuration());
        fileSystem.mkdirs(new Path(DIR));
    }

 2.上传文件

        private static final String PATH = "hdfs://hadoop:9000/";
	private static final String FILE = "/d2/hello";
	
	public static void main(String[] args) throws Exception 
	{
		FileSystem fileSystem = FileSystem.get(new URI(PATH), new Configuration());
		FSDataOutputStream out = fileSystem.create(new Path(FILE));
		FileInputStream in = new FileInputStream("D:/readme.txt");
		IOUtils.copyBytes(in, out, 1024, true);
	}

3.下载文件

    private static final String PATH = "hdfs://hadoop:9000/";
    private static final String FILE = "/d2/hello";
    
    public static void main(String[] args) throws Exception 
    {
        FileSystem fileSystem = FileSystem.get(new URI(PATH), new Configuration());
        FSDataInputStream in = fileSystem.open(new Path(FILE));
        IOUtils.copyBytes(in, System.out, 1024,true);
    }

 4.浏览文件夹

 

private static final String PATH = "hdfs://chaoren:9000/";
    
    public static void main(String[] args) throws Exception 
    {
        FileSystem fileSystem = FileSystem.get(new URI(PATH), new Configuration());
        FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
        
        for(FileStatus status : listStatus) 
        {
            String isDir = status.isDir()?"文件夹":"文件";
            String path = status.getPath().toString();
            long length = status.getLen(); 
            System.out.println(isDir + "\t" + length + "\t" + path);
        }
    }

5.删除文件夹

        private static final String PATH = "hdfs://hadoop:9000/";
    private static final String DIR = "/d3";
    
    public static void main(String[] args) throws Exception 
    {
        FileSystem fileSystem = FileSystem.get(new URI(PATH), new Configuration());
        fileSystem.delete(new Path(DIR), true);
    }

 

 

posted @ 2015-01-18 23:33  龙族小龙  阅读(281)  评论(0编辑  收藏  举报