mapreduce实现一个简单的单词计数的功能。

一,准备工作:eclipse 安装hadoop 插件:

下载相关版本的hadoop-eclipse-plugin-2.2.0.jar到eclipse/plugins下。

二,实现:

新建mapreduce project

map 用于分词,reduce计数。

package tank.demo;

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.LongWritable;
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; /**
* @author tank
* @date:2015年1月5日 上午10:03:43
* @description:记词器
* @version :0.1
*/ public class WordCount {
public static class TokenizerMapper extends 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, 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();
if (args.length != 2) {
System.err.println("Usage: wordcount ");
System.exit(2);
}
Job job = new Job(conf, "word count");
//主类
job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class);
job.setReducerClass(IntSumReducer.class);
//map输出格式
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.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);
} }

打包world-count.jar

三,准备输入数据

hadoop fs -mkdir /user/hadoop/input//建好输入目录

//随便写点数据文件

echo hello my hadoop this is my first application>file1

echo hello world my deer my applicaiton >file2

//拷贝到hdfs中

hadoop fs -put file* /user/hadoop/input

hadoop fs -ls /user/hadoop/input //查看

四,运行

上传到集群环境中:

hadoop jar world-count.jar  WordCount input output

截取一段输出如:

15/01/05 11:14:36 INFO mapred.Task: Task:attempt_local1938802295_0001_r_000000_0 is done. And is in the process of committing
15/01/05 11:14:36 INFO mapred.LocalJobRunner:
15/01/05 11:14:36 INFO mapred.Task: Task attempt_local1938802295_0001_r_000000_0 is allowed to commit now
15/01/05 11:14:36 INFO output.FileOutputCommitter: Saved output of task 'attempt_local1938802295_0001_r_000000_0' to hdfs://192.168.183.130:9000/user/hadoop/output/_temporary/0/task_local1938802295_0001_r_000000
15/01/05 11:14:36 INFO mapred.LocalJobRunner: reduce > reduce
15/01/05 11:14:36 INFO mapred.Task: Task 'attempt_local1938802295_0001_r_000000_0' done.
15/01/05 11:14:36 INFO mapreduce.Job: Job job_local1938802295_0001 running in uber mode : false
15/01/05 11:14:36 INFO mapreduce.Job:  map 100% reduce 100%
15/01/05 11:14:36 INFO mapreduce.Job: Job job_local1938802295_0001 completed successfully
15/01/05 11:14:36 INFO mapreduce.Job: Counters: 32
        File System Counters
                FILE: Number of bytes read=17706
                FILE: Number of bytes written=597506
                FILE: Number of read operations=0
                FILE: Number of large read operations=0
                FILE: Number of write operations=0
                HDFS: Number of bytes read=205
                HDFS: Number of bytes written=85
                HDFS: Number of read operations=25
                HDFS: Number of large read operations=0
                HDFS: Number of write operations=5
        Map-Reduce Framework
                Map input records=2
                Map output records=14
                Map output bytes=136
                Map output materialized bytes=176
                Input split bytes=232
                Combine input records=0
                Combine output records=0
                Reduce input groups=10
                Reduce shuffle bytes=0
                Reduce input records=14
                Reduce output records=10
                Spilled Records=28
                Shuffled Maps =0
                Failed Shuffles=0
                Merged Map outputs=0
                GC time elapsed (ms)=67
                CPU time spent (ms)=0
                Physical memory (bytes) snapshot=0
                Virtual memory (bytes) snapshot=0
                Total committed heap usage (bytes)=456536064
        File Input Format Counters
                Bytes Read=80
        File Output Format Counters
                Bytes Written=85

查看输出目录下的文件

[hadoop@tank1 ~]$ hadoop fs -cat /user/hadoop/output/part-r-00000
applicaiton     1
application     1
deer    1
first   1
hadoop  1
hello   2
is      1
my      4
this    1
world   1

已经正确统计出单词数量!

hadoop mapreduce 基础实例一记词的更多相关文章

  1. Hadoop 综合揭秘——MapReduce 基础编程(介绍 Combine、Partitioner、WritableComparable、WritableComparator 使用方式)

    前言 本文主要介绍 MapReduce 的原理及开发,讲解如何利用 Combine.Partitioner.WritableComparator等组件对数据进行排序筛选聚合分组的功能.由于文章是针对开 ...

  2. Hadoop学习基础之三:MapReduce

    现在是讨论这个问题的不错的时机,因为最近媒体上到处充斥着新的革命所谓“云计算”的信息.这种模式需要利用大量的(低端)处理器并行工作来解决计算问题.实际上,这建议利用大量的低端处理器来构建数据中心,而不 ...

  3. [Hadoop in Action] 第4章 编写MapReduce基础程序

    基于hadoop的专利数据处理示例 MapReduce程序框架 用于计数统计的MapReduce基础程序 支持用脚本语言编写MapReduce程序的hadoop流式API 用于提升性能的Combine ...

  4. Hadoop MapReduce执行过程实例分析

    1.MapReduce是如何执行任务的?2.Mapper任务是怎样的一个过程?3.Reduce是如何执行任务的?4.键值对是如何编号的?5.实例,如何计算没见最高气温? 分析MapReduce执行过程 ...

  5. hadoop之mapreduce编程实例(系统日志初步清洗过滤处理)

    刚刚开始接触hadoop的时候,总觉得必须要先安装hadoop集群才能开始学习MR编程,其实并不用这样,当然如果你有条件有机器那最好是自己安装配置一个hadoop集群,这样你会更容易理解其工作原理.我 ...

  6. MongoDB:MapReduce基础及实例

    背景 MapReduce是个非常灵活和强大的数据聚合工具.它的好处是可以把一个聚合任务分解为多个小的任务,分配到多服务器上并行处理. MongoDB也提供了MapReduce,当然查询语肯定是Java ...

  7. 【Hadoop离线基础总结】MapReduce增强(下)

    MapReduce增强(下) MapTask运行机制详解以及MapTask的并行度 MapTask运行流程 第一步:读取数据组件InputFormat(默认TextInputFormat)会通过get ...

  8. 【Hadoop离线基础总结】MapReduce增强(上)

    MapReduce增强 MapReduce的分区与reduceTask的数量 概述 MapReduce当中的分区:物以类聚,人以群分.相同key的数据,去往同一个reduce. ReduceTask的 ...

  9. Hadoop(十五)MapReduce程序实例

    一.统计好友对数(去重) 1.1.数据准备 joe, jon joe , kia joe, bob joe ,ali kia, joe kia ,jim kia, dee dee ,kia dee, ...

随机推荐

  1. chrome实用快捷键速记

    标签页和窗口快捷键 操作 快捷键 打开新窗口 Ctrl + n 无痕模式下打开新窗口 Ctrl + Shift + n 打开新的标签页,并跳转到该标签页 Ctrl + t 重新打开最后关闭的标签页,并 ...

  2. CodeCraft-19 and Codeforces Round #537 Div. 2

    D:即有不超过52种物品,求容量为n/2的有序01背包方案数.容易想到设f[i][j]为前i种物品已用容量为j的方案数,有f[i][j]=f[i-1][j-a[i]]*C(n/2-j+a[i],a[i ...

  3. 一个模拟——抢票部分功能的 简单版(主要实例化一下 Lock 的使用)

    """ 抢票! 多个用户在同时读写同一个数据 """ from multiprocessing import Process,Lock im ...

  4. Codeforces980 D. Perfect Groups

    传送门:>Here< 题目大意:先抛出了一个问题——“已知一个序列,将此序列中的元素划分成几组(不需要连续)使得每一组中的任意两个数的乘积都是完全平方数.特殊的,一个数可以为一组.先要求最 ...

  5. Going Home POJ - 2195(费用流)

    就是一个简单题 四个月前a的一道题,今天又看到了,再a一遍吧. 好吧 我想多了 用了bfs求最短路  其实不用的 因为没有障碍物 #include <iostream> #include ...

  6. Ionic生命周期与注意点

    需要注意的地方是:在走页面的生命周期以前,会先走构造方法 构造方法只走一次,除非再次创建这个页面.所以如果先push了一个新页面,然后再调用pop()返回到之前的页面,那么是不会走构造方法里面的方法的 ...

  7. php+redis配置

    系统环境: win10+phpstudy+lamp 安装扩展 php5.6.4 =>下载地址:http://windows.php.net/downloads/pecl/releases/red ...

  8. 概念数据模型CDM基础

    概念数据模型CDM 概念数据模型是设计数据库不可或缺的一步,是整个数据库设计的关键,CDM的主要作用如下: 1)能够真实地模拟真实世界,是需求分析人员和数据库设计人员沟通的桥梁.2)将系统需求分析得到 ...

  9. python的生成器与迭代器和可迭代对象

    来简单的说下python中的生成器和可迭代对象以及迭代器的问题.只是简单地记录一下并不涉及太深入的内容. 首先来说一下什么是生成器,先看下面的代码: #_*_ coding:utf-8 _*_ res ...

  10. BZOJ2961 共点圆[CDQ分治]

    题面 bzoj 其实就是推一下圆的式子 长成这个样子 假设要查询的点是(x, y) 某个圆心是(p, q) \((x - p)^2 + (y - q)^2 \leq p^2 + q^2\) 变成 \( ...