运行MapReduce WordCount作业时出现错误。
错误:java.io.IOException:初始化所有收集器 失败。最后一个收集器的错误是:class wordcount.wordmapper at org.apache.hadoop.mapred.MapTask.createSortingCollector(MapTask.java:414) 在org.apache.hadoop.mapred.MapTask.access $ 100(MapTask.java:81)at org.apache.hadoop.mapred.YarnChild $ 2.run(YarnChild.java:164)at at java.security.AccessController.doPrivileged(Native Method)at javax.security.auth.Subject.doAs(Subject.java:415) atorg.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1693)在 org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)引起: java.lang.ClassCastException:class wordcount.wordmapperat java.lang.Class.asSubclass(Class.java:3165)在 org.apache.hadoop.mapred.JobConf.getOutputKeyComparator(JobConf.java:892) 在 org.apache.hadoop.mapred.MapTask $ MapOutputBuffer.init(MapTask.java:1005) 在 org.apache.hadoop.mapred.MapTask.createSortingCollector(MapTask.java:402)
答案 0 :(得分:2)
我发现必须实现WritableComparable<>
接口才能起作用。
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
public class Pair implements WritableComparable<Pair> {
public Text key1;
public Text key2;
public Pair() {
key1 = new Text();
key2 = new Text();
}
public Pair(String key1, String key2) {
this.key1 = new Text(key1);
this.key2 = new Text(key2);
}
public void setKey1(Text key1) {
this.key1 = key1;
}
public void setKey2(Text key2) {
this.key2 = key2;
}
public Text getKey1() {
return key1;
}
public Text getKey2() {
return key2;
}
@Override
public boolean equals(Object b) {
Pair p = (Pair) b;
return p.key1.toString().equals(this.key1.toString())
&& p.key2.toString().equals(this.key2.toString());
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((key1 == null) ? 0 : key1.toString().hashCode());
result = prime * result
+ ((key2 == null) ? 0 : key2.toString().hashCode());
return result;
}
@Override
public String toString() {
return "(" + key1 + ", " + key2 + ")";
}
@Override
public void readFields(DataInput arg0) throws IOException {
key1.readFields(arg0);
key2.readFields(arg0);
}
@Override
public void write(DataOutput arg0) throws IOException {
key1.write(arg0);
key2.write(arg0);
}
public int compareTo(Pair p1) {
int k = this.key1.toString().compareTo(p1.key1.toString());
if (k != 0) {
return k;
} else {
return this.key2.toString().compareTo(p1.key2.toString());
}
}
}
答案 1 :(得分:0)
运行mapredue作业时,我也会发生同样的事情。请导入正确的Text类。
如果您导入了以下导入,则会收到该错误:
import com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider.Text;
将其更改为此。
import org.apache.hadoop.io.Text;
答案 2 :(得分:0)
您的类必须具有默认构造函数,如果已定义任何参数化构造函数,则会自动删除默认构造函数。您应该明确定义此构造函数。你能展示你的代码片段吗?
Class SampleClass
{
int a;
public SampleClass(int param)
{
a = param;
}
public SampleClass()
{
}
}