This project has retired. For details please refer to its Attic page.
InMemoryVertexOutputFormat 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 org.apache.giraph.bsp.ImmutableOutputCommitter;
22  import org.apache.giraph.conf.GiraphConfiguration;
23  import org.apache.giraph.graph.Vertex;
24  import org.apache.giraph.io.VertexOutputFormat;
25  import org.apache.giraph.io.VertexWriter;
26  import org.apache.giraph.utils.TestGraph;
27  import org.apache.hadoop.io.Writable;
28  import org.apache.hadoop.io.WritableComparable;
29  import org.apache.hadoop.mapreduce.JobContext;
30  import org.apache.hadoop.mapreduce.OutputCommitter;
31  import org.apache.hadoop.mapreduce.TaskAttemptContext;
32  
33  import java.io.IOException;
34  
35  /**
36   * VertexOutputFormat which stores all vertices in memory
37   *
38   * @param <I> Vertex id
39   * @param <V> Vertex data
40   * @param <E> Edge data
41   */
42  public class InMemoryVertexOutputFormat<I extends WritableComparable,
43      V extends Writable, E extends Writable> extends
44      VertexOutputFormat<I, V, E> {
45    /** Graph where we store all vertices */
46    private static TestGraph OUTPUT_GRAPH;
47  
48    /**
49     * Initialize this output format - needs to be called before running the
50     * application. Creates new instance of TestGraph
51     *
52     * @param conf Configuration
53     */
54    public static void initializeOutputGraph(GiraphConfiguration conf) {
55      OUTPUT_GRAPH = new TestGraph(conf);
56    }
57  
58    /**
59     * Get graph containing all the vertices
60     *
61     * @param <I> Vertex id
62     * @param <V> Vertex data
63     * @param <E> Edge data
64     * @return Output graph
65     */
66    public static <I extends WritableComparable, V extends Writable,
67        E extends Writable> TestGraph<I, V, E> getOutputGraph() {
68      return OUTPUT_GRAPH;
69    }
70  
71    @Override
72    public VertexWriter<I, V, E> createVertexWriter(
73        TaskAttemptContext context) throws IOException, InterruptedException {
74      return new VertexWriter<I, V, E>() {
75        @Override
76        public void initialize(
77            TaskAttemptContext context) throws IOException, InterruptedException {
78        }
79  
80        @Override
81        public void close(
82            TaskAttemptContext context) throws IOException, InterruptedException {
83        }
84  
85        @Override
86        public void writeVertex(
87            Vertex<I, V, E> vertex) throws IOException, InterruptedException {
88          synchronized (OUTPUT_GRAPH) {
89            OUTPUT_GRAPH.addVertex(vertex);
90          }
91        }
92      };
93    }
94  
95    @Override
96    public void checkOutputSpecs(
97        JobContext context) throws IOException, InterruptedException {
98    }
99  
100   @Override
101   public OutputCommitter getOutputCommitter(
102       TaskAttemptContext context) throws IOException, InterruptedException {
103     return new ImmutableOutputCommitter();
104   }
105 }