package com.hadoop.sample;
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 {
//继承mapper接口,设置map的输入类型为<Object,Text>
//输出类型为<Text,IntWritable>
public static class Map extends Mapper<Object,Text,Text,IntWritable>{
//one表示单词出现一次
private static IntWritable one = new IntWritable(1);
//word存储切下的单词
private Text word = new Text();
public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
//对输入的行切词
StringTokenizer st = new StringTokenizer(value.toString());
while(st.hasMoreTokens()){
word.set(st.nextToken());//切下的单词存入word
context.write(word, one);
}
}
}
//继承reducer接口,设置reduce的输入类型<Text,IntWritable>
//输出类型为<Text,IntWritable>
public static class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{
//result记录单词的频数
private static IntWritable result = new IntWritable();
public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException,InterruptedException{
int sum = 0;
//对获取的<key,value-list>计算value的和
for(IntWritable val:values){
sum += val.get();
}
//将频数设置到result
result.set(sum);
//收集结果
context.write(key, result);
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Configuration conf = new Configuration();
//检查运行命令
String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
if(otherArgs.length != 2){
System.err.println("Usage WordCount <int> <out>");
System.exit(2);
}
//配置作业名
Job job = new Job(conf,"word count");
//配置作业各个类
job.setJarByClass(WordCount.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
分享到:
相关推荐
用java的MapReduce写了个demo,用于计算文档单词出现个数
hadoop中的demo,wordcount列子用到的JAR包 用法: # 在容器里运行WordCount程序,该程序需要2个参数...hadoop jar hadoop-mapreduce-examples-2.7.1-sources.jar org.apache.hadoop.examples.WordCount input output
(3)写wordcount代码并把代码生成jar包 (4)运行命令 (1):把linus下的文件放到hdfs上 (2):运行MapReduce (5):查看运行结果 (6)网页上查看MapReduce任务 2.矩阵相乘实验(matrix) (1)写matrix代码并把...
Hadoop 用mapreduce实现Wordcount实例,绝对能用
Python 中的 Hadoop Mapreduce 示例 python 中的几个 Mapreduce 示例以及有关运行它们的文档! 运行代码的步骤 文件夹结构 假定文件存储在 Linux 操作系统中的给定位置。 这只是一个示例说明,实际上位置并不重要。 ...
Hadoop MapReduce示例 hdfs dfs -rm -r / user / huangzehai / wordcount / output hadoop jar target / hadoop-example-1.0-SNAPSHOT.jar huangzehai.mr.wordcout.WordCount / user / huangzehai / wordcount / ...
wordcount-mapreduce Hadoop MapReduce WordCount 示例应用程序
<groupId>com.hadoop.mapreduce</groupId> <artifactId>wordcount <version>0.0.1-SNAPSHOT <packaging>jar <name>wordcount <url>http://maven.apache.org</url> <project.build.sourceEncoding>UTF-8 ...
最近在研究hadoop与mapReduce,网上教程只有个wordcount程序示范,太简单,故写了个相对复杂点的涉及到多个文件之间同时运算的矩阵乘法的代码用于实验与测试,上传供大家学习与参考。 调用方法: 执行:hadoop jar ...
包org.dan.mr.wordcount MapReduce单词计数 包org.dan.mr.flowsum MapReduce流量统计 包org.dan.mr.flowsumsort MapReduce流量统计,按总流量排序 包org.dan.mr.order_pro MapReduce实现订单信息和产品信息的join...
hadoop 框架下 mapreduce源码例子 wordcount ,eclipse下,hadoop 2.2 可以运行
学习Hadoop和Spark 内容 这是我在Apache Hadoop和Apache Spark上的LinkedIn学习课程的配套资源库。 :elephant: 1.学习Hadoop--主要使用GCP Dataproc -用于运行Hadoop及相关库(即Hive,Pig,Spark ...)的工作负载...
内容概要: mr执行笔记; mapreduce框架的规范; wc流程.xls; wordcount的伪代码; yarn提交job的源码流程; YARN中提交job的详细流程; 打开流的关键代码; 打开流的调用流程; 日志格式;
使用eclipse和maven的Hadoop Mapreduce示例:日志文件分析:本文简要概述了如何使用Eclipse和maven应用map reduce来计算每天发生的日志消息类型的数量。 先决条件: • 虚拟机上的Hadoop 设置。 • Java 版本 6 ...
3. 查看 Hadoop 自带的 MR-App 单词计数源代码 WordCount.java,在 Eclipse 项目 MapReduceExample 下建立新包 com.xijing.mapreduce,模仿内置的 WordCount 示例,自己编写一个 WordCount 程序,最后打包成 JAR ...
myeclipse +maven 搭建的hadoop mapreduce 例子项目,运行了单机wordcount
[hadoop@test Desktop]$ hadoop jar wordcount.jar \ > /user/hadoop/input/file* /user/hadoop/output 18/05/25 19:51:32 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032 18/05/25 19:51:32...
Hadoop的词频统计源代码WordCount
Ubuntu环境下新手学习Hadoop,从配置Hadoop开始,实现mapreduce,过程详细,也是自己学习过程的一个记录。
WordCount_MapReduce 在 Hadoop 上运行的 MapReduce 程序