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  package org.apache.giraph.io.gora;
19  
20  import java.io.IOException;
21  
22  import org.apache.giraph.edge.Edge;
23  import org.apache.giraph.edge.EdgeFactory;
24  import org.apache.giraph.io.gora.generated.GEdge;
25  import org.apache.hadoop.io.FloatWritable;
26  import org.apache.hadoop.io.LongWritable;
27  import org.apache.hadoop.mapreduce.InputSplit;
28  import org.apache.hadoop.mapreduce.TaskAttemptContext;
29  
30  /**
31   * Example implementation of a specific reader for a generated data bean.
32   */
33  public class GoraGEdgeEdgeInputFormat
34    extends GoraEdgeInputFormat<LongWritable, FloatWritable> {
35  
36    /**
37     * Default constructor
38     */
39    public GoraGEdgeEdgeInputFormat() {
40    }
41  
42    /**
43     * Creates specific vertex reader to be used inside Hadoop.
44     * @param split split to be read.
45     * @param context JobContext to be used.
46     * @return GoraEdgeReader Edge reader to be used by Hadoop.
47     */
48    @Override
49    public GoraEdgeReader createEdgeReader(
50        InputSplit split, TaskAttemptContext context) throws IOException {
51      return new GoraGEdgeEdgeReader();
52    }
53  
54    /**
55     * Gora edge reader
56     */
57    protected class GoraGEdgeEdgeReader extends GoraEdgeReader {
58  
59      /** source vertex of the edge */
60      private LongWritable sourceId;
61  
62      /**
63       * Transforms a GoraObject into an Edge object.
64       * @param goraObject Object from Gora to be translated.
65       * @return Edge Result from transforming the gora object.
66       */
67      @Override
68      protected Edge<LongWritable, FloatWritable> transformEdge
69      (Object goraObject) {
70        Edge<LongWritable, FloatWritable> edge = null;
71        GEdge goraEdge = (GEdge) goraObject;
72        this.sourceId = new LongWritable();
73        this.sourceId.set(Long.parseLong(goraEdge.getVertexInId().toString()));
74        edge = EdgeFactory.create(
75            new LongWritable(
76                Long.parseLong(goraEdge.getVertexOutId().toString())),
77            new FloatWritable(goraEdge.getEdgeWeight()));
78        return edge;
79      }
80  
81      /**
82       * Gets the currentSourceId for the edge.
83       * @return LongWritable currentSourceId for the edge.
84       */
85      @Override
86      public LongWritable getCurrentSourceId() throws IOException,
87          InterruptedException {
88        return this.sourceId;
89      }
90    }
91  }