码迷,mamicode.com
首页 > Windows程序 > 详细

HDFS API 学习:几个常用的API

时间:2015-01-22 23:19:29      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:

1.Hadoop-1.2.1 API 文档:http://hadoop.apache.org/docs/r1.2.1/api/

2.几个API:

  create(Path f) :Opens an FSDataOutputStream at the indicated Path.

  copyFromLocalFile(Path src, Path dst) :The src file is on the local disk.

  create(Path f) :Opens an FSDataOutputStream at the indicated Path.

  boolean exists(Path f) :Check if exists.

  get(URI uri, Configuration conf):Returns the FileSystem for this URI‘s scheme and authority.

  listStatus(Path f): List the statuses of the files/directories in the given path if the path is a directory.

  mkdirs(Path f) : Call mkdirs(Path, FsPermission) with default permission.

  rename(Path src, Path dst) :Renames Path src to Path dst.

3.代码实现:

import java.io.IOException;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class testHDFS {

    static String hdfs = "hdfs://localhost:9000";
    static Configuration conf = new Configuration();

    public static void createFolder() throws IOException {

        FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
        Path path = new Path("/test");
        fs.mkdirs(path);
        fs.close();
    }

    public static void createFile() throws IOException {

        FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
        Path path = new Path("/test/test3.txt");
        FSDataOutputStream out = fs.create(path);
        out.write("hello hadoop.".getBytes());
    }

    public static void renameFile() throws IOException {

        FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
        Path path = new Path("/test/test1.txt");
        Path newPath = new Path("/test/test2.txt");
        System.out.println(fs.rename(path, newPath));
    }

    public static void uploadFileToHDFS() throws IOException {

        FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
        Path src = new Path("/home/ares/test.txt");
        Path dst = new Path("/test");
        fs.copyFromLocalFile(src, dst);
    }

    public static void listFile() throws IOException {

        FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
        Path path = new Path("/test");
        FileStatus[] files = fs.listStatus(path);
        for (FileStatus file : files) {
            System.out.println(file.getPath().toString());
        }
    }

    public static void deleteFileOnHDFS() throws IOException {
        FileSystem fs = FileSystem.get(URI.create(hdfs), conf);
        Path path = new Path("/test/test2.txt");
        boolean isExists = fs.exists(path);
        if (isExists) {
            fs.delete(path, isExists);
            System.out.println("file is deleted.");
        } else {
            System.out.println("file is exist.");
        }
    }

    public static void main(String[] args) throws IOException {
        // createFolder();
        // createFile();
        // renameFile();
        // uploadFileToHDFS() ;
        // listFile();
         deleteFileOnHDFS();
    }
}

 

HDFS API 学习:几个常用的API

标签:

原文地址:http://www.cnblogs.com/xp12/p/4242882.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!