Eclipse连接Hadoop集群并运行wordCount全过程记录

Stella981
• 阅读 834

为了自己以后方便会看记录一下整个过程,Hadoop集群之前已经搭建好,这部分工作以后再补写。

环境:win10     Eclipse4.4.2     JDK1.7     Hadoop2.7.1     

相关安装文件可在文末的链接下载

1.加载eclipse插件

(1)下载hadoop-eclipse-plugin插件,此处下载了hadoop-eclipse-plugin-2.7.1。将hadoop-eclipse-plugin-2.7.1.jar拷贝到 eclipse的plugins目录。重启eclipse。

(2)下载Hadoop,解压到自己想要的路径,如D:\Java\hadoop-2.7.1

(3)打开 window-->preferences ,配置Hadoop MapReduce的安装路径,即(2)中的路径,如下图示

Eclipse连接Hadoop集群并运行wordCount全过程记录

配置HADOOP_HOME环境变量,指向D:\Java\hadoop-2.7.1路径

2.配置MapReduce

(4)打开MapReduce视图。Window-->Show View-->Other 窗口,选择 MapReducer Locations,视图如下图所示

Eclipse连接Hadoop集群并运行wordCount全过程记录

(5)点击蓝色小象新增按钮,提示输入MapReduce和HDFS Master相关信息

Eclipse连接Hadoop集群并运行wordCount全过程记录

(6)配置完成后左侧project Explorer中会出现DFS Location

Eclipse连接Hadoop集群并运行wordCount全过程记录 该目录为HDFS目录,可以右击向HDFS传文件。

在user文件中建立input output两个文件夹,并往input文件夹中上传wordCountInput.txt 内容为 

c++ java hello 
world java hello 
you me too

若出现权限问题考虑是否对文件有写的权限,实在不行文件权限改为777。可以ssh连接到hadoop的master服务器  hadoop fs -ls /user 查看目录  hadoop fs -cat /user/aa.txt查看文件

3.创建MapReduce Project 

(7)file->new->other 选择Map/Reduce Project 命名为MyHadoop

Eclipse连接Hadoop集群并运行wordCount全过程记录

(8)在项目中建包,并建一个类名为WordCount.java的类。具体源码如下,来自于官方WordCount源码

//package org.apache.hadoop.examples;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

    /*
     * 通过扩展Mapper实现内部类TokenizerMapper
     */
    public static class TokenizerMapper extends
            Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        /*
         * 重载map方法(non-Javadoc)
         * 
         * @see org.apache.hadoop.mapreduce.Mapper#map(KEYIN, VALUEIN,
         * org.apache.hadoop.mapreduce.Mapper.Context)
         */
        public void map(Object key, Text value, Context context)
                throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);// 写入处理的中间结果<key,value>
            }
        }
    }

    /*
     * 通过扩展Reducer实现内部类IntSumReducer
     */
    public static class IntSumReducer extends
            Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();

        /*
         * 重载reduce方法(non-Javadoc)
         * 
         * @see org.apache.hadoop.mapreduce.Reducer#reduce(KEYIN,
         * java.lang.Iterable, org.apache.hadoop.mapreduce.Reducer.Context)
         */
        public void reduce(Text key, Iterable<IntWritable> values,
                Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get(); // 计数
            }
            result.set(sum);
            context.write(key, result); // 写回结果
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration(); // 启用默认配置
        String[] otherArgs = new GenericOptionsParser(conf, args)
                .getRemainingArgs();
        if (otherArgs.length != 2) {
            System.err.println("Usage: wordcount <in> <out>");
            System.exit(2);
        }
        Job job = new Job(conf, "word count");// 定义一个job
        job.setJarByClass(WordCount.class);// 设定执行类
        job.setMapperClass(TokenizerMapper.class);// 设定Mapper实现类
        job.setCombinerClass(IntSumReducer.class);// 设定Combiner实现类
        job.setReducerClass(IntSumReducer.class);// 设定Reducer实现类
        job.setOutputKeyClass(Text.class);// 设定OutputKey实现类,Text.class是默认实现
        job.setOutputValueClass(IntWritable.class);// 设定OutputValue实现类
        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));// 设定job输入文件夹
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));// 设定job输出文件夹
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

(9)加载hadoop运行参数。即加载输入输出文件,右键->run as->run configuration 设置Arguments

Eclipse连接Hadoop集群并运行wordCount全过程记录 ,点击Apply并退出。右击 run as ->Run on Hadoop。

(10)错误及解决方法

可能会出现 log4j:WARN No appenders could be found for logger错误:

解决方法:

在src下面新建file名为log4j.properties内容如下:
# Configure logging for testing: optionally with log file
log4j.rootLogger=WARN, stdout
# log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

windows可能会出现 hadoop2.7.1运行Wordcount错误
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1012)

Exception in thread "main" Java.lang.UnsatisfiedLinkError

解决方法:

    1:将Wordcount.jar文件解压到hadoop的bin目录下,文件可在文末的链接下载
    2:将hadoop.dll复制到C:\Window\System32下
    3:添加环境变量HADOOP_HOME,指向Hadoop目录

    4:将%HADOOP_HOME%\bin加入到path里面

    5:重启myeclipse或者eclipse

(11)运行正常之后会在刚刚传入的Hadoop输出文件中输出

Eclipse连接Hadoop集群并运行wordCount全过程记录  part-r-00000为结果Eclipse连接Hadoop集群并运行wordCount全过程记录

(12)用到的文件 http://pan.baidu.com/s/1pLk7UQ3

(13)最后感谢千面人对我的帮助,https://my.oschina.net/amhuman/blog/845826 这篇博文对读者帮助很大,欢迎大家阅读。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Eclipse插件开发_学习_00_资源帖
一、官方资料 1.eclipseapi(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fhelp.eclipse.org%2Fmars%2Findex.jsp%3Ftopic%3D%252Forg.eclipse.platform.doc.isv%252Fguide%2
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
4个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这