eclipse + hadoop 1.2.1 搭建过程记录

我的环境是centos 6.2,  hadoop与eclipse都运行其中

1 首先下载安装eclipse, 注意eclipse的版本,需要是4.2, 本人开始下的最新4.3竟然与下面的插件不兼容,汗。

 

2 编译hadoop-eclipse-plugin-1.2.1.jar

参考  http://my.oschina.net/vigiles/blog/132238

把编译好的插件放入 $eclipse_home/dropins/hadoop/plugins/ (mkdir -p 创建)

 

3 启动eclipse 新建mapreduce project, 配置好map/reduce location, 就可以看到hdfs中的文件了

下面简单的代码分别是 GetFile( 遍历hdfs中dir ), GetFileStatus(得到文件的相关状态,比如这个文件有多少个block,分别在哪个host上)

package ysp.hdfs;

import java.io.IOException;
import java.util.Date;

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

public class TestHdfs {
    private static FileSystem hdfs;

    public static void GetFile() throws Exception {
        // TODO Auto-generated method stub
        // 1.创建配置器
        Configuration conf = new Configuration();

        // 2.创建文件系统(指定为HDFS文件系统到URI)
        // hdfs = FileSystem.get(URI.create("hdfs://localhost:9000/"), conf);

        // 2.创建文件系统
        conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));
        hdfs = FileSystem.get(conf);

        // 3.遍历HDFS上的文件和目录
        FileStatus[] fs;
        fs = hdfs.listStatus(new Path("/home"));

        if (fs.length > 0) {
            for (FileStatus f : fs) {
                showDir(f);
            }
        }
    }

    public static void GetFileStatus(String filepath) throws Exception {
        // TODO Auto-generated method stub
        // 1.创建配置器
        Configuration conf = new Configuration();

        // 2.创建文件系统(指定为HDFS文件系统到URI)
        // hdfs = FileSystem.get(URI.create("hdfs://localhost:9000/"), conf);

        // 2.创建文件系统
        conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));
        hdfs = FileSystem.get(conf);

        Path path = new Path(filepath);
        FileStatus status = hdfs.getFileStatus(path);
        BlockLocation[] blockLocations = hdfs.getFileBlockLocations(status, 0,
                status.getLen());

        int blockLen = blockLocations.length;
        System.err.println("块数量:" + blockLen);
        for (int i = 0; i < blockLen; i++) {
            String[] hosts = blockLocations[i].getHosts();
            for (String host : hosts) {
                System.err.println("主机:" + host);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        GetFile();
        GetFileStatus("/home/fs-test.txt");
    }

    private static void showDir(FileStatus fs) {
        Path path = fs.getPath();
        long time = fs.getModificationTime();
        System.out.println("last modify time is: " + new Date(time));
        System.out.println(path);
        // 如果是目录
        if (fs.isDir()) {
            FileStatus[] f = {};

            try {
                f = hdfs.listStatus(path);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if (f.length > 0) {
                for (FileStatus file : f) {
                    showDir(file);
                }
            }
        }
    }
}

 

 

 

posted on 2013-09-11 15:19  gentlepong  阅读(1171)  评论(0编辑  收藏  举报