今天实现了pairs算法,但是出于公司保密协议的规定,不能够随意把代码分享出来。
简单分享Map,Reduce,MaruPairs三个类,要实现MaruPairs,还需要重写自己的
Comparator类,做点优化。程序功能是判断风扇fan1开启下时,风扇fan2开启的比率。
前三个类代码粘帖如下:
Map类:
public static class MyMap extends Mapper<LongWritable, Text, MaruPair, IntWritable> {
//private MaruPair keyM = new MaruPair(); //output.collect key
private IntWritable valueM=new IntWritable(); //output.collect value
private String fan1 = new String();
private String fan2 = new String();
private HashMap<MaruPair, Integer> inContextM;
public void map(LongWritable key, Text value, Context context)
throws IOException , InterruptedException {
String line = new String(value.getBytes(),0,value.getLength(),"sjis"); //sjis:キャラクタのパラメータ( 日本語を指定する)
StringTokenizer tokenizer = new StringTokenizer(line);
String strvalue = new String();
int column = 0; //列の番号
//start while1
while (tokenizer.hasMoreTokens()){
column += 1;
strvalue = tokenizer.nextToken();
strvalue = strvalue.trim();//move space if there is.
switch(column){
case 90:
//inFan1
fan1 = strvalue;
break;
case 91:
//inFan2
fan2 = strvalue;
MaruPair tmpkeyM = new MaruPair(fan1, fan2);
if (inContextM.containsKey(tmpkeyM)) {
inContextM.put(tmpkeyM, inContextM.get(tmpkeyM) + 1);
} else {
inContextM.put(tmpkeyM, 1);
}
break;
default:
//
break;
}//end switch(column)
}//end while1
}//end map
//do once at MapTask startup
@Override
protected void setup(Context context) {
inContextM = new HashMap<MaruPair, Integer>();
}
//do once at MapTask end up
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
for (Entry<MaruPair, Integer> m : inContextM.entrySet()) {
valueM.set(m.getValue());
context.write(m.getKey(), valueM);
}
}
}//end Mapclass
Reduce类:
public static class MyReduce extends Reducer<MaruPair, IntWritable, Text, DoubleWritable> {
private Text keyR = new Text(); //output.collect key
private DoubleWritable valueR = new DoubleWritable(); //output.collect value
private HashMap<Text, Integer> inContextR;
private Text keyOn = new Text("On");
private Text keyOff = new Text("Off");
public void reduce(MaruPair key, Iterable<IntWritable> values, Context context)
throws IOException , InterruptedException {
int sum = 0;
//start for
for(IntWritable value : values) {
sum += value.get();
}//end for
if (isOn(key.getMaru1())){
if (isOn(key.getMaru2())){
if (inContextR.containsKey(keyOn)) {
inContextR.put(keyOn, inContextR.get(keyOn) + sum);
} else {
inContextR.put(keyOn, sum);
}
}else{
if (inContextR.containsKey(keyOff)) {
inContextR.put(keyOff, inContextR.get(keyOff) + sum);
} else {
inContextR.put(keyOff, sum);
}
}
}
}//end reduce
@Override
protected void setup(Context context) {
inContextR = new HashMap<Text, Integer>();
}
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
Double rate = new Double(0);
int on = inContextR.get(keyOn);
int off = inContextR.get(keyOff);
rate = (on*1.00)/(on + off);
valueR.set(Math.round(rate*1000)/1000.0);
keyR.set("FanOnRate:");
context.write(keyR, valueR);
}
}//end Reduce class
MaruPair类:
public class MaruPair implements WritableComparable<MaruPair> {
private Text maru1;
private Text maru2;
public MaruPair() {
set(new Text(), new Text());
}
public MaruPair(String maru1, String maru2) {
set(new Text(maru1), new Text(maru2));
}
public MaruPair(Text maru1, Text maru2) {
set(maru1, maru2);
}
public void set(Text maru1, Text maru2) {
this.maru1 = maru1;
this.maru2 = maru2;
}
public void set(String maru1, String maru2) {
this.maru1 = new Text(maru1);
this.maru2 = new Text(maru2);
}
public Text getMaru1() {
return maru1;
}
public Text getMaru2() {
return maru2;
}
@Override
public void write(DataOutput out) throws IOException {
maru1.write(out);
maru2.write(out);
}
@Override
public void readFields(DataInput in) throws IOException {
maru1.readFields(in);
maru2.readFields(in);
}
@Override
public int hashCode() {
return maru1.hashCode() * 157 + maru2.hashCode();
}
@Override
public boolean equals(Object o) {
if (o instanceof MaruPair) {
MaruPair tp = (MaruPair) o;
return maru1.equals(tp.maru1) && maru2.equals(tp.maru2);
}
return false;
}
@Override
public String toString() {
return maru1 + "\t" + maru2;
}
@Override
public int compareTo(MaruPair tp) {
int cmp = maru1.compareTo(tp.maru1);
return (cmp != 0 ? cmp : (maru2.compareTo(tp.maru2)));
}
// ^^ MaruPair