MapReduce之过滤(一)

MapReduce之过滤

模式描述

过滤作为一个抽象模式为其他模式服务,过滤简单的对每一条记录进行评估,并基于某个条件作出判断,以确定当前的这条记录是否保留。

目的

过滤掉不感兴趣的记录并将需要的记录保留下来

适用场景

使用过滤的唯一必要条件就是数据可以被解析为“记录”,并通过特定的准则判断他们是否可以被保留

  • 近距离观察数据
  • 跟踪某个事件的线索
  • 数据清洗
  • 简单随机抽样
  • 移除低分值数据

问题描述

在MapReduce中使用正则表达式匹配文本,输出存在该文本的这一行

样例输入

创建数据集的代码如下:

import java.io.*;
import java.util.Random;

public class create {
    public static String getRandomChar(int length){
        char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            buffer.append(chr[random.nextInt(36)]);
        }
        return buffer.toString();
    }
    public static void main(String[] args) throws IOException{
        String path="input/file.txt";
        File file=new File(path);
        if(!file.exists()){
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
        FileWriter fw=new FileWriter(file,true);
        BufferedWriter bw=new BufferedWriter(fw);

        for(int i=0;i<2000;i++){
            bw.write(getRandomChar(300)+"\n");
        }
        bw.flush();
        bw.close();
        fw.close();
    }
}

运行结果如下:
在这里插入图片描述

样例输出

由于数据集是随机生成的,匹配结果可能不一样
在这里插入图片描述

mapper阶段任务

mapper十分简单,使用java中内置的正则表达式库即可,如果匹配到符合模式的条件,就将该行输出出来,不匹配则什么也不作,不过在这个过程中直接使用String中的contains也挺方便的

mapper阶段编码如下
public static class GrepMapper extends Mapper<Object,Text, NullWritable,Text>{
        public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
            String line=value.toString();
            //匹配拥有“GRE”文本的行
            if(line.contains("GRE")){
                System.out.println(line);
                context.write(NullWritable.get(),new Text(line));

            }
        }
    }

reducer阶段任务

无,引文该模式只需要在map阶段就可以完成,所以没有reduce阶段,所有输出记录直接写在了文件系统的,这也加快了程序的运行速度,为了方便查看便直接打印在了控制台上

完整代码如下

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;

public class Grep {
    public static class GrepMapper extends Mapper<Object,Text, NullWritable,Text>{
        public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
            String line=value.toString();
            if(line.contains("GRE")){
                System.out.println(line);
                context.write(NullWritable.get(),new Text(line));

            }
        }
    }
    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(GrepMapper.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);
    }
}

遇到的问题

不知道为什么IDEA控制台无法显示报错原因了,突然返回一个异常值,查了一天也没发现问题,直到最后发现主函数中的输出的<键,值>类型设置错了,哎!以后还是细心点吧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值