This project has retired. For details please refer to its Attic page.
JsonLongDoubleFloatDoubleVertexInputFormat 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.formats;
19  
20  import org.apache.giraph.edge.Edge;
21  import org.apache.giraph.edge.EdgeFactory;
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.InputSplit;
28  import org.apache.hadoop.mapreduce.TaskAttemptContext;
29  import org.json.JSONArray;
30  import org.json.JSONException;
31  
32  import com.google.common.collect.Lists;
33  
34  import java.io.IOException;
35  import java.util.List;
36  
37  /**
38    * VertexInputFormat that features <code>long</code> vertex ID's,
39    * <code>double</code> vertex values and <code>float</code>
40    * out-edge weights, and <code>double</code> message types,
41    *  specified in JSON format.
42    */
43  public class JsonLongDoubleFloatDoubleVertexInputFormat extends
44    TextVertexInputFormat<LongWritable, DoubleWritable, FloatWritable> {
45  
46    @Override
47    public TextVertexReader createVertexReader(InputSplit split,
48        TaskAttemptContext context) {
49      return new JsonLongDoubleFloatDoubleVertexReader();
50    }
51  
52   /**
53    * VertexReader that features <code>double</code> vertex
54    * values and <code>float</code> out-edge weights. The
55    * files should be in the following JSON format:
56    * JSONArray(<vertex id>, <vertex value>,
57    *   JSONArray(JSONArray(<dest vertex id>, <edge value>), ...))
58    * Here is an example with vertex id 1, vertex value 4.3, and two edges.
59    * First edge has a destination vertex 2, edge value 2.1.
60    * Second edge has a destination vertex 3, edge value 0.7.
61    * [1,4.3,[[2,2.1],[3,0.7]]]
62    */
63    class JsonLongDoubleFloatDoubleVertexReader extends
64      TextVertexReaderFromEachLineProcessedHandlingExceptions<JSONArray,
65      JSONException> {
66  
67      @Override
68      protected JSONArray preprocessLine(Text line) throws JSONException {
69        return new JSONArray(line.toString());
70      }
71  
72      @Override
73      protected LongWritable getId(JSONArray jsonVertex) throws JSONException,
74                IOException {
75        return new LongWritable(jsonVertex.getLong(0));
76      }
77  
78      @Override
79      protected DoubleWritable getValue(JSONArray jsonVertex) throws
80        JSONException, IOException {
81        return new DoubleWritable(jsonVertex.getDouble(1));
82      }
83  
84      @Override
85      protected Iterable<Edge<LongWritable, FloatWritable>> getEdges(
86          JSONArray jsonVertex) throws JSONException, IOException {
87        JSONArray jsonEdgeArray = jsonVertex.getJSONArray(2);
88        List<Edge<LongWritable, FloatWritable>> edges =
89            Lists.newArrayListWithCapacity(jsonEdgeArray.length());
90        for (int i = 0; i < jsonEdgeArray.length(); ++i) {
91          JSONArray jsonEdge = jsonEdgeArray.getJSONArray(i);
92          edges.add(EdgeFactory.create(new LongWritable(jsonEdge.getLong(0)),
93              new FloatWritable((float) jsonEdge.getDouble(1))));
94        }
95        return edges;
96      }
97  
98      @Override
99      protected Vertex<LongWritable, DoubleWritable, FloatWritable>
100     handleException(Text line, JSONArray jsonVertex, JSONException e) {
101       throw new IllegalArgumentException(
102           "Couldn't get vertex from line " + line, e);
103     }
104 
105   }
106 }