MapReduce原理及编程(java实现Map、Reduce)

Hadoop架构

HDFS - 分布式文件系统
MapReduce - 分布式计算框架
YARN - 分布式资源管理系统
Common

什么是MapReduce?
MapReduce是一个分布式计算框架
它将大型数据操作作业分解为可以跨服务器集群并行执行的单个任务。
起源于Google
适用于大规模数据处理场景
每个节点处理存储在该节点的数据
每个job包含Map和Reduce两部分

MapReduce的设计思想
分而治之–
简化并行计算的编程模型
构建抽象模型:Map和Reduce–
开发人员专注于实现Mapper和Reducer函数
隐藏系统层细节–
开发人员专注于业务逻辑实现

MapReduce特点

优点–
易于编程
可扩展性
高容错性
高吞吐量
不适用领域–
难以实时计算
不适合流式计算

MapReduce实现WordCount

在这里插入图片描述
对于MapReduce的原理可以参考此处,分析很到位
——————————————————————————

MapReduce执行过程

数据定义格式
map: (K1,V1) → list (K2,V2)
reduce: (K2,list(V2)) → list (K3,V3)
MapReduce执行过程
Mapper
Combiner
Partitioner
Shuffle and Sort
Reducer

在这里插入图片描述

WordCount代码

创建项目使用模板 maven-archetype-quickstart
项目执行前需要在window环境中配置hadoop
或将程序打胖包置入安装了hadoop的linux环境中运行

pom文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>mymr</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>mymr</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>2.6.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>2.6.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.6.0</version>
    </dependency>



  </dependencies>

  <build>

      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3.2</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
              <manifest>
                <mainClass>services.wc.MyDriver</mainClass>
              </manifest>
            </archive>
          </configuration>
          <executions>
            <execution>
              <id>make-assembly</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>

  </build>
</project>

模拟实现Map

package services.wc;

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

import java.io.IOException;

public class MyMapper extends Mapper<LongWritable, Text,Text,LongWritable> {
    private LongWritable one = new LongWritable(1);
    private Text word = new Text();


    @Override
    protected void map(LongWritable key, Text value, Context context) throws  IOException, InterruptedException {
        String[] wds = value.toString().split(" ");
        for (String word : wds) {
            Text wd = new Text(word);
            context.write(wd,one);
        }
    }
}

尤其需要注意,Text类是org.apache.hadoop.io.Text,此处容易导错包,易造成不出结果

模拟实现Reduce

package services.wc;

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

import java.io.IOException;

public class MyReduce extends Reducer<Text, LongWritable,Text,LongWritable> {
    private LongWritable res = new LongWritable();

    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        long ressum = 0;
        for (LongWritable one : values) {
            ressum+=one.get();
        }
        res.set(ressum);
        context.write(key,res);
    }
}

模拟任务启动

package services.wc;

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.File;


public class MyDriver {
    public static void main(String[] args) throws Exception {
        File file=new File("d://eee");
        if (file.exists()){
            file.delete();
        }
        Configuration conf = new Configuration();
		//解决filesystem默认配置无法正常读取文件
        conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
        conf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());
        //准备一个空任务
        Job job = Job.getInstance(conf);
        //设置该任务的主启动类
        job.setJarByClass(MyDriver.class);
        //设置任务的输入数据源
        FileInputFormat.addInputPath(job,new Path("D://study/abc.txt"));
        //设置你的Mapper任务类
        job.setMapperClass(MyMapper.class);
        //设置Mapper任务类的输出数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        //设置你的Reduce任务类
        job.setReducerClass(MyReduce.class);
        //设置Reduce任务类的输出数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        //设置任务的输出数据目标
        FileOutputFormat.setOutputPath(job,new Path("d://eee"));
        //启动任务并执行
        job.waitForCompletion(true);
    }
}

程序正常执行结束后,会在目标文件夹中生成四个文件(如下图),最终wordcount结果存储在part-r-00000中
在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的 Java 实现MapReduce 去重程序。 首先,让我们定义一些术语: - Mapper:将输入数据分成不同的键值对。 - Reducer:将来自 Mapper 的键值对进行合并和处理。 - InputFormat:用于将输入文件切分成小块供 Mapper 处理的格式。 - OutputFormat:将 Reduce 输出的结果转换成最终输出文件的格式。 现在,让我们来看一下具体的实现步骤: 1. Mapper 阶段 Mapper 阶段的任务是将输入数据分成键值对。在本例中,我们将输入文件的每一行作为一个键,将值设置为一个空字符串。 ```java public class DedupMapper extends Mapper<LongWritable, Text, Text, Text> { private Text outkey = new Text(); private Text outvalue = new Text(""); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { outkey.set(value); context.write(outkey, outvalue); } } ``` 2. Reducer 阶段 Reducer 阶段的任务是将 Mapper 阶段产生的键值对进行合并和去重。在本例中,我们只需将键输出到输出文件中,而值可以忽略,因为我们只需要去重。 ```java public class DedupReducer extends Reducer<Text, Text, Text, NullWritable> { public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { context.write(key, NullWritable.get()); } } ``` 3. 定义 InputFormat 和 OutputFormat 我们需要定义一个 TextInputFormat 类来将输入文件切分成小块,并且定义一个 TextOutputFormat 类将 Reduce 输出的结果转换成最终输出文件的格式。 ```java public class Dedup { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "deduplication"); job.setJarByClass(Dedup.class); job.setMapperClass(DedupMapper.class); job.setReducerClass(DedupReducer.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(NullWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); } } ``` 这就是一个简单的 Java 实现MapReduce 去重程序。希望能对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值