知识点
相关文章
更多最近更新
更多Hadoop实例WordCount程序一步一步运行
2019-03-28 14:21|来源: 网络
虽说现在用Eclipse下开发
Hadoop程序很方便了,但是命令行方式对于小程序开发验证很方便。这是初学hadoop时的笔记,记录下来以备查。
1. 经典的WordCound程序(WordCount.java),可参见 hadoop0.18文档
import
java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class WordCount extends Configured implements Tool {
public static class MapClass extends MapReduceBase implements
Mapper < LongWritable, Text, Text, IntWritable > {
private final static IntWritable one = new IntWritable( 1 );
private Text word = new Text();
public void map(LongWritable key, Text value,
OutputCollector < Text, IntWritable > output, Reporter reporter)
throws IOException {
String line = value.toString();
StringTokenizer itr = new StringTokenizer(line);
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
output.collect(word, one);
}
}
}
/**
* A reducer class that just emits the sum of the input values.
*/
public static class Reduce extends MapReduceBase implements
Reducer < Text, IntWritable, Text, IntWritable > {
public void reduce(Text key, Iterator < IntWritable > values,
OutputCollector < Text, IntWritable > output, Reporter reporter)
throws IOException {
int sum = 0 ;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
static int printUsage() {
System.out.println( " wordcount [-m <maps>] [-r <reduces>] <input> <output> " );
ToolRunner.printGenericCommandUsage(System.out);
return - 1 ;
}
/**
* The main driver for word count map/reduce program. Invoke this method to
* submit the map/reduce job.
*
* @throws IOException
* When there is communication problems with the job tracker.
*/
public int run(String[] args) throws Exception {
JobConf conf = new JobConf(getConf(), WordCount. class );
conf.setJobName( " wordcount " );
// the keys are words (strings)
conf.setOutputKeyClass(Text. class );
// the values are counts (ints)
conf.setOutputValueClass(IntWritable. class );
conf.setMapperClass(MapClass. class );
conf.setCombinerClass(Reduce. class );
conf.setReducerClass(Reduce. class );
List < String > other_args = new ArrayList < String > ();
for ( int i = 0 ; i < args.length; ++ i) {
try {
if ( " -m " .equals(args[i])) {
conf.setNumMapTasks(Integer.parseInt(args[ ++ i]));
} else if ( " -r " .equals(args[i])) {
conf.setNumReduceTasks(Integer.parseInt(args[ ++ i]));
} else {
other_args.add(args[i]);
}
} catch (NumberFormatException except) {
System.out.println( " ERROR: Integer expected instead of "
+ args[i]);
return printUsage();
} catch (ArrayIndexOutOfBoundsException except) {
System.out.println( " ERROR: Required parameter missing from "
+ args[i - 1 ]);
return printUsage();
}
}
// Make sure there are exactly 2 parameters left.
if (other_args.size() != 2 ) {
System.out.println( " ERROR: Wrong number of parameters: "
+ other_args.size() + " instead of 2. " );
return printUsage();
}
FileInputFormat.setInputPaths(conf, other_args.get( 0 ));
FileOutputFormat.setOutputPath(conf, new Path(other_args.get( 1 )));
JobClient.runJob(conf);
return 0 ;
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run( new Configuration(), new WordCount(), args);
System.exit(res);
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class WordCount extends Configured implements Tool {
public static class MapClass extends MapReduceBase implements
Mapper < LongWritable, Text, Text, IntWritable > {
private final static IntWritable one = new IntWritable( 1 );
private Text word = new Text();
public void map(LongWritable key, Text value,
OutputCollector < Text, IntWritable > output, Reporter reporter)
throws IOException {
String line = value.toString();
StringTokenizer itr = new StringTokenizer(line);
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
output.collect(word, one);
}
}
}
/**
* A reducer class that just emits the sum of the input values.
*/
public static class Reduce extends MapReduceBase implements
Reducer < Text, IntWritable, Text, IntWritable > {
public void reduce(Text key, Iterator < IntWritable > values,
OutputCollector < Text, IntWritable > output, Reporter reporter)
throws IOException {
int sum = 0 ;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
static int printUsage() {
System.out.println( " wordcount [-m <maps>] [-r <reduces>] <input> <output> " );
ToolRunner.printGenericCommandUsage(System.out);
return - 1 ;
}
/**
* The main driver for word count map/reduce program. Invoke this method to
* submit the map/reduce job.
*
* @throws IOException
* When there is communication problems with the job tracker.
*/
public int run(String[] args) throws Exception {
JobConf conf = new JobConf(getConf(), WordCount. class );
conf.setJobName( " wordcount " );
// the keys are words (strings)
conf.setOutputKeyClass(Text. class );
// the values are counts (ints)
conf.setOutputValueClass(IntWritable. class );
conf.setMapperClass(MapClass. class );
conf.setCombinerClass(Reduce. class );
conf.setReducerClass(Reduce. class );
List < String > other_args = new ArrayList < String > ();
for ( int i = 0 ; i < args.length; ++ i) {
try {
if ( " -m " .equals(args[i])) {
conf.setNumMapTasks(Integer.parseInt(args[ ++ i]));
} else if ( " -r " .equals(args[i])) {
conf.setNumReduceTasks(Integer.parseInt(args[ ++ i]));
} else {
other_args.add(args[i]);
}
} catch (NumberFormatException except) {
System.out.println( " ERROR: Integer expected instead of "
+ args[i]);
return printUsage();
} catch (ArrayIndexOutOfBoundsException except) {
System.out.println( " ERROR: Required parameter missing from "
+ args[i - 1 ]);
return printUsage();
}
}
// Make sure there are exactly 2 parameters left.
if (other_args.size() != 2 ) {
System.out.println( " ERROR: Wrong number of parameters: "
+ other_args.size() + " instead of 2. " );
return printUsage();
}
FileInputFormat.setInputPaths(conf, other_args.get( 0 ));
FileOutputFormat.setOutputPath(conf, new Path(other_args.get( 1 )));
JobClient.runJob(conf);
return 0 ;
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run( new Configuration(), new WordCount(), args);
System.exit(res);
}
}
2. 保证hadoop集群是配置好了的,单机的也好。新建一个目录,比如 /home/admin/WordCount
编译WordCount.java程序。
javac
-
classpath
/
home
/
admin
/
hadoop
/
hadoop
-
0.19
.
1
-
core.jar WordCount.java
-
d
/
home
/
admin
/
WordCount
3. 编译完后在/home/admin/WordCount目录会发现三个class文件 WordCount.class,WordCount$Map.class,WordCount$Reduce.class。
cd 进入 /home/admin/WordCount目录,然后执行:
jar cvf WordCount.jar
*
.
class
就会生成 WordCount.jar 文件。
4. 构造一些输入数据
input1.txt和input2.txt的文件里面是一些单词。如下:
[admin@host WordCount]$ cat input1.txt
Hello, i love china
are you ok ?
[admin@host WordCount]$ cat input2.txt
hello, i love word
You are ok
Hello, i love china
are you ok ?
[admin@host WordCount]$ cat input2.txt
hello, i love word
You are ok
在hadoop上新建目录,和put程序运行所需要的输入文件:
hadoop fs
-
mkdir
/
tmp
/
input
hadoop fs - mkdir / tmp / output
hadoop fs - put input1.txt / tmp / input /
hadoop fs - put input2.txt / tmp / input /
hadoop fs - mkdir / tmp / output
hadoop fs - put input1.txt / tmp / input /
hadoop fs - put input2.txt / tmp / input /
5. 运行程序,会显示job运行时的一些信息。
[admin@host WordCount]$ hadoop jar WordCount.jar WordCount
/
tmp
/
input
/
tmp
/
output
10 / 09 / 16 22 : 49 : 43 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
10 / 09 / 16 22 : 49 : 43 INFO mapred.FileInputFormat: Total input paths to process : 2
10 / 09 / 16 22 : 49 : 43 INFO mapred.JobClient: Running job: job_201008171228_76165
10 / 09 / 16 22 : 49 : 44 INFO mapred.JobClient: map 0 % reduce 0 %
10 / 09 / 16 22 : 49 : 47 INFO mapred.JobClient: map 100 % reduce 0 %
10 / 09 / 16 22 : 49 : 54 INFO mapred.JobClient: map 100 % reduce 100 %
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Job complete: job_201008171228_76165
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Counters: 16
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: File Systems
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: HDFS bytes read = 62
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: HDFS bytes written = 73
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Local bytes read = 152
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Local bytes written = 366
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Job Counters
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Launched reduce tasks = 1
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Rack - local map tasks = 2
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Launched map tasks = 2
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Map - Reduce Framework
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Reduce input groups = 11
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Combine output records = 14
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Map input records = 4
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Reduce output records = 11
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Map output bytes = 118
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Map input bytes = 62
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Combine input records = 14
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Map output records = 14
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Reduce input records = 14
10 / 09 / 16 22 : 49 : 43 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
10 / 09 / 16 22 : 49 : 43 INFO mapred.FileInputFormat: Total input paths to process : 2
10 / 09 / 16 22 : 49 : 43 INFO mapred.JobClient: Running job: job_201008171228_76165
10 / 09 / 16 22 : 49 : 44 INFO mapred.JobClient: map 0 % reduce 0 %
10 / 09 / 16 22 : 49 : 47 INFO mapred.JobClient: map 100 % reduce 0 %
10 / 09 / 16 22 : 49 : 54 INFO mapred.JobClient: map 100 % reduce 100 %
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Job complete: job_201008171228_76165
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Counters: 16
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: File Systems
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: HDFS bytes read = 62
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: HDFS bytes written = 73
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Local bytes read = 152
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Local bytes written = 366
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Job Counters
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Launched reduce tasks = 1
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Rack - local map tasks = 2
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Launched map tasks = 2
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Map - Reduce Framework
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Reduce input groups = 11
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Combine output records = 14
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Map input records = 4
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Reduce output records = 11
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Map output bytes = 118
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Map input bytes = 62
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Combine input records = 14
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Map output records = 14
10 / 09 / 16 22 : 49 : 55 INFO mapred.JobClient: Reduce input records = 14
6. 查看运行结果
[admin@host WordCount]$ hadoop fs
-
ls
/
tmp
/
output
/
Found 2 items
drwxr - x --- - admin admin 0 2010 - 09 - 16 22 : 43 / tmp / output / _logs
- rw - r ----- 1 admin admin 102 2010 - 09 - 16 22 : 44 / tmp / output / part - 00000
[admin@host WordCount]$ hadoop fs - cat / tmp / output / part - 00000
Hello, 1
You 1
are 2
china 1
hello, 1
i 2
love 2
ok 1
ok ? 1
word 1
you 1
Found 2 items
drwxr - x --- - admin admin 0 2010 - 09 - 16 22 : 43 / tmp / output / _logs
- rw - r ----- 1 admin admin 102 2010 - 09 - 16 22 : 44 / tmp / output / part - 00000
[admin@host WordCount]$ hadoop fs - cat / tmp / output / part - 00000
Hello, 1
You 1
are 2
china 1
hello, 1
i 2
love 2
ok 1
ok ? 1
word 1
you 1
相关问答
更多-
如何一步一步学习java 知乎[2023-10-21]
兄弟连JavaEE战狼班: 第一阶段:Java语言基础 ★ Java语言基础 1、面向对象思维JAVASE 2、(类加载机制与反射,annotation,泛型,网络编程,多线程,IO,异常处理,常用API,面向对象,JAVA编程基础) 3、Java8新特性 第二阶段:数据库 ★ 数据库 1、Oracle(SQL语句、SQL语句原理、SQL语句优化、表、视图 2、序列、索引、Oracle数据字典、Oracle 数据库PL/SQL开发 3、数据库设计原则、 MySQL 、 JDBC 第三阶段:Web基础 ★ W ... -
oracle dba基础教程哪里有? 最好是一步一步教的![2022-07-15]
进入这个网址 http://www.verycd.com/topics/2767912/,把韩顺平老师的视频教程下下来学,很不错 -
安装mysql数据库 最后一步报错 怎么解决[2023-06-08]
最后一个有好几个选项的么。具体是哪一个错了? 说清楚点,好“对症下药” -
求一步一步教PHP留言板制作方法[2023-10-19]
1. 先设计数据表,一般留言板需要两个表:留言内容表、回复表 留言内容表:messages 字段如下: id 自动增加 contents 留言内容 messages_time 留言时间 回复表:reply 字段如下: id 自动增加 messages_id 关联messages表的id contents 回复的内容 reply_time 回复时间 2. 设计留言板页面及保存留言数据 大概代码如下: 3. 显示留言内容,遍历数据表 4. 回复的设计可以参照留言的设计 -
幼儿简笔画恐龙,一步一步的带步骤[2023-12-07]
先画身体,壮壮的! 还有‘恐龙'腿!要画粗一点哦。 紧跟着是尾巴!长一点。 添上眼睛和鼻子! 然后就是身上很有特色的恐龙背刺啦! 最后画上一棵树!不知它吃不吃树叶! -
制作网站的最后一步??[2021-07-28]
去申请个空间然后再买个域名,然后按提示做就行了 空间1多块钱1兆,域名100元1年,还有各种其它的,你去找电脑老师了解了解,也有专门给人做网页的,不过有点贵。 -
MySQL安装最后一步无法完成问题![2022-01-21]
MySQL也是很好安装的,就和APACHE的安装一样。但是大部分人包括我在内,都在配置的最后一步的第三小项目Start Service出现了错误。这十分令人懊恼。由于注册表的原因,我因此重装了系统,XP变身成为2003. MySQL之所以会出现这个错误,原因再简单不过——由于注册表里之前安装过的MySQL信息无法彻底删除导致新的MySQL不能启动。目前的解决方法似乎是将注册表内的旧信息删除,但是由于注册表的目录庞大,若要找到了再删除十分麻烦而且不一定能根除。我试过这一方法,却依然不能启动。最终只有重启了。不 ... -
oracle dba基础教程哪里有? 最好是一步一步教的![2022-03-07]
有一定的数据库基础吗?没有的话,建议从SQL语句学起。比较好的教材是Oracle OCP认证的《SQL and PL/SQL》。学习SQL的时候,尽可能坚持使用Oracle自带的工具:SQLPLUS。 有了一定的SQL基础后,就要尽可能的了解Oracle的体系结构,这就涉及到了Oracle管理的内容了。《Oracle10g OCP认证手册》这本书不错。 不过,如果是初学者的话,不建议自己去摸索,因为这样往往会如盲人摸象,不仅会事倍功半,而且会有一些错误的概念。你可以去CUUG报个DBA就业培训班进行学习, ... -
您可以使用可在emacs中运行的调试器ocamldebug来逐步执行代码。 You can step through the code using the debugger ocamldebug, which can be run in emacs.
-
一步安装程序(One Step Installer)[2022-04-29]
默认情况下做起来并不容易。 但是可以这样做,下面的代码生成了像这样的页面。 [Setup] AppName=Test AppVersion=1.5 DefaultDirName={code:AppDir} ;Disable all of the default wizard pages DisableDirPage=yes DisableProgramGroupPage=yes DisableReadyMemo=yes DisableReadyPage=yes DisableStartupPrompt=ye ...