MapReduce 学习5 ---- 输出文件学习
2.reduce任务处理
2.3 把reduce的输出保存到文件中。
ruduce任务中把数据写到hdfs中使用
context.write(k2, new LongWritable(sum));
只要分析context,就可以理解如何把数据写到hdfs上
class ChainReduceContextImpl<KEYIN, VALUEIN, KEYOUT, VALUEOUT> implements
ReduceContext<KEYIN, VALUEIN, KEYOUT, VALUEOUT> {
private final RecordWriter<KEYOUT, VALUEOUT> rw;
public void write(KEYOUT key, VALUEOUT value) throws IOException,
InterruptedException {
rw.write(key, value);
}
}
//分隔符的配置项
public static String SEPERATOR = "mapreduce.output.textoutputformat.separator";
protected static class LineRecordWriter<K, V>
extends RecordWriter<K, V> {
private static final byte[] newline;
static {
try {
//设置每一行的换行符
newline = "\n".getBytes(utf8);
} catch (UnsupportedEncodingException uee) {
throw new IllegalArgumentException("can't find " + utf8 + " encoding");
}
}
protected DataOutputStream out;
private final byte[] keyValueSeparator;
public LineRecordWriter(DataOutputStream out, String keyValueSeparator) {
this.out = out;
try {
this.keyValueSeparator = keyValueSeparator.getBytes(utf8);
} catch (UnsupportedEncodingException uee) {
throw new IllegalArgumentException("can't find " + utf8 + " encoding");
}
}
public LineRecordWriter(DataOutputStream out) {
//默认分隔符是空格
this(out, "\t");
}
//把每一个k3,v3写到一行中,k3与v3默认以空格为分隔符
public synchronized void write(K key, V value)
throws IOException {
boolean nullKey = key == null || key instanceof NullWritable;
boolean nullValue = value == null || value instanceof NullWritable;
if (nullKey && nullValue) {
return;
}
if (!nullKey) {
writeObject(key);
}
if (!(nullKey || nullValue)) {
out.write(keyValueSeparator);
}
if (!nullValue) {
writeObject(value);
}
out.write(newline);
}
private void writeObject(Object o) throws IOException {
if (o instanceof Text) {
Text to = (Text) o;
out.write(to.getBytes(), 0, to.getLength());
} else {
out.write(o.toString().getBytes(utf8));
}
}
}

浙公网安备 33010602011771号