我不知道我的代码出了什么问题。它100%停留在地图上输入:
expression, number
1+2+3, 0.4
输出应为:
count expression number
1 1+2+3 0.4
2 3*4 0.8
这是map
方法:
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
inputs = value.toString();
tokens = inputs.split(",");
expr = new Text (tokens[0]);
fit = new DoubleWritable(Double.parseDouble(tokens[1]));
EF.setExpr(tokens[0]);
EF.setFit(Double.parseDouble(tokens[1]));
count++;
context.write(new IntWritable(count),EF );
}
和类别Reduce
:
public static class Reduce extends Reducer<IntWritable,exprfit,IntWritable,exprfit> {
private exprfit EF = new exprfit();
private int count;
public void reduce(IntWritable key, Iterable<exprfit> values, Context context) throws IOException, InterruptedException {
EF.setExpr(values.iterator().next().getExpr());
EF.setFit(values.iterator().next().getFit());
context.write(key, EF);
}
}
类别exprfit
:
public static class exprfit implements Writable {
private String expr;
private Double fit;// type of output value
public String getExpr() {
return expr;
}
public void setExpr(String expr) {
this.expr = expr;
}
public double getFit() {
return fit;
}
public void setFit(Double fit) {
this.fit = fit;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeChars(expr);
out.writeDouble(fit);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
public void readFields(DataInput in) throws IOException {
expr =in.readLine();
fit = in.readDouble();
}
}
减速器是否启动?日志中有错误吗?
只是一个想法:可能是readLine()使您停滞。。。我会尝试使用writeString()
和readString()
,而不是writeChars()
和readLine()
来从WritableUtils类中写入/读取Writable实现中的expr。重写exprfit类中的方法get()
和set(Writable value)
也是一个好主意(至少在旧的API中是这样)。
您也可以将计数器存储为VIntWritable,而不是IntWritable以节省一些空间(如果需要),甚至更短。
更多评论:在Reducer中,我会在reduce()方法中初始化EF,并删除计数,因为它没有被使用。我不确定您是否需要Map/Reduce来完成此任务。