eclipse + hadoop 1.2.1 搭建过程记录

本文详细介绍如何在Eclipse中集成Hadoop环境,并提供具体步骤和代码实例,包括配置环境、编译插件及实现MapReduce项目等。

我的环境是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);
                }
            }
        }
    }
}

 

 

 

转载于:https://www.cnblogs.com/bruceyan/p/3311736.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值