折腾了半天。终于编写成功了第一个自己的mapreduce程序,并通过打jar包的方式运行起来了。

运行环境:

windows 64bit

eclipse 64bit

jdk6.0 64bit

一、工程准备

1、新建java project

2、导入jar包

新建一个user library 把hadoop文件夹里的hadoop-core和lib包里的所有包都导入进来,以免出错。

二、编码

1、主要是计算单词的小程序,测试用

package com.hirra;

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
//Mapper<keyin,valuein,keyout,valueout>
public static class WordCountMapper extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text(); @Override
protected 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);//Context机制
}
}
} //嵌套类Reducer
//Reduce<keyin,valuein,keyout,valueout>
//Reducer的valuein类型要和Mapper的va lueout类型一致,Reducer的valuein是Mapper的valueout经过shuffle之后的值
public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
private IntWritable result = new IntWritable(); @Override
protected void reduce(Text key, Iterable<IntWritable> values,
Context context)
throws IOException, InterruptedException {
int sum = 0;
for(IntWritable i:values){
sum += i.get();
}
result.set(sum);
context.write(key,result);//Context机制
} } public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();//获得Configuration配置 Configuration: core-default.xml, core-site.xml 
     //很关键
     conf.set("mapred.job.tracker", "hadoopmaster:9001");
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();//获得输入参数[hdfs://localhost:9000/user/dat/input, hdfs://localhost:9000/user/dat/output]
if(otherArgs.length != 2){//判断输入参数个数,不为两个异常退出
System.err.println("Usage:wordcount <in> <out>");
System.exit(2);
} ////设置Job属性
Job job = new Job(conf,"word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCountMapper.class);
job.setCombinerClass(WordCountReducer.class);//将结果进行局部合并
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//传入input path
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//传入output path,输出路径应该为空,否则报错org.apache.hadoop.mapred.FileAlreadyExistsException。 System.exit(job.waitForCompletion(true)?0:1);//是否正常退出
}
}

2、注意问题

有些jar包没导入会出现问题

三、生成jar包

1、eclipse自带功能export jar包

四、运行

1、ssh client工具导入至linux

2、hadoop运行,转到hadoop的bin目录下,执行下面指令:

./hadoop jar test.jar /README.txt /usr/dat/output  

3、注意问题

output目录必须是之前不存在的路径。

mapreduce程序编写(WordCount)的更多相关文章

  1. 运行第一个MapReduce程序,WordCount

    1.安装Eclipse 安装后如果无法启动重新配置Java路径(如果之前配置了Java) 2.下载安装eclipse的hadoop插件 注意版本对应,放到/uer/lib/eclipse/plugin ...

  2. Hadoop学习之路(5)Mapreduce程序完成wordcount

    程序使用的测试文本数据: Dear River Dear River Bear Spark Car Dear Car Bear Car Dear Car River Car Spark Spark D ...

  3. MapReduce 程序:WordCount

  4. 一起学Hadoop——使用IDEA编写第一个MapReduce程序(Java和Python)

    上一篇我们学习了MapReduce的原理,今天我们使用代码来加深对MapReduce原理的理解. wordcount是Hadoop入门的经典例子,我们也不能免俗,也使用这个例子作为学习Hadoop的第 ...

  5. 大数据之路week07--day03(Hadoop深入理解,JAVA代码编写WordCount程序,以及扩展升级)

    什么是MapReduce 你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃. MapReduce方法则是: 1.给在座的所有玩家中分配这摞牌 2.让每个玩家数自己手中的牌有几 ...

  6. 编写简单的Mapreduce程序并部署在Hadoop2.2.0上运行

    今天主要来说说怎么在Hadoop2.2.0分布式上面运行写好的 Mapreduce 程序. 可以在eclipse写好程序,export或用fatjar打包成jar文件. 先给出这个程序所依赖的Mave ...

  7. 用PHP编写Hadoop的MapReduce程序

    用PHP编写Hadoop的MapReduce程序     Hadoop流 虽然Hadoop是用Java写的,但是Hadoop提供了Hadoop流,Hadoop流提供一个API, 允许用户使用任何语言编 ...

  8. 如何在maven项目里面编写mapreduce程序以及一个maven项目里面管理多个mapreduce程序

    我们平时创建普通的mapreduce项目,在遍代码当你需要导包使用一些工具类的时候, 你需要自己找到对应的架包,再导进项目里面其实这样做非常不方便,我建议我们还是用maven项目来得方便多了 话不多说 ...

  9. MapReduce程序(一)——wordCount

    写在前面:WordCount的功能是统计输入文件中每个单词出现的次数.基本解决思路就是将文本内容切分成单词,将其中相同的单词聚集在一起,统计其数量作为该单词的出现次数输出. 1.MapReduce之w ...

随机推荐

  1. Nginx启动SSL功能,并进行功能优化,你看这个就足够了

    一:开始Nginx的SSL模块 1.1 Nginx如果未开启SSL模块,配置Https时提示错误 nginx: [emerg] the "ssl" parameter requir ...

  2. 启动 mysql 失败 Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql'

    Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql' 这应该是某种情况下导致/usr/ ...

  3. Mac上安装brew

    用过ubuntu系统的都知道,上面有一个命令apt-get 很方便可以快速的安装很多软件 特别lamp环境 都是一键安装. 在mac上也有类似的命令 brew brew用法可以访问官网地址  http ...

  4. js学习笔记一类型、值和变量

    js的数据类型分为两类:原始类型和对象类型 原始类型包括数字.字符串和布尔值 js中有2个特殊的原始值:null(空)和undefined(未定义) 对象是属性的集合,每个属性都由名/值对组成 js的 ...

  5. 跟着PHP100第一季学写一个CMS(11-20)

    PS.刚发现IE并不兼容,有点悲剧  cms1.1密码处理 1.小知识:sha1($_answer) //加密为40位,不知道能不能破解 2.小知识:mysql_real_escape_string( ...

  6. [转] shell字符串操作方法,以及实例

    每一种语言都有他独自的字符串操作方法,shell也一样,下面以以例子的方式,简单介绍常用方法. 1,取得字符串长度 string=abc12342341 //等号二边不要有空格 echo ${#str ...

  7. Json(2)-DataContractJsonSerializer

    public static void DataContractSerializeDemo()        {            User user = new User { UserID = 1 ...

  8. 如何访问Microsoft Azure Storage

    首先先要创建存储账户 http://www.cnblogs.com/SignalTips/p/4119128.html 可以通过以下的几个方式访问 通过Visual Studio 2013 Commu ...

  9. FastLoad错误 — SELECT Failed. 2652

    SELECT * FROM   teradata_education. emp_fl;     在做查询时碰到2652错误.   SELECT Failed. 2652: Operation not ...

  10. 从零开始学ios开发(十五):Navigation Controllers and Table Views(中)

    这篇内容我们继续上一篇的例子接着做下去,为其再添加3个table view的例子,有了之前的基础,学习下面的例子会变得很简单,很多东西都是举一反三,稍稍有些不同的内容,好了,闲话少说,开始这次的学习. ...