MapReduce之过滤(二)

本文介绍如何使用MapReduce实现简单随机抽样,通过在mapper阶段生成随机数并比较阈值,实现从大数据集中按固定概率抽取样本。适用于数据处理与分析场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MapReduce之简单随机抽样

这篇博客和上一篇有点类似,模式描述和应用场景都和MapReduce之过滤(一)类似

简单随机抽样

从一个较大的数据集中以一定概率抓取一个数据集,其中每条记录均有相同的抽取概率

问题描述

一个较大的数据集中以一定概率抓取一个数据集

样例输入

MapReduce之过滤(一)数据类似

样例输出

数据集随机生成,可能存在不同

mapper阶段任务

在map函数中,简单的生成一个(0,1)之间的随机数并与之指定的阈值进行比较,来确定是否保存数据

mapper阶段编码如下
public static class SRSMapper extends Mapper<Object,Text,NullWritable,Text>{
        private Random rands=new Random();
        //在这里假设阈值为0.3
        private Double percentage=0.3;
        public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
            if(rands.nextDouble()<percentage){
                context.write(NullWritable.get(),value);
            }
        }
    }

reducer阶段

无具体任务,仅仅是简单的从文件系统中将抓取的数据抓取出来,可以用reduce或者使用hadoop fd -cat收集,也可以控制台打印(意义不大)

public static class SRSReduce extends Reducer<NullWritable,Text,Text,NullWritable>{
        public void reducer(NullWritable key, Iterator<Text> values,Context context) throws IOException,InterruptedException{
            while(values.hasNext()){
                String value=values.toString();
                context.write(new Text(value),NullWritable.get());
            }
        }
    }

完整编码如下

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.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.omg.IOP.IOR;

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

public class SRS {
    public static class SRSMapper extends Mapper<Object,Text,NullWritable,Text>{
        private Random rands=new Random();
        private Double percentage=0.3;
        public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
            if(rands.nextDouble()<percentage){
                context.write(NullWritable.get(),value);
            }
        }
    }
    public static class SRSReduce extends Reducer<NullWritable,Text,Text,NullWritable>{
        public void reducer(NullWritable key, Iterator<Text> values,Context context) throws IOException,InterruptedException{
            while(values.hasNext()){
                String value=values.toString();
                context.write(new Text(value),NullWritable.get());
            }
        }
    }
    public static void main(String[] args) throws Exception{
        FileUtil.deleteDir("output");
        Configuration configuration=new Configuration();
        String[] otherArgs=new String[]{"input/file.txt","output"};
        if(otherArgs.length!=2){
            System.err.println("参数错误");
            System.exit(2);
        }
        Job job=new Job(configuration,"Inverse");
        job.setJarByClass(Grep.class);
        job.setMapperClass(SRSMapper.class);
        job.setReducerClass(SRSReduce.class);
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job,new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));
        System.exit(job.waitForCompletion(true)?0:1);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值