hadoop2.7.0实践- WordCount
环境要求
说明:本文档为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的更多相关文章
- 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 ...
- hadoop2.6.0实践:002 检查伪分布式环境搭建
1.检查网络配置[root@hadoop-master ~]# cat /etc/sysconfig/networkNETWORKING=yesHOSTNAME=hadoop-masterGATEWA ...
- Hadoop2.6.0实践:001 伪分布式环境搭建
##################### Centos6.4VM_01_os.rar ################################################准备工作/opt ...
- hadoop2.6.0实践:004 启动伪分布式hadoop的进程
[hadoop@LexiaofeiMaster hadoop-2.6.0]$ start-dfs.shStarting namenodes on [localhost]localhost: start ...
- 在Linux上编译Hadoop-2.4.0实践与总结
问题导读: 1.编译源码前需要安装哪些软件? 2.安装之后该如何设置环境变量? 3.为什么不要使用JDK1.8? 4.mvn package -Pdist -DskipTests -Dtar的作用是什 ...
- hadoop2.2.0的WordCount程序
package com.my.hadoop.mapreduce.wordcount; import java.io.IOException; import org.apache.hadoop.conf ...
- hadoop2.6.0实践:A02 问题处理 util.NativeCodeLoader: Unable to load native-hadoop library for your platform
############################################################# hadoop "util.NativeCodeLoader: Un ...
- hadoop2.6.0实践:A03 例子验证
[hadoop@LexiaofeiN1 ~]$ hdfs dfs -ls /output/grep[hadoop@LexiaofeiN1 ~]$ hdfs dfs -rm -R /output/gre ...
- 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 ...
随机推荐
- Android Studio初级介绍
Android Studio原来不咋地,但是现在可以尝试抛弃eclipse转用它了, 亲儿子到底是亲儿子,现在的Android Studio已经今非昔比,用了一段时间,简直爱不释手,我觉得,It's ...
- ACM/CF赛制getstart模板
(包含整型变量快速读入.简易循环和连续容器的迭代器循环的宏定义.调试时的运行时间输出(编译选项应有“DEBUG”宏定义)等) 1 /*================================ ...
- 用yum安装完mysql后没有mysqld的问题
在Centos中用命令 yum install mysql安装数据库,但装完后运行mysqld启动mysql的时候提示找不到,通过 find / | grep mysqld 也没找到mysqld的目录 ...
- linux自定义开机启动服务和chkconfig使用方法
linux自定义开机启动服务和chkconfig使用方法 1. 服务概述在linux操作系统下,经常需要创建一些服务,这些服务被做成shell脚本,这些服务需要在系统启动的时候自动启动,关闭的时候自动 ...
- JavaScript 创建类/对象的几种方式
在JS中,创建对象(Create Object)并不完全是我们时常说的创建类对象,JS中的对象强调的是一种复合类型,JS中创建对象及对对象的访问是极其灵活的. JS对象是一种复合类型,它允许你通过变量 ...
- 【算法导论C++代码】Strassen算法
简单方阵矩乘法 SQUARE-MATRIX-MULTIPLY(A,B) n = A.rows let C be a new n*n natrix to n to n cij = to n cij=ci ...
- linux系统下mysql跳过密码验证登录和创建新用户
修改MySQL的登录设置: # vi /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] datadir=/var/lib/mysq ...
- Android EditText 状态切换
不可编辑状态 <EditText android:id="@+id/ed_newPwd" an ...
- 云计算之路-试用Azure:每一次删除都让人如此“心惊”
这篇博文吐槽的是Azure(Virtual Machine)的虚拟机删除功能. 在阿里云中,没有提供直接的虚拟机删除操作,如果不用某台虚拟机,“停止”即可,过期一段时间后会自动释放(这里的释放相当于删 ...
- LinkedIn架构这十年
原文: A Brief History of Scaling LinkedIn 2003年是LinkedIn元年,公司成立的目标是连接你的个人人脉以获得更好的的工作机会.上线第一周才有2700个会员注 ...