环境要求

说明:本文档为wordcount的mapreduce job编写及执行文档。

操作系统:Ubuntu14 x64位

Hadoop:Hadoop 2.7.0

Hadoop官网:http://hadoop.apache.org/releases.html

MapReduce參照官网步骤:

http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html#Source_Code

本章基于前一篇文章《hadoop2.7.0实践-环境搭建》。

1.安装Eclipse

1)下载eclipse

官网:http://www.eclipse.org/



2)解压eclipse包

$tar -xvf eclipse-jee-mars-R-linux-gtk-x86_64.tar.gz

3)启动eclipse

4)写測试程序

public class TestMore {

    public static void main(String[] args) {
System.out.println("hello world!");
System.out.println("I'm so glad to see that");
}
}

2.编写wordcount

1)jar包引入

eclipse的lib中引入的jar包

hadoop包下的share/hadoop下的各个文件夹都有jar包

hadoop-2.7.0/share/hadoop/common/hadoop-common-2.7.0.jar

hadoop-2.7.0/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.7.0.jar

2)编写worcount程序

相应源代码

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; public class WordCount { public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1);
private Text word = new Text(); 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);
}
}
} public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable(); 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();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

3)导出jar包

取名wc.jar,直接导出到hadoop文件夹下。



3.执行wordcount

1)启动dfs服务

參照文件《hadoop2.7.0实践-环境搭建》。

进入hadoop文件夹,用cd命令。

$sbin/start-dfs.sh

相应查看网页:http://localhost:50070/

2)准备文件

hadoop-2.7.0/wctest/input文件夹中放入待统计文件file01

输入内容:hello world bye world

//创建hdfs文件夹。操作命令相似本地操作

$ bin/hdfs fs -mkdir /user
$ bin/hdfs fs -mkdir /user/a

//复制本地文件到hdfs中

$ bin/hdfs fs -put wctest/input /user/a/input

//备注:相应文件夹删除命令例如以下

delete dir:bin/hadoop fs -rm -f -r /user/a/input

相应文件http://localhost:50070/

3)启动yarn服务

$ sbin/start-yarn.sh

4)执行wordcount程序

$ bin/hadoop jar wc.jar WordCount /user/a/input /user/a/output

5)查看结果

$ bin/hadoop fs -cat /user/a/output/part-r-00000
bye 1
hello 1
world 2

常见错误及说明

1)未启动yarn时执行MapReduce程序



原因:已经配置了yarn,但没有启动引起的

调整:启动一下yarn

$ sbin/start-yarn.sh

hadoop2.7.0实践- WordCount的更多相关文章

  1. hadoop2.6.0实践:引入开发依赖的jar包

    hadoop-2.5.0\share\hadoop\common  所有jar,hadoop-2.5.0\share\hadoop\common\lib  所有jar,hadoop-2.5.0\sha ...

  2. hadoop2.6.0实践:002 检查伪分布式环境搭建

    1.检查网络配置[root@hadoop-master ~]# cat /etc/sysconfig/networkNETWORKING=yesHOSTNAME=hadoop-masterGATEWA ...

  3. Hadoop2.6.0实践:001 伪分布式环境搭建

    ##################### Centos6.4VM_01_os.rar ################################################准备工作/opt ...

  4. hadoop2.6.0实践:004 启动伪分布式hadoop的进程

    [hadoop@LexiaofeiMaster hadoop-2.6.0]$ start-dfs.shStarting namenodes on [localhost]localhost: start ...

  5. 在Linux上编译Hadoop-2.4.0实践与总结

    问题导读: 1.编译源码前需要安装哪些软件? 2.安装之后该如何设置环境变量? 3.为什么不要使用JDK1.8? 4.mvn package -Pdist -DskipTests -Dtar的作用是什 ...

  6. hadoop2.2.0的WordCount程序

    package com.my.hadoop.mapreduce.wordcount; import java.io.IOException; import org.apache.hadoop.conf ...

  7. hadoop2.6.0实践:A02 问题处理 util.NativeCodeLoader: Unable to load native-hadoop library for your platform

    ############################################################# hadoop "util.NativeCodeLoader: Un ...

  8. hadoop2.6.0实践:A03 例子验证

    [hadoop@LexiaofeiN1 ~]$ hdfs dfs -ls /output/grep[hadoop@LexiaofeiN1 ~]$ hdfs dfs -rm -R /output/gre ...

  9. hadoop2.6.0实践:A01 问题处理 DEPRECATED: Use of this script to execute hdfs command is deprecated.

    [hadoop@hadoop-master data]$ hadoop dfs -ls /DEPRECATED: Use of this script to execute hdfs command ...

随机推荐

  1. JavaScript之引用类型(Object类型)

    ECMAScript提供了很多原生的引用类型,以便开发人员进行常见的计算任务. 对象是某一个特定引用类型的的实例. Object类型 用的最多.虽然这个Object实例不具备多少功能,但是在应用程序的 ...

  2. js禁止复制粘贴

    1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键,其实是禁止快捷菜单,因为不光右键可以弹出这个菜单,键盘上空格 ...

  3. juqery.fn.extend和jquery.extend

    jquery.fn == jquery.prototype //true jquery.extend( obj1,obj2 ) 用一个或多个对象来拓展一个对象,返回拓展之后的对象 var aaa = ...

  4. pycharm的插件pylint报错:java.lang.Throwable: Write-unsafe context! Model changes are allowed from write-safe contexts only. Please ensure you're using invokeLater/invokeAndWait with a correct modality stat

    java.lang.Throwable: Write-unsafe context! Model changes are allowed from write-safe contexts only. ...

  5. [转]在SSIS中,使用“包配置”时的常见错误与解析

    本文转自:http://www.cnblogs.com/invinboy/archive/2008/05/26/1034312.html 在以前的DTS中,在包的开发.测试.发布迁移过程中你必须手动的 ...

  6. Linux pci驱动源码

    #include <linux/kernel.h>#include <linux/errno.h>#include <linux/module.h>#include ...

  7. IT人士感悟(转)

    我今年39岁了,25岁研究生毕业,工作14年,回头看看,应该说走了不少的弯路,有一些经验和教训.现在开一个小公司,赚的钱刚够养家糊口的.看看这些刚毕业的学生,对前景也很迷茫,想抛砖引玉,谈谈自己的看法 ...

  8. BS中保存参数

    开发中经常需要将值存起来,当点击某一项时以便知道点击了哪一项. 一:应用JS页面跳转(牛腩中讲到) HTML: <td class="txt c"><a href ...

  9. 使用kubeadm部署Kubernetes v1.13.3

    kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具. 这个工具能通过两条指令完成一个kubernetes集群的部署: 1. 安装要求 在开始之前,部署Kubernetes集群 ...

  10. [Tools] Unlock TypeScript's Features in Vanilla JS with @ts-check and JSDoc

    TypeScript can help you with your plain JavaScript files if you add a simple //@ts-check comment. Th ...