wordcount java
2016-06-14 12:19
330 查看
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 {
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 {
System.out.println(System.getProperty("java.version"));
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
Job job = new Job(conf, "word count"); // 实例化一个Job对象
job.setJarByClass(WordCount.class); // 为Job对像指定运行时所需的类,表示告诉Hadoop集群,作业从哪个类开始运行
job.setMapperClass(TokenizerMapper.class); // 表示执行哪个类的map方法
job.setCombinerClass(IntSumReducer.class); // 这行语句,在Hadoop中,Combiner主要用于提升Hadoop的处理效率
job.setReducerClass(IntSumReducer.class); // 这行语句设置用于进行Reduce的类,告诉Hadoop集群执行哪个reduce函数
job.setOutputKeyClass(Text.class); // 表示MapReduce执行结束之后,将结果保存在HDFS中时,保存的数据类型。这里将结果的key以Text类型保存
job.setOutputValueClass(IntWritable.class); // value以IntWritable类型保存
FileInputFormat.addInputPath(job, new Path(otherArgs[0])); // 输入路径
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); // 输出路径
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 {
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 {
System.out.println(System.getProperty("java.version"));
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
Job job = new Job(conf, "word count"); // 实例化一个Job对象
job.setJarByClass(WordCount.class); // 为Job对像指定运行时所需的类,表示告诉Hadoop集群,作业从哪个类开始运行
job.setMapperClass(TokenizerMapper.class); // 表示执行哪个类的map方法
job.setCombinerClass(IntSumReducer.class); // 这行语句,在Hadoop中,Combiner主要用于提升Hadoop的处理效率
job.setReducerClass(IntSumReducer.class); // 这行语句设置用于进行Reduce的类,告诉Hadoop集群执行哪个reduce函数
job.setOutputKeyClass(Text.class); // 表示MapReduce执行结束之后,将结果保存在HDFS中时,保存的数据类型。这里将结果的key以Text类型保存
job.setOutputValueClass(IntWritable.class); // value以IntWritable类型保存
FileInputFormat.addInputPath(job, new Path(otherArgs[0])); // 输入路径
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); // 输出路径
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
相关文章推荐
- 史上最全最强SpringMVC详细示例实战教程
- JAVA 线程初步
- JAVA 事件监听
- Java - Java概述
- java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
- java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
- Java BigDecimal类的使用和注意事项
- Java字符串转码操作
- Maven 和 Eclipse 基础报错与解决方案
- eclipse插件实现Java调用 asmx 的Web Service
- JHTP小结_第二章_Java应用、输入、输出及运算符简介
- 【转载】javaMail发送邮件
- 【转载】JAVA如何把一个float四舍五入到小数点后2位,4位,或者其它指定位数.
- Struts.xml配置返回JSON数据
- Struts2 用 s:if test 判断属性和字符串相等时 注意双引号和单引号的使用
- 【转载】struts+spring+hibernate无法插入数据库数据的问题
- spring bean 加载顺序
- JAVA开发人员必备的免费JSP空间
- java数字类型转换
- 有关java对压缩文件的加密