CustomWritable 对象导致测试用例失败
CustomWritable objects resulting in test case failure
我将 Normal IntWritable
更改为适当的 CustomerWritable
class ,此后我的测试用例失败了。我在这里做错了什么?
/*
this is my customWritable
*/
package hadoop.mapreduce;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
public class BidPriceCustWritable implements WritableComparable<BidPriceCustWritable> {
private Text cityId;
private Text osName;
private IntWritable bidPrice;
public Text getCityId() {
return cityId;
}
public void setCityId(Text cityId) {
this.cityId = cityId;
}
public Text getOsName() {
return osName;
}
//
public void setOsName(Text osName) {
this.osName = osName;
}
public IntWritable getBidPrice() {
return bidPrice;
}
public void setBidPrice(IntWritable bidPrice) {
this.bidPrice = bidPrice;
}
//hithihi
public BidPriceCustWritable() {
super();
this.cityId = new Text("");
this.osName = new Text("");
this.bidPrice = new IntWritable(0);
}
// hiihioiii
public BidPriceCustWritable(Text cityId, Text osName, IntWritable bidPrice) {
super();
this.cityId = cityId;
this.osName = osName;
this.bidPrice = bidPrice;
}
@Override
public void write(DataOutput out) throws IOException {
// TODO Auto-generated method stub
cityId.write(out);
osName.write(out);
bidPrice.write(out);
}
@Override
public void readFields(DataInput in) throws IOException {
// TODO Auto-generated method stub
cityId.readFields(in);
osName.readFields(in);
bidPrice.readFields(in);
}
@Override
public int compareTo(BidPriceCustWritable o) {
return this.getBidPrice().compareTo(o.getBidPrice());
}
@Override
public String toString() {
return osName + "," + bidPrice;
}
}
Expected output is
mapDriver.withOutput(new Text("daxinganling"), new BidPriceCustWritable(new Text("daxinganling"), new Text("WINDOWS_XP"), new IntWritable(294)));
mapDriver.withOutput(new Text("huainan"), new BidPriceCustWritable(new Text("huainan"), new Text("WINDOWS_7"), new IntWritable(277)));
但是它失败了。我调试了问题,一切都按预期工作,但测试用例失败。
结果日志中的密钥不匹配。检查您的代码。
此外,如我所见,您没有覆盖 equals() 和 hashcode() 方法在您的自定义可写类型中。否则你会得到错误的结果。
我将 Normal IntWritable
更改为适当的 CustomerWritable
class ,此后我的测试用例失败了。我在这里做错了什么?
/*
this is my customWritable
*/
package hadoop.mapreduce;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
public class BidPriceCustWritable implements WritableComparable<BidPriceCustWritable> {
private Text cityId;
private Text osName;
private IntWritable bidPrice;
public Text getCityId() {
return cityId;
}
public void setCityId(Text cityId) {
this.cityId = cityId;
}
public Text getOsName() {
return osName;
}
//
public void setOsName(Text osName) {
this.osName = osName;
}
public IntWritable getBidPrice() {
return bidPrice;
}
public void setBidPrice(IntWritable bidPrice) {
this.bidPrice = bidPrice;
}
//hithihi
public BidPriceCustWritable() {
super();
this.cityId = new Text("");
this.osName = new Text("");
this.bidPrice = new IntWritable(0);
}
// hiihioiii
public BidPriceCustWritable(Text cityId, Text osName, IntWritable bidPrice) {
super();
this.cityId = cityId;
this.osName = osName;
this.bidPrice = bidPrice;
}
@Override
public void write(DataOutput out) throws IOException {
// TODO Auto-generated method stub
cityId.write(out);
osName.write(out);
bidPrice.write(out);
}
@Override
public void readFields(DataInput in) throws IOException {
// TODO Auto-generated method stub
cityId.readFields(in);
osName.readFields(in);
bidPrice.readFields(in);
}
@Override
public int compareTo(BidPriceCustWritable o) {
return this.getBidPrice().compareTo(o.getBidPrice());
}
@Override
public String toString() {
return osName + "," + bidPrice;
}
}
Expected output is
mapDriver.withOutput(new Text("daxinganling"), new BidPriceCustWritable(new Text("daxinganling"), new Text("WINDOWS_XP"), new IntWritable(294)));
mapDriver.withOutput(new Text("huainan"), new BidPriceCustWritable(new Text("huainan"), new Text("WINDOWS_7"), new IntWritable(277)));
但是它失败了。我调试了问题,一切都按预期工作,但测试用例失败。
结果日志中的密钥不匹配。检查您的代码。
此外,如我所见,您没有覆盖 equals() 和 hashcode() 方法在您的自定义可写类型中。否则你会得到错误的结果。