Mapreduce的使用

创建三个类:

复制代码
package com.example.mapreduce;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 java.io.IOException;
public class WordCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        //设置用户名:
        System.setProperty("HADOOP_USER_NAME", "root");
        //1.获取job对象
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hadoop100:8020");

        Job job = Job.getInstance(conf);
        //2.关联啊本地Driver类的jar
        job.setJarByClass(WordCountDriver.class);
        //3.关联map和reduce
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        //4.设置map的输出kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        //5.设置map的输出kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        //6.设置输入数据和输出结果的地址
        //FileInputFormat.setInputPaths(job, new Path("E\\cinput"));
        //FileOutputFormat.setOutputPath(job, new Path("E\\output10"));
        FileInputFormat.setInputPaths(job, new Path("/cinput"));
        FileOutputFormat.setOutputPath(job, new Path("/output10"));

        //7.提交job
        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }
}

package com.example.mapreduce;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.io.Text;

import java.io.IOException;
//1.继承 hadoop的map重写
//2.重写map方法
public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    //每一行的文本内容,使用空格做拆分,得到一个列表
        String[] words = value.toString().split(" ");
    //对每一个单词,把它当做key,并设置value为1
        for (String word : words) {
            context.write(new Text(word), new LongWritable(1));
        }
    }
}

package com.example.mapreduce;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.io.Text;
import java.io.IOException;
//继承hadoop的reducer类
//重写reduce方法
public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
       //对value中的值做累加求和
        long sum = 0;
        for (LongWritable value : values) {
            sum += value.get();
        }
        //将结果输出
        context.write(key, new LongWritable(sum));
    }
}
相关推荐
一只小bit3 小时前
MySQL 索引:从聚簇到普通索引,如何加快查询效率?
数据库·mysql·oracle
猫猫姐姐5 小时前
Flink基于Paimon的实时湖仓解决方案的演进
大数据·flink·湖仓一体
洛克大航海5 小时前
解锁 PySpark SQL 的强大功能:有关 App Store 数据的端到端教程
linux·数据库·sql·pyspark sql
XueminXu6 小时前
ClickHouse数据库的表引擎
数据库·clickhouse·log·表引擎·mergetree·special·integrations
冒泡的肥皂7 小时前
MVCC初学demo(二
数据库·后端·mysql
代码程序猿RIP7 小时前
【Redis 】Redis 详解以及安装教程
数据库·etcd
小生凡一7 小时前
redis 大key、热key优化技巧|空间存储优化|调优技巧(一)
数据库·redis·缓存
oe10197 小时前
好文与笔记分享 A Survey of Context Engineering for Large Language Models(上)
数据库·笔记·语言模型·agent·上下文工程
小马哥编程7 小时前
【软考架构】案例分析-对比MySQL查询缓存与Memcached
java·数据库·mysql·缓存·架构·memcached
一 乐7 小时前
高校后勤报修系统|物业管理|基于SprinBoot+vue的高校后勤报修系统(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·毕设