最全wordcount

World Count

一,hadoop API

1,在HDFS上创建/wordcount/input目录 ,并把word.txt文件上传到该目录下。

hadoop fs -mkdir -p /wordcount/input
hadoop fs -put /root/word.txt /wordcount/input

2,进入$HADOOP_HOME/share/hadoop/mapreduce/目录下,查看文件
cd $HADOOP_HOME/share/hadoop/mapreduce/

3,直接使用hadoop-mapreduce-examples-2.7.7.jar 包进行wordcount计算
hadoop jar hadoop-mapreduce-examples-2.7.7.jar wordcount \
/wordcount/input/word.txt /wordcount/output

4,查看结果数据

hadoop fs -cat /wordcount/output/part-r-00000

二,spark scala

1,首先进入命令行输入spark-shell
spark-shell
2,单词统计,注意路径是hadoop上的
val rdd1 = sc.textFile("/wordcount/input/word.txt")
val rdd2 = rdd1.flatMap(line => line.split(" "))
val rdd3 = rdd2.map(word => (word,1))
val rdd4 = rdd3.reduceByKey(_ + _)
rdd4.collect


三,word count MR 程序

1 Driver 代码
package com.hdfs.mr;

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

import java.io.IOException;


public class WordDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        String[] args1 = new String[2];
        args1[0]= "d:/hello.txt";
        args1[1]="d:/output10";
        Configuration conf = new Configuration();

        //1, 获取job对象
        Job job = Job.getInstance(conf);

        //2, 设置JAR存储位置
        job.setJarByClass(WordDriver.class);

        //3, 关联Map和Reducer类
        job.setMapperClass(WordMapper.class);
        job.setReducerClass(WordReducer.class);

        //4, 设置Maper阶段输出数据的Key和Value类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //5, 设置最终输出阶段的Key和Value类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //6, 设置输入和输出路径
        FileInputFormat.setInputPaths(job,new Path(args1[0]));
        FileOutputFormat.setOutputPath(job,new Path(args1[1]));

        //7, 提交JOB
        job.waitForCompletion(true);
    }
}

2 Mapper 代码
package com.hdfs.mr;



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

import java.io.IOException;

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

    Text k = new Text();
    IntWritable v = new IntWritable(1);

    @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) {
            k.set(word);
            context.write(k,v);
        }
    }

}

3 Reducer代码
package com.hdfs.mr;

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

import java.io.IOException;
import java.util.Iterator;

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

    IntWritable v = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;

        Iterator it = values.iterator();

        while (it.hasNext()){
            sum += Integer.parseInt(it.next().toString());
        }
        v.set(sum);

        context.write(key,v);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醉卧千山下,诗酒趁年华。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值