Java 在mapreduce的映射函数中使用context.write两次

Java 在mapreduce的映射函数中使用context.write两次,java,hadoop,mapreduce,Java,Hadoop,Mapreduce,在一个简单的映射函数中,有两个输入文件。第一个文件行以M开头,第二个文件行以N开头。映射函数为: public void映射(可长写键、文本值、上下文) 抛出IOException、InterruptedException{ int m=4,p=4; 字符串行=value.toString(); //(M,i,j,Mij); 字符串[]表示ANDValue=line.split(“,”); Text outputKey=新文本(); Text outputValue=新文本(); 如果(指示值

在一个简单的映射函数中,有两个输入文件。第一个文件行以M开头,第二个文件行以N开头。映射函数为:

public void映射(可长写键、文本值、上下文)
抛出IOException、InterruptedException{
int m=4,p=4;
字符串行=value.toString();
//(M,i,j,Mij);
字符串[]表示ANDValue=line.split(“,”);
Text outputKey=新文本();
Text outputValue=新文本();
如果(指示值[0]。等于(“M”)){
//第一档
对于(int k=1;k
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);
                }
            }
        }
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)));
        }