Coverage Report - org.apache.giraph.lib.IdWithValueTextOutputFormat
 
Classes in this File Line Coverage Branch Coverage Complexity
IdWithValueTextOutputFormat
0%
0/3
N/A
1.667
IdWithValueTextOutputFormat$IdWithValueVertexWriter
0%
0/13
0%
0/4
1.667
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 package org.apache.giraph.lib;
 20  
 
 21  
 
 22  
 import org.apache.giraph.graph.BasicVertex;
 23  
 import org.apache.giraph.graph.VertexWriter;
 24  
 import org.apache.hadoop.io.Text;
 25  
 import org.apache.hadoop.io.Writable;
 26  
 import org.apache.hadoop.io.WritableComparable;
 27  
 import org.apache.hadoop.mapreduce.RecordWriter;
 28  
 import org.apache.hadoop.mapreduce.TaskAttemptContext;
 29  
 
 30  
 import java.io.IOException;
 31  
 
 32  
 /**
 33  
  * Write out Vertices' IDs and values, but not their edges nor edges' values.
 34  
  * This is a useful output format when the final value of the vertex is
 35  
  * all that's needed. The boolean configuration parameter reverse.id.and.value
 36  
  * allows reversing the output of id and value.
 37  
  *
 38  
  * @param <I> Vertex index value
 39  
  * @param <V> Vertex value
 40  
  * @param <E> Edge value
 41  
  */
 42  
 @SuppressWarnings("rawtypes")
 43  0
 public class IdWithValueTextOutputFormat<I extends WritableComparable,
 44  
     V extends Writable, E extends Writable>
 45  
     extends TextVertexOutputFormat<I, V, E> {
 46  
 
 47  
   /**
 48  
    * Vertex writer used with {@link IdWithValueTextOutputFormat}.
 49  
    *
 50  
    * @param <I> Vertex id
 51  
    * @param <V> Vertex data
 52  
    * @param <E> Edge data
 53  
    */
 54  0
   static class IdWithValueVertexWriter<I extends WritableComparable, V extends
 55  
       Writable, E extends Writable> extends TextVertexWriter<I, V, E> {
 56  
     /** Specify the output delimiter */
 57  
     public static final String LINE_TOKENIZE_VALUE = "output.delimiter";
 58  
     /** Default output delimiter */
 59  
     public static final String LINE_TOKENIZE_VALUE_DEFAULT = "\t";
 60  
     /** Reverse id and value order? */
 61  
     public static final String REVERSE_ID_AND_VALUE = "reverse.id.and.value";
 62  
     /** Default is to not reverse id and value order. */
 63  
     public static final boolean REVERSE_ID_AND_VALUE_DEFAULT = false;
 64  
     /** Saved delimiter */
 65  
     private String delimiter;
 66  
 
 67  
     /**
 68  
      * Constructor with record writer.
 69  
      *
 70  
      * @param recordWriter Writer from LineRecordWriter.
 71  
      */
 72  
     public IdWithValueVertexWriter(RecordWriter<Text, Text> recordWriter) {
 73  0
       super(recordWriter);
 74  0
     }
 75  
 
 76  
     @Override
 77  
     public void writeVertex(BasicVertex<I, V, E, ?> vertex) throws IOException,
 78  
     InterruptedException {
 79  0
       if (delimiter == null) {
 80  0
         delimiter = getContext().getConfiguration()
 81  
             .get(LINE_TOKENIZE_VALUE, LINE_TOKENIZE_VALUE_DEFAULT);
 82  
       }
 83  
 
 84  
       String first;
 85  
       String second;
 86  0
       boolean reverseOutput = getContext().getConfiguration()
 87  
           .getBoolean(REVERSE_ID_AND_VALUE, REVERSE_ID_AND_VALUE_DEFAULT);
 88  
 
 89  0
       if (reverseOutput) {
 90  0
         first = vertex.getVertexValue().toString();
 91  0
         second = vertex.getVertexId().toString();
 92  
       } else {
 93  0
         first = vertex.getVertexId().toString();
 94  0
         second = vertex.getVertexValue().toString();
 95  
       }
 96  
 
 97  0
       Text line = new Text(first + delimiter + second);
 98  
 
 99  0
       getRecordWriter().write(line, null);
 100  0
     }
 101  
   }
 102  
 
 103  
   @Override
 104  
   public VertexWriter<I, V, E> createVertexWriter(TaskAttemptContext context)
 105  
     throws IOException, InterruptedException {
 106  0
     return new IdWithValueVertexWriter<I, V, E>
 107  
     (textOutputFormat.getRecordWriter(context));
 108  
   }
 109  
 }