栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

2021.10.27MapReduce计数

2021.10.27MapReduce计数

WordCountMapper

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 WordCountMapper extends Mapper {
    Text txt =new Text();
    IntWritable intWritable=new IntWritable();

    


    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        System.out.println("WordCountMapper key:"+key+"  value:"+value);
        String[] words=value.toString().split(" ");
        for(String word :
                    words){

            txt.set(word);
            intWritable.set(1);
            context.write(txt,intWritable);

        }
    }
}
WordCountReduce

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

import java.io.IOException;

public class WordCountReduce extends Reducer {

    @Override
    protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
        int count=0;
        for (IntWritable intWritable:
             values) {
            count+=intWritable.get();

        }
        LongWritable longWritable=new LongWritable(count);
        System.out.println("WordCountReduce key:"+key+ "value:"+longWritable.get());
        context.write(key,longWritable);
    }
}
WordCountDriver

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.mapred.FileInputFormat;
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 WordCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf =new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(WordCountDriver.class);
        //配置当前job 指定mapper类
        job.setMapperClass(WordCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //配置当前job 执行的reduce类
        job.setReducerClass(WordCountReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);


        //指定读取文件路径
//        Path path =new Path("E:\hadoopstu\in\demo1");
//        Path path =new Path("hdfs://linux01:9000/input");
        Path path=new Path(args[0]);
        FileInputFormat.setInputPaths(job,path);


        //指定执行任务完成后的路径
//        Path pathout =new Path("E:\hadoopstu\in\out1");
//        Path pathout =new Path("hdfs://linux01:9000/output1");
        Path pathout =new Path(args[1]);
        FileSystem fileSystem=FileSystem.get(pathout.toUri(),conf);

        if(fileSystem.exists(pathout)){
            fileSystem.delete(pathout,true);
        }
        FileOutputFormat.setOutputPath(job,pathout);

        job.waitForCompletion(true);
        System.out.println(args[0]+" "+args[1]);



    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/389527.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号