This project has retired. For details please refer to its Attic page.
JsonLongDoubleFloatDoubleVertexOutputFormat 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.edge.Edge;
22  import org.apache.giraph.graph.Vertex;
23  import org.apache.hadoop.io.DoubleWritable;
24  import org.apache.hadoop.io.FloatWritable;
25  import org.apache.hadoop.io.LongWritable;
26  import org.apache.hadoop.io.Text;
27  import org.apache.hadoop.mapreduce.TaskAttemptContext;
28  import org.json.JSONArray;
29  import org.json.JSONException;
30  
31  import java.io.IOException;
32  
33  /**
34   * VertexOutputFormat that supports JSON encoded vertices featuring
35   * <code>double</code> values and <code>float</code> out-edge weights
36   */
37  public class JsonLongDoubleFloatDoubleVertexOutputFormat extends
38    TextVertexOutputFormat<LongWritable, DoubleWritable,
39    FloatWritable> {
40  
41    @Override
42    public TextVertexWriter createVertexWriter(
43        TaskAttemptContext context) {
44      return new JsonLongDoubleFloatDoubleVertexWriter();
45    }
46  
47   /**
48    * VertexWriter that supports vertices with <code>double</code>
49    * values and <code>float</code> out-edge weights.
50    */
51    private class JsonLongDoubleFloatDoubleVertexWriter extends
52      TextVertexWriterToEachLine {
53      @Override
54      public Text convertVertexToLine(
55        Vertex<LongWritable, DoubleWritable, FloatWritable> vertex
56      ) throws IOException {
57        JSONArray jsonVertex = new JSONArray();
58        try {
59          jsonVertex.put(vertex.getId().get());
60          jsonVertex.put(vertex.getValue().get());
61          JSONArray jsonEdgeArray = new JSONArray();
62          for (Edge<LongWritable, FloatWritable> edge : vertex.getEdges()) {
63            JSONArray jsonEdge = new JSONArray();
64            jsonEdge.put(edge.getTargetVertexId().get());
65            jsonEdge.put(edge.getValue().get());
66            jsonEdgeArray.put(jsonEdge);
67          }
68          jsonVertex.put(jsonEdgeArray);
69        } catch (JSONException e) {
70          throw new IllegalArgumentException(
71            "writeVertex: Couldn't write vertex " + vertex);
72        }
73        return new Text(jsonVertex.toString());
74      }
75    }
76  }