MapReduce实例

输出Top N

需求

输出流量使用量在前10的用户信息
例如:左为输入右为输出
在这里插入图片描述

实现:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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 FlowDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Job job = Job.getInstance(new Configuration());
        job.setJarByClass(FlowDriver.class);
        job.setMapperClass(FlowMap.class);
        job.setReducerClass(FlowReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);
        job.setMapOutputKeyClass(FlowBean.class);
        job.setMapOutputValueClass(Text.class);
        job.setGroupingComparatorClass(FlowComparator.class);
        FileInputFormat.setInputPaths(job, new Path("F:\\TopN\\in.txt"));
        FileOutputFormat.setOutputPath(job, new Path("F:\\TopN\\output"));
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }
}

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

import java.io.IOException;

/**
 * 先输出FlowBean、在输出电话号
 */
public class FlowMap extends Mapper<LongWritable, Text,FlowBean,Text> {

    // 减少对象创建
    private FlowBean flowBean = new FlowBean();
    private Text phone = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] datas = value.toString().split("\t");
        flowBean.setUpFlow(Long.parseLong(datas[1]));
        flowBean.setDownFlow(Long.parseLong(datas[2]));
        flowBean.setSumFlow(Long.parseLong(datas[3]));
        phone.set(datas[0]);
        context.write(flowBean,phone);
    }
}

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;



/**
 * 为了将所有数据都存在一个组中,而不是按照框架默认分组
 */
public class FlowComparator extends WritableComparator {
    protected FlowComparator() {
        super(FlowBean.class,true);// 防止空指针
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        return 0; // 保证所有的数据都在同一组中
    }
}

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

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

public class FlowReduce extends Reducer<FlowBean, Text, Text, FlowBean> {
    // 输入数据为流量、电话号 输出数据为电话号、流量

    @Override
    protected void reduce(FlowBean key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        // 只要前十的数据
        Iterator<Text> iterator = values.iterator();
        for (int i = 0; i < 10; i++) {
            if (iterator.hasNext()){
                context.write(iterator.next(),key);
            }
        }
    }
}

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class FlowBean implements WritableComparable<FlowBean> {
    private long upFlow;
    private long downFlow;
    private long sumFlow;

    @Override
    public String toString() {
        return upFlow + "\t" + downFlow + "\t" + sumFlow;
    }

    public long getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(long upFlow) {
        this.upFlow = upFlow;
    }

    public long getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(long downFlow) {
        this.downFlow = downFlow;
    }

    public long getSumFlow() {
        return sumFlow;
    }

    public void setSumFlow(long sumFlow) {
        this.sumFlow = sumFlow;
    }

    public int compareTo(FlowBean o) {
        return Long.compare(o.sumFlow,this.sumFlow);//按照总流量比较
    }

    public void write(DataOutput out) throws IOException {
        out.writeLong(upFlow);
        out.writeLong(downFlow);
        out.writeLong(sumFlow);
    }

    public void readFields(DataInput in) throws IOException {
        upFlow = in.readLong();
        downFlow = in.readLong();
        sumFlow = in.readLong();
    }
}

倒排索引

需求

在这里插入图片描述

实现

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;
public class InvertedMap1 extends Mapper<LongWritable, Text, Text, IntWritable> {
    private Text k = new Text();
    private IntWritable v = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 首先,获取文件名
        FileSplit fileSplit = (FileSplit) context.getInputSplit();
        String filename = fileSplit.getPath().getName();
        // 获取数据
        String[] datas = value.toString().split(" ");
        for (String data : datas) {
            k.set(data + "--" + filename);
            context.write(k,v);
        }
    }
}

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

import java.io.IOException;
public class InvertedReduce1 extends Reducer<Text, IntWritable, Text, IntWritable> {
    // 求和
    IntWritable v = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable value : values) {
            sum += value.get();
        }
        v.set(sum);
        context.write(key,v);
    }
}


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

import java.io.IOException;

public class InvertedMap2 extends Mapper<LongWritable, Text, Text, Text> {
    private Text k = new Text();
    private Text v = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 按照--切分value
        String[] split = value.toString().split("--");
        k.set(split[0]);
        String[] datas = split[1].toString().split("\t");
        // 此时datas[0]为文件名 data[1]为次数
        v.set(datas[0] + "-->" + datas[1]);
        context.write(k,v);
    }
}

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

import java.io.IOException;

public class InvertedReduce2 extends Reducer<Text, Text, Text, Text> {
    // 字符串拼接
    private StringBuffer stringBuffer = new StringBuffer();
    private Text v = new Text();
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        stringBuffer.delete(0,stringBuffer.length());
        for (Text value : values) {
            stringBuffer.append(value.toString()).append(" ");
        }
        v.set(stringBuffer.toString());
        context.write(key,v);
    }
}

寻找共同好友

需求

类似抖音关注,查找两个人的共同关注。
输入:(A关注了BCDEFO)
A:B,C,D,F,E,O
B:A,C,E,K
C:F,A,D,I
D:A,E,F,L
E:B,C,D,M,L
F:A,B,C,D,E,O,M
G:A,C,D,E,F
H:A,C,D,E,O
I:A,O
J:B,O
K:A,C,D
L:D,E,F
M:E,F,G
O:A,H,I,J
第一次输出:(A被IKCBGFHOD关注)
A I,K,C,B,G,F,H,O,D,
B A,F,J,E,
C A,E,B,H,F,G,K,
D G,C,K,A,L,F,E,H,
E G,M,L,H,A,F,B,D,
F L,M,D,C,G,A,
G M,
H O,
I O,C,
J O,
K B,
L D,E,
M E,F,
O A,H,I,J,F,
第二次输出:即两人共同关注
A-B E C
A-C D F
A-D E F
A-E D B C
A-F O B C D E
A-G F E C D
A-H E C D O
A-I O
A-J O B
A-K D C
A-L F E D
A-M E F
B-C A
B-D A E
B-E C
B-F E A C
B-G C E A
B-H A E C
B-I A
B-K C A
B-L E
B-M E
B-O A
C-D A F
C-E D
C-F D A
C-G D F A
C-H D A
C-I A
C-K A D
C-L D F
C-M F
C-O I A
D-E L
D-F A E
D-G E A F
D-H A E
D-I A
D-K A
D-L E F
D-M F E
D-O A
E-F D M C B
E-G C D
E-H C D
E-J B
E-K C D
E-L D
F-G D C A E
F-H A D O E C
F-I O A
F-J B O
F-K D C A
F-L E D
F-M E
F-O A
G-H D C E A
G-I A
G-K D A C
G-L D F E
G-M E F
G-O A
H-I O A
H-J O
H-K A C D
H-L D E
H-M E
H-O A
I-J O
I-K A
I-O A
K-L D
K-O A
L-M E F

实操

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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 FriendDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Job job = Job.getInstance(new Configuration());
        job.setJarByClass(FriendDriver.class);
        job.setMapperClass(FriendMap1.class);
        job.setReducerClass(FriendReduce1.class);

        job.setMapOutputValueClass(Text.class);
        job.setMapOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.setOutputKeyClass(Text.class);

        FileOutputFormat.setOutputPath(job, new Path("F:\\FindFriend\\output"));
        FileInputFormat.setInputPaths(job, new Path("F:\\FindFriend"));
        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}

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

import java.io.IOException;

public class FriendMap1 extends Mapper<LongWritable,Text, Text,Text> {
    private Text k = new Text();
    private Text v = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // B:A,C,E,K
        // 首先,找出被谁关注
        String[] split = value.toString().split(":");
        String person = split[0];
        String[] friends = split[1].split(",");
        for (String friend : friends) {
            k.set(friend);
            v.set(person);
            context.write(k,v);
        }
    }
}

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

import java.io.IOException;

public class FriendReduce1 extends Reducer<Text, Text, Text, Text> {
    private Text v = new Text();
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        // key不变,改value就行
        String s = new String();
        for (Text value : values) {
            s += value.toString()+",";
        }
        v.set(s);
        context.write(key,v);
    }
}

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

import java.io.IOException;

// B	A,F,J,E,(表示B被AFJE关注) =》A-B	E C
public class FriendMap2 extends Mapper<LongWritable, Text, Text, Text> {
    private Text k = new Text();
    private Text v = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] split = value.toString().split("\t");
        String friend = split[0];//被关注的人
        String person = split[1];//关注的人
        String[] men = person.split(",");
        v.set(friend);
        for (int i = 0; i < men.length; i++) {
            for (int j = i + 1; j < men.length; j++) {
                if (men[i].compareTo(men[j]) > 0) {// men[i]更大
                    k.set(men[j] + "-" + men[i]);
                } else {
                    k.set(men[i] + "-" + men[j]);
                }
                context.write(k, v);
            }
        }
    }
}

重点是map2的排序部分

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值