作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这里是 Hadoop 新手。我想计算文本中每行单词的共现次数,例如一个单词与其他单词出现在同一行中的次数。为此,我创建了一个特殊的词对类,因此 MapReduce 会给我词对,然后是计数。问题是,结果一团糟,我不知道我哪里错了。
我的词对类是这样的:
public class Par implements Writable,WritableComparable<Par> {
public String palabra;
public String vecino;
public Par(String palabra, String vecino) {
this.palabra = palabra;
this.vecino = vecino;
}
public Par() {
this.palabra = new String();
this.vecino = new String();
}
@Override
public int compareTo(Par otra) {
int retorno = this.palabra.compareTo(otra.palabra);
if(retorno != 0){
return retorno;
}
return this.vecino.compareTo(otra.vecino);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(palabra);
out.writeUTF(vecino);
}
@Override
public void readFields(DataInput in) throws IOException {
palabra = in.readUTF();
vecino = in.readUTF();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((palabra == null) ? 0 : palabra.hashCode());
result = prime * result + ((vecino == null) ? 0 : vecino.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Par other = (Par) obj;
if (palabra == null) {
if (other.palabra != null)
return false;
} else if (!palabra.equals(other.palabra))
return false;
if (vecino == null) {
if (other.vecino != null)
return false;
} else if (!vecino.equals(other.vecino))
return false;
return true;
}
@Override
public String toString() {
return "Par [" + palabra + " , " + vecino + "]";
}
}
我的映射器是:
public class Co_OcurrenciaMapper extends Mapper<LongWritable, Text, Par, IntWritable> {
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
IntWritable one = new IntWritable(1);
String[] palabras = value.toString().split("\\s+");
if (palabras.length > 1) {
for (int i = 0; i < palabras.length - 1; i++) {
for (int j = i + 1; j < palabras.length; j++) {
context.write(new Par(palabras[i], palabras[j]), one);
}
}
}
}
}
我用 MapReduce 得到的结果是:
[cloudera@quickstart Desktop]$ hadoop fs -cat salidaO11/part-r-00000 |head -15
Par [ , &c.] 35
Par [ , &c.'] 2
Par [ , &c.,] 4
Par [ , &c]] 23
Par [ , '] 6
Par [ , ''Od's] 1
Par [ , ''Tis] 2
Par [ , ''tis] 1
Par [ , ''twas] 1
Par [ , '--O] 1
Par [ , 'A] 17
Par [ , 'ARTEMIDORUS.'] 1
Par [ , 'Above] 1
Par [ , 'Achilles] 2
Par [ , 'Ad] 3
cat: Unable to write to output stream.
我哪里错了?有 friend 建议将这两个词连接成一个String,但我觉得这样不太优雅。
最佳答案
我不认为这里有什么问题。您似乎没有进行任何数据清理,所以我认为它会产生那样的脏输出是公平的。
您可能想尝试编写一些 MRUnit 测试,或者将一些更小、更干净的数据集输入作业以确认它是否符合您的预期?
关于java - Hadoop 结果搞砸了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36201506/
我是一名优秀的程序员,十分优秀!