MapReduce 基础算法【关系代数运算】

本文介绍了如何使用MapReduce进行关系代数运算,包括选择、投影、交、差和自然连接操作。通过MapReduce的并行化设计,可以高效处理这些运算,尤其是在满足特定并行化条件时。
摘要由CSDN通过智能技术生成

关系代数运算

MapReduce可以在关系代数的运算上发挥重要的作用,因为关系代数运算具有数据相关性低的特性,这使得其便于进行MapReduce的并行化算法设计。

常见的关系代数运算包括选择、投影、并、交、差以及自然连接操作,都可以十分容易利用MapReduce来进行并行化。

选择操作

对于关系 R R 应用条件 C ,例如:查询分数大于90分的学生。我们只需要在Map阶段对于每个输入的记录判断是否满足条件,将满足条件的记录的输出即可。Reduce阶段无需做额外的工作。

代码:
package cn.zzuli.zcs0;

/**
 * Created by 张超帅 on 2018/8/16.
 */
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;


/**
 * 表示一个关系的属性构成
 * @author KING
 *
 */
public class RelationA implements WritableComparable<RelationA>{
    private int id;
    private String name;
    private int age;
    private double weight;

    public RelationA(){}

    public RelationA(int id, String name, int age, double weight){
        this.setId(id);
        this.setName(name);
        this.setAge(age);
        this.setWeight(weight);
    }

    public RelationA(String line){
        String[] value = line.split(",");
        this.setId(Integer.parseInt(value[0]));
        this.setName(value[1]);
        this.setAge(Integer.parseInt(value[2]));
        this.setWeight(Double.parseDouble(value[3]));
    }

    public boolean isCondition(int col, String value){
        if(col == 0 && Integer.parseInt(value) == this.id)
            return true;
        else if(col == 1 && name.equals(value))
            return true;
        else if(col ==2 && Integer.parseInt(value) == this.age)
            return true;
        else if(col ==3 && Double.parseDouble(value) == this.weight)
            return true;
        else
            return false;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public String getCol(int col){
        switch(col){
            case 0: return String.valueOf(id);
            case 1: return name;
            case 2: return String.valueOf(age);
            case 3: return String.valueOf(weight);
            default: return null;
        }
    }

    public String getValueExcept(int col){
        switch(col){
            case 0: return name + "," + String.valueOf(age) + "," + String.valueOf(weight);
            case 1: return String.valueOf(id) + "," + String.valueOf(age) + "," + String.valueOf(weight);
            case 2: return String.valueOf(id) + "," + name + 
  • 3
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值