This project has retired. For details please refer to its Attic page.
SrcIdDstIdEdgeValueTextOutputFormat 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  import java.io.IOException;
22  
23  import org.apache.giraph.edge.Edge;
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  import static org.apache.giraph.conf.GiraphConstants.GIRAPH_TEXT_OUTPUT_FORMAT_SEPARATOR;
30  import static org.apache.giraph.conf.GiraphConstants.GIRAPH_TEXT_OUTPUT_FORMAT_REVERSE;
31  
32  /**
33   * Write out Edge Value with Source and Destination ID, but not the vertex
34   * value.
35   * This is a demostration output format to show the possibility to separately
36   * output edges from vertices.
37   *
38   * @param <I> Vertex index value
39   * @param <V> Vertex value
40   * @param <E> Edge value
41   */
42  @SuppressWarnings("rawtypes")
43  public class SrcIdDstIdEdgeValueTextOutputFormat<I extends WritableComparable,
44      V extends Writable, E extends Writable>
45      extends TextEdgeOutputFormat<I, V, E> {
46  
47    @Override
48    public TextEdgeWriter createEdgeWriter(TaskAttemptContext context) {
49      return new SrcIdDstIdEdgeValueEdgeWriter();
50    }
51  
52    /**
53     * Edge writer used with {@link SrcIdDstIdEdgeValueTextOutputFormat}.
54     */
55    protected class SrcIdDstIdEdgeValueEdgeWriter<I extends WritableComparable,
56      V extends Writable, E extends Writable>
57      extends TextEdgeWriterToEachLine<I, V, E> {
58  
59      /** Saved delimiter */
60      private String delimiter;
61      /** Cached reserve option */
62      private boolean reverseOutput;
63  
64      @Override
65      public void initialize(TaskAttemptContext context)
66        throws IOException, InterruptedException {
67        super.initialize(context);
68        delimiter = GIRAPH_TEXT_OUTPUT_FORMAT_SEPARATOR.get(getConf());
69        reverseOutput = GIRAPH_TEXT_OUTPUT_FORMAT_REVERSE.get(getConf());
70      }
71  
72      @Override
73      protected Text convertEdgeToLine(I sourceId, V sourceValue, Edge<I, E> edge)
74        throws IOException {
75        StringBuilder msg = new StringBuilder();
76        if (reverseOutput) {
77          msg.append(edge.getValue().toString());
78          msg.append(delimiter);
79          msg.append(edge.getTargetVertexId().toString());
80          msg.append(delimiter);
81          msg.append(sourceId.toString());
82        } else {
83          msg.append(sourceId.toString());
84          msg.append(delimiter);
85          msg.append(edge.getTargetVertexId().toString());
86          msg.append(delimiter);
87          msg.append(edge.getValue().toString());
88        }
89        return new Text(msg.toString());
90      }
91    }
92  }