This project has retired. For details please refer to its Attic page.
IdWithValueTextOutputFormat xref
View Javadoc

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.io.formats;
20  
21  
22  import java.io.IOException;
23  import org.apache.giraph.graph.Vertex;
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.TaskAttemptContext;
28  
29  /**
30   * Write out Vertices' IDs and values, but not their edges nor edges' values.
31   * This is a useful output format when the final value of the vertex is
32   * all that's needed. The boolean configuration parameter reverse.id.and.value
33   * allows reversing the output of id and value.
34   *
35   * @param <I> Vertex index value
36   * @param <V> Vertex value
37   * @param <E> Edge value
38   */
39  @SuppressWarnings("rawtypes")
40  public class IdWithValueTextOutputFormat<I extends WritableComparable,
41      V extends Writable, E extends Writable>
42      extends TextVertexOutputFormat<I, V, E> {
43  
44    /** Specify the output delimiter */
45    public static final String LINE_TOKENIZE_VALUE = "output.delimiter";
46    /** Default output delimiter */
47    public static final String LINE_TOKENIZE_VALUE_DEFAULT = "\t";
48    /** Reverse id and value order? */
49    public static final String REVERSE_ID_AND_VALUE = "reverse.id.and.value";
50    /** Default is to not reverse id and value order. */
51    public static final boolean REVERSE_ID_AND_VALUE_DEFAULT = false;
52  
53    @Override
54    public TextVertexWriter createVertexWriter(TaskAttemptContext context) {
55      return new IdWithValueVertexWriter();
56    }
57  
58    /**
59     * Vertex writer used with {@link IdWithValueTextOutputFormat}.
60     */
61    protected class IdWithValueVertexWriter extends TextVertexWriterToEachLine {
62      /** Saved delimiter */
63      private String delimiter;
64      /** Cached reserve option */
65      private boolean reverseOutput;
66  
67      @Override
68      public void initialize(TaskAttemptContext context) throws IOException,
69          InterruptedException {
70        super.initialize(context);
71        delimiter = getConf().get(
72            LINE_TOKENIZE_VALUE, LINE_TOKENIZE_VALUE_DEFAULT);
73        reverseOutput = getConf().getBoolean(
74            REVERSE_ID_AND_VALUE, REVERSE_ID_AND_VALUE_DEFAULT);
75      }
76  
77      @Override
78      protected Text convertVertexToLine(Vertex<I, V, E> vertex)
79        throws IOException {
80  
81        StringBuilder str = new StringBuilder();
82        if (reverseOutput) {
83          str.append(vertex.getValue().toString());
84          str.append(delimiter);
85          str.append(vertex.getId().toString());
86        } else {
87          str.append(vertex.getId().toString());
88          str.append(delimiter);
89          str.append(vertex.getValue().toString());
90        }
91        return new Text(str.toString());
92      }
93    }
94  }