Hadoop作业输出中不需要的字符
我写了一个简单的程序来收集一些数据中关于bigram的统计数据。 我将统计数据打印到自定义文件Hadoop作业输出中不需要的字符,hadoop,Hadoop,我写了一个简单的程序来收集一些数据中关于bigram的统计数据。 我将统计数据打印到自定义文件 Path file = new Path(context.getConfiguration().get("mapred.output.dir") + "/bigram.txt"); FSDataOutputStream out = file.getFileSystem(context.getConfiguration()).create(file); 我的代码有以下几行: Text.writeStr
Path file = new Path(context.getConfiguration().get("mapred.output.dir") + "/bigram.txt");
FSDataOutputStream out = file.getFileSystem(context.getConfiguration()).create(file);
我的代码有以下几行:
Text.writeString(out, "total number of unique bigrams: " + uniqBigramCount + "\n");
Text.writeString(out, "total number of bigrams: " + totalBigramCount + "\n");
Text.writeString(out, "number of bigrams that appear only once: " + onceBigramCount + "\n");
我在vim/gedit中获得以下输出:
'total number of unique bigrams: 424462
!total number of bigrams: 1578220
0number of bigrams that appear only once: 296139
除了行首不需要的字符外,还有一些非打印字符。这背后的原因可能是什么?正如@ThomasJungblut所说,writeString方法为每次调用writeString写出两个值——字符串的长度(作为VIN)和字符串字节:
/** Write a UTF8 encoded string to out
*/
public static int writeString(DataOutput out, String s) throws IOException {
ByteBuffer bytes = encode(s);
int length = bytes.limit();
WritableUtils.writeVInt(out, length);
out.write(bytes.array(), 0, length);
return length;
}
如果您只是希望能够将文本输出打印到此文件(即所有人类可读),那么我建议您使用PrintStream
包装out
变量,并使用println或printf方法:
PrintStream ps = new PrintStream(out);
ps.printf("total number of unique bigrams: %d\n", uniqBigramCount);
ps.printf("total number of bigrams: %d\n", totalBigramCount);
ps.printf("number of bigrams that appear only once: %d\n", onceBigramCount);
ps.close();
我相信是字符串的长度(写在前面)造成了一些二进制的扭曲。@ThomasJungblut:不,我删除了写在前面的字符串,例如,我将第一个print语句改为
Text.writeString(out,uniqBigramCount+“\n”)代码>。我得到了以下输出:^G424462^H1578220^G296139不需要的字符仍然存在。我指的是文本的内部行为,而不是你传递的内容,这完全无关。@Thomas Jungblut和Chris:谢谢你的回答,Chris的建议奏效了。