当前位置: 首页 > news >正文

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));
    }
}

http://www.dtcms.com/a/106654.html

相关文章:

  • 深入理解归并排序:分治艺术的经典实践
  • 【AI产品分享】面向图片的原始位置翻译功能
  • Redisson中BitMap位图的基本操作
  • CORS与OPTIONS请求
  • 蓝桥杯 游戏 6251 单调队列
  • .NET 创建MCP使用大模型对话
  • 【计网速通】计算机网络核心知识点与高频考点——数据链路层(二)
  • kafka消息可靠性传输语义
  • 大语言模型开发框架——LangChain
  • 使用PyTorch实现LeNet-5并在Fashion-MNIST数据集上训练
  • 【Linux】内核驱动学习笔记(二)
  • 基于Spring AI与Ollama构建本地DeepSeek对话机器人
  • 数据库分库分表中间件及对比
  • ensp 网络模拟器 思科华为基于VLANIF的公司网络搭建
  • 2025.4.2总结
  • Go语言GC:三色标记法工程启示|Go语言进阶(3)
  • K-means算法
  • 从零搭建微服务项目Pro(第7-1章——分布式雪花算法)
  • cmake(11):list 选项 排序 SORT,定义宏 add_definitions,cmake 里预定义的 8 个宏
  • Git 命令大全:通俗易懂的指南
  • 基于大模型预测风湿性心脏病二尖瓣病变的多维度诊疗研究报告
  • 内网隔离环境下Java实现图片预览的三大解决方案
  • 【Django开发】前后端分离django美多商城项目第15篇:商品搜索,1. Haystack介绍和安装配置【附代码文档】
  • 从 ZStack 获取物理机与云主机信息并导出 Excel 文件
  • visual studio 2022的windows驱动开发
  • C# System.Text.Json 中 JsonIgnoreCondition 使用详解
  • Linux2 CD LL hostnamectl type mkdir dudo
  • 跨系统平台实践:在内网自建kylin服务版系统yum源
  • 面基JavaEE银行金融业务逻辑层处理金融数据类型BigDecimal
  • AI提示词:好评生成器