在mapreduce的map函数中使用context.write两次

qc6wkl3g  于 2021-05-29  发布在  Hadoop
关注(0)|答案(0)|浏览(294)

在一个简单的Map函数中,有两个输入文件。第一个文件行以m开头,第二个文件行以n开头。Map功能是:
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { int m = 4, p = 4; String line = value.toString(); // (M, i, j, Mij); String[] indicesAndValue = line.split(","); Text outputKey = new Text(); Text outputValue = new Text(); if (indicesAndValue[0].equals("M")) { //First file for (int k = 1; k <= p; k++) { outputKey.set(indicesAndValue[1] + "," + k); // outputKey.set(i,k); outputValue.set(indicesAndValue[0] + "," + indicesAndValue[2] + "," + indicesAndValue[3]); // outputValue.set(M,j,Mij); context.write(outputKey, outputValue); } } else { //Second file // (N, j, k, Njk); for (int k = 1; k <= m; k++) { outputKey.set(k + "," + indicesAndValue[2]); outputValue.set("N," + indicesAndValue[1] + "," + indicesAndValue[3]); context.write(outputKey, outputValue); } } }
现在,我的两个输入文件是一样的。所以,我只想使用一个输入文件,但我想使用context.write两次。分割一条线后,将其绘制两次。当我改变代码如下,减少功能不产生任何输出!更改Map功能:
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { Configuration conf = context.getConfiguration(); int m = 4, p = 4; String line = value.toString(); // (M, i, j, Mij); String[] indicesAndValue = line.split(","); Text outputKey = new Text(); Text outputValue = new Text(); //if (indicesAndValue[0].equals("M")) { for (int k = 1; k <= p; k++) { outputKey.set(indicesAndValue[1] + "," + k); // outputKey.set(i,k); outputValue.set(indicesAndValue[0] + "," + indicesAndValue[2] + "," + indicesAndValue[3]); // outputValue.set(M,j,Mij); context.write(outputKey, outputValue); outputKey.set(k + "," + indicesAndValue[2]); outputValue.set("N," + indicesAndValue[1] + "," + indicesAndValue[3]); context.write(outputKey, outputValue); } }
减少功能:
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { String[] value; String[] k; // key=(i,k), // Values = [(M/N,j,V/W),..] k = key.toString().split(","); HashMap<Integer, Integer> hashA = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> hashB = new HashMap<Integer, Integer>(); int i = 0, j = 0; //check values for (Text val : values) { value = val.toString().split(","); if (value[0].equals("M")) { hashA.put(Integer.parseInt(value[1]), Integer.parseInt(value[2])); } else { hashB.put(Integer.parseInt(value[1]), Integer.parseInt(value[2])); } } //some codes to produce result value int n = 4, result = 101, m_ij , n_jk , t; for (int r = 1; r <= n; r++) { m_ij = hashA.containsKey(r) ? hashA.get(r) : 101; n_jk = hashB.containsKey(r) ? hashB.get(r) : 101; t = m_ij + n_jk; //finding minimum value result = result > t ? t : result; } context.write(null, new Text(key.toString() + ","+ Integer.toString(result))); }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题