运行hadoop的WordCount程序
程序员文章站
2022-06-14 14:56:56
...
源代码
hadoop配置成伪分布模式。本机上新建一个目录,如:~/code/hadoop/WordCount,编译WordCount.java。
编译后生成三个class文件,WordCount.class,WordCount$Map.class,WordCount$Reduce.class。
打包成jar文件,
新建input1.txt和input2.txt文件,输入一些单词。
在hdfs上新建目录,上传输入文件
运行程序
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);
}
}
hadoop配置成伪分布模式。本机上新建一个目录,如:~/code/hadoop/WordCount,编译WordCount.java。
javac -classpath /usr/local/hadoop-0.20.2/hadoop-0.20.2-core.jar WordCount.java
编译后生成三个class文件,WordCount.class,WordCount$Map.class,WordCount$Reduce.class。
打包成jar文件,
jar -cvf WordCount.jar *.class
新建input1.txt和input2.txt文件,输入一些单词。
在hdfs上新建目录,上传输入文件
hadoop fs -mkdir /tmp/input
hadoop fs -put input1.txt /tmp/input/
hadoop fs -put input2.txt /tmp/input/
运行程序
hadoop jar WordCount.jar WordCount /tmp/input /tmp/output
推荐阅读
-
用visual studio 2010 打开winform程序时无法运行的解决方法
-
在Winform程序运行时启动Cmd命令行显示日志信息的设置方法
-
Visual Studio寻找C#程序必要的运行库文件
-
安装程序不能验证Update.inf文件的完整性,请确定加密服务正在此计算机上运行
-
一个可以让.net程序在非WIN平台上运行的软件Mono
-
.Net WInform开发笔记(二)Winform程序运行结构图及TCP协议在Winform中的应用
-
java怎么用cmd运行(简单的java程序代码)
-
优酷客户端提示:程序运行错误 是否重试的解决办法
-
linux nohup命令使程序在后台运行的方法
-
win0如何运行安卓apk程序?win0运行安卓apk程序的方法