关系代数运算
MapReduce可以在关系代数的运算上发挥重要的作用,因为关系代数运算具有数据相关性低的特性,这使得其便于进行MapReduce的并行化算法设计。
常见的关系代数运算包括选择、投影、并、交、差以及自然连接操作,都可以十分容易利用MapReduce来进行并行化。
选择操作
对于关系 R R 应用条件 ,例如:查询分数大于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 +