hadoop-mapreduce-(1)-统计单词数量

编写map程序

package com.cvicse.ump.hadoop.mapreduce.map;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {

        String line = value.toString();
        String[] words = line.split(" ");
        for(String word:words){
            context.write(new Text(word), new IntWritable(1));
        }

    }

}

编写reduce程序

package com.cvicse.ump.hadoop.mapreduce.reduce;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WordCountReduce extends
        Reducer<Text, IntWritable, Text, IntWritable> {

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context)
            throws IOException, InterruptedException {

        Integer count = 0;
        for(IntWritable value:values){
            count+=value.get();
        }

        context.write(key, new IntWritable(count));

    }

}

编写main函数

package com.cvicse.ump.hadoop.mapreduce;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import com.cvicse.ump.hadoop.mapreduce.map.WordCountMap;
import com.cvicse.ump.hadoop.mapreduce.reduce.WordCountReduce;

public class WordCount {

    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();

        Job job = Job.getInstance(conf,"wordCount");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCountMap.class);
        job.setReducerClass(WordCountReduce.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        boolean bb = job.waitForCompletion(true);
        if(!bb){
            System.out.println("wrodcount task fail!");
        }else{
            System.out.println("wordcount task success!");
        }

    }

}

把wordcount.txt放在hdfs的/dyh/data/input/目录下

执行:hadoop jar hdfs.jar com.cvicse.ump.hadoop.mapreduce.WordCount /dyh/data/input/wordcount.txt /dyh/data/output/1

时间: 2024-08-30 14:44:44

hadoop-mapreduce-(1)-统计单词数量的相关文章

Hadoop Mapreduce 案例 统计手机流量使用情况

需要被统计流量的文件内容如下: 1363157985066 13726230503 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 24 27 2481 24681 2001363157995052 13826544101 5C-0E-8B-C7-F1-E0:CMCC 120.197.40.4 4 0 264 0 2001363157991076 13926435656 20-10-7A-28-CC-0A:CMCC 120.196.1

Java的TreeMap统计单词数量

使用TreeMap统计单词个数,并输出单词和书目,单词按升序排列 TreeMap的特点是无重复元素,且元素的key值既可以按默认的Comparable接口排序也可以按Comparator比较器排序,为TreeMap设计一个比较器,此比较器要实现Comparator接口 //可以扩展到从文件中或者从控制台输入单词,来统计,只需要加入流就行了 import java.util.Map; import java.util.Set; import java.util.TreeMap; public cl

go语言小练习——给定英语文章统计单词数量

给定一篇英语文章,要求统计出所有单词的个数,并按一定次序输出.思路是利用go语言的map类型,以每个单词作为关键字存储数量信息,代码实现如下: 1 package main 2 3 import ( 4 "fmt" 5 "sort" 6 ) 7 8 func wordCounterV1(str string) { 9 /*定义变量*/ 10 stringSlice := str[:] 11 temp := str[:] 12 wordStatistic := mak

Storm监控文件夹变化 统计文件单词数量

监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

Hadoop读书笔记(五)MapReduce统计单词demo

Hadoop读书笔记(一)Hadoop介绍:http://blog.csdn.net/caicongyang/article/details/39898629 Hadoop读书笔记(二)HDFS的shell操作:http://blog.csdn.net/caicongyang/article/details/41253927 Hadoop读书笔记(三)Java API操作HDFS:http://blog.csdn.net/caicongyang/article/details/41290955

Hadoop实例之利用MapReduce实现Wordcount单词统计 (附源代码)

大致思路是将hdfs上的文本作为输入,MapReduce通过InputFormat会将文本进行切片处理,并将每行的首字母相对于文本文件的首地址的偏移量作为输入键值对的key,文本内容作为输入键值对的value,经过在map函数处理,输出中间结果<word,1>的形式,并在reduce函数中完成对每个单词的词频统计.整个程序代码主要包括两部分:Mapper部分和Reducer部分. Mapper代码 public static class doMapper extends Mapper<O

Hadoop:MapReduce编程-WordCount统计单词个数-eclipse-java环境

之前习惯用hadoop streaming环境编写python程序,下面总结编辑java的eclipse环境配置总结,及一个WordCount例子运行. 一 下载eclipse安装包及hadoop插件 1去官网下载linux版本的eclipse安装包(或者在本人为了大家方便下载,上传到了csdn下载,网址: 2下载插件:hadoop-eclipse-plugin-2.6.0.jar 二 安装elicpse及hadoop插件 1 把eclipse解压到路径 /user/local/eclipse

【hadoop】 3002-mapreduce程序统计单词个数示例

一.新建文本文件wordcount.txt,并上传至hdfs服务器上 [[email protected] HDFSdemo]$ hadoop fs -cat /wc/wordcount.txt hello world hello China hello wenjie hello USA hello China hello China hello Japan [[email protected] HDFSdemo]$ hadoop fs -cat /wc/wordcount1.txt hello

Hadoop MapReduce编程 API入门系列之统计学生成绩版本2(十八)

不多说,直接上代码. 统计出每个年龄段的 男.女 学生的最高分 这里,为了空格符的差错,直接,我们有时候,像如下这样的来排数据. 代码 package zhouls.bigdata.myMapReduce.Gender; import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;import org.apache.hadoop.fs