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.avro.util.Utf8;
23  import org.apache.giraph.edge.Edge;
24  import org.apache.giraph.io.gora.generated.GEdge;
25  import org.apache.giraph.io.gora.generated.GEdgeResult;
26  import org.apache.gora.persistency.Persistent;
27  import org.apache.hadoop.io.DoubleWritable;
28  import org.apache.hadoop.io.FloatWritable;
29  import org.apache.hadoop.io.LongWritable;
30  import org.apache.hadoop.mapreduce.TaskAttemptContext;
31  import org.junit.Assert;
32  
33  /**
34   * Implementation of a specific writer for a generated data bean.
35   */
36  public class GoraTestEdgeOutputFormat
37    extends GoraEdgeOutputFormat<LongWritable, DoubleWritable,
38    FloatWritable> {
39  
40    /**
41     * Default constructor
42     */
43    public GoraTestEdgeOutputFormat() {
44    }
45  
46    @Override
47    public GoraEdgeWriter createEdgeWriter(
48        TaskAttemptContext context) throws IOException, InterruptedException {
49      return new GoraGEdgeEdgeWriter();
50    }
51  
52    /**
53     * Gora edge writer.
54     */
55    protected class GoraGEdgeEdgeWriter
56      extends GoraEdgeWriter {
57  
58      @Override
59      protected Persistent getGoraEdge(LongWritable srcId,
60          DoubleWritable srcValue, Edge<LongWritable, FloatWritable> edge) {
61        GEdgeResult tmpGEdge = new GEdgeResult();
62        Utf8 keyLabel = new Utf8(srcId.toString() + "-" +
63        edge.getTargetVertexId().toString());
64        tmpGEdge.setEdgeId(keyLabel.toString());
65        tmpGEdge.setEdgeWeight(edge.getValue().get());
66        tmpGEdge.setVertexInId(srcId.toString());
67        tmpGEdge.setVertexOutId(edge.getTargetVertexId().toString());
68        tmpGEdge.setLabel(keyLabel.toString());
69        getLogger().debug("GoraObject created: " + tmpGEdge.toString());
70        return tmpGEdge;
71      }
72  
73      @Override
74      protected Object getGoraKey(LongWritable srcId,
75          DoubleWritable srcValue, Edge<LongWritable, FloatWritable> edge) {
76        String goraKey = String.valueOf(
77            edge.getTargetVertexId().get() + edge.getValue().get());
78        return goraKey;
79      }
80  
81      @SuppressWarnings("unchecked")
82      @Override
83      public void writeEdge(LongWritable srcId, DoubleWritable srcValue,
84          Edge<LongWritable, FloatWritable> edge)
85          throws IOException, InterruptedException {
86        super.writeEdge(srcId, srcValue, edge);
87        Object goraKey = getGoraKey(srcId, srcValue, edge);
88        String keyLabel = String.valueOf(srcId) + "-" +
89            String.valueOf(edge.getTargetVertexId());
90        float weight = Float.valueOf(srcId.toString()) +
91            Float.valueOf(edge.getTargetVertexId().toString());
92        // Asserting
93        Assert.assertEquals(createEdge(keyLabel, String.valueOf(srcId),
94                String.valueOf(edge.getTargetVertexId()),keyLabel, weight),
95                getDataStore().get(goraKey));
96      }
97  
98      /**
99       * Creates an edge using an id and a set of edges.
100      * @param id Vertex id.
101      * @param vertexInId Vertex source Id.
102      * @param vertexOutId Vertex destination Id.
103      * @param edgeLabel Edge label.
104      * @param edgeWeight Edge wight.
105      * @return GEdge created.
106      */
107     private GEdgeResult createEdge(String id, String vertexInId,
108         String vertexOutId, String edgeLabel, float edgeWeight) {
109       GEdgeResult newEdge = new GEdgeResult();
110       newEdge.setEdgeId(id);
111       newEdge.setVertexInId(vertexInId);
112       newEdge.setVertexOutId(vertexOutId);
113       newEdge.setLabel(edgeLabel);
114       newEdge.setEdgeWeight(edgeWeight);
115       return newEdge;
116     }
117   }
118 }