This project has retired. For details please refer to its Attic page.
GoraTestEdgeInputFormat 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  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   * Implementation of a specific reader for a generated data bean.
32   */
33  public class GoraTestEdgeInputFormat
34    extends GoraEdgeInputFormat<LongWritable, FloatWritable> {
35  
36    /**
37     * Default constructor
38     */
39    public GoraTestEdgeInputFormat() {
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      putArtificialData();
52      return new GoraGEdgeEdgeReader();
53    }
54  
55    /**
56     * Writes data into the data store in order to test it out.
57     */
58    @SuppressWarnings("unchecked")
59    private static void putArtificialData() {
60      getDataStore().put("11-22",
61          createEdge("11-22", "11", "22", "11-22", (float)(11+22)));
62      getDataStore().put("22-11",
63          createEdge("22-11", "22", "11", "22-11", (float)(22+11)));
64      getDataStore().put("11-33",
65          createEdge("11-33", "11", "33", "11-33", (float)(11+33)));
66      getDataStore().put("33-11",
67          createEdge("33-11", "33", "11", "33-11", (float)(33+11)));
68      getDataStore().flush();
69    }
70  
71    /**
72     * Creates an edge using an id and a set of edges.
73     * @param id Vertex id.
74     * @param vertexInId Vertex source Id.
75     * @param vertexOutId Vertex destination Id.
76     * @param edgeLabel Edge label.
77     * @param edgeWeight Edge wight.
78     * @return GEdge created.
79     */
80    private static GEdge createEdge(String id, String vertexInId,
81        String vertexOutId, String edgeLabel, float edgeWeight) {
82      GEdge newEdge = new GEdge();
83      newEdge.setEdgeId(id);
84      newEdge.setVertexInId(vertexInId);
85      newEdge.setVertexOutId(vertexOutId);
86      newEdge.setLabel(edgeLabel);
87      newEdge.setEdgeWeight(edgeWeight);
88      return newEdge;
89    }
90  
91    /**
92     * Gora edge reader
93     */
94    protected class GoraGEdgeEdgeReader extends GoraEdgeReader {
95  
96      /** source vertex of the edge */
97      private LongWritable sourceId;
98  
99      /**
100      * Transforms a GoraObject into an Edge object.
101      * @param goraObject Object from Gora to be translated.
102      * @return Edge Result from transforming the gora object.
103      */
104     @Override
105     protected Edge<LongWritable, FloatWritable> transformEdge
106     (Object goraObject) {
107       Edge<LongWritable, FloatWritable> edge = null;
108       GEdge goraEdge = (GEdge) goraObject;
109       Long dest;
110       Float value;
111       dest = Long.valueOf(goraEdge.getVertexOutId().toString());
112       this.sourceId = new LongWritable();
113       this.sourceId.set(Long.valueOf(goraEdge.getVertexInId().toString()));
114       value = (float) goraEdge.getEdgeWeight();
115       edge = EdgeFactory.create(new LongWritable(dest),
116           new FloatWritable(value));
117       return edge;
118     }
119 
120     /**
121      * Gets the currentSourceId for the edge.
122      * @return LongWritable currentSourceId for the edge.
123      */
124     @Override
125     public LongWritable getCurrentSourceId() throws IOException,
126         InterruptedException {
127       return this.sourceId;
128     }
129   }
130 }