1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.giraph.io.formats;
1920import org.apache.giraph.edge.Edge;
21import org.apache.giraph.edge.EdgeFactory;
22import org.apache.giraph.graph.Vertex;
23import org.apache.hadoop.io.DoubleWritable;
24import org.apache.hadoop.io.FloatWritable;
25import org.apache.hadoop.io.LongWritable;
26import org.apache.hadoop.io.Text;
27import org.apache.hadoop.mapreduce.InputSplit;
28import org.apache.hadoop.mapreduce.TaskAttemptContext;
29import org.json.JSONArray;
30import org.json.JSONException;
3132import com.google.common.collect.Lists;
3334import java.io.IOException;
35import java.util.List;
3637/**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 */43publicclassJsonLongDoubleFloatDoubleVertexInputFormatextends44 TextVertexInputFormat<LongWritable, DoubleWritable, FloatWritable> {
4546 @Override
47publicTextVertexReader createVertexReader(InputSplit split,
48 TaskAttemptContext context) {
49returnnewJsonLongDoubleFloatDoubleVertexReader();
50 }
5152/**53 * VertexReader that features <code>double</code> vertex54 * values and <code>float</code> out-edge weights. The55 * 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 */63classJsonLongDoubleFloatDoubleVertexReaderextends64 TextVertexReaderFromEachLineProcessedHandlingExceptions<JSONArray,
65 JSONException> {
6667 @Override
68protected JSONArray preprocessLine(Text line) throws JSONException {
69returnnew JSONArray(line.toString());
70 }
7172 @Override
73protected LongWritable getId(JSONArray jsonVertex) throws JSONException,
74 IOException {
75returnnew LongWritable(jsonVertex.getLong(0));
76 }
7778 @Override
79protected DoubleWritable getValue(JSONArray jsonVertex) throws80 JSONException, IOException {
81returnnew DoubleWritable(jsonVertex.getDouble(1));
82 }
8384 @Override
85protected 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());
90for (int i = 0; i < jsonEdgeArray.length(); ++i) {
91 JSONArray jsonEdge = jsonEdgeArray.getJSONArray(i);
92 edges.add(EdgeFactory.create(new LongWritable(jsonEdge.getLong(0)),
93new FloatWritable((float) jsonEdge.getDouble(1))));
94 }
95return edges;
96 }
9798 @Override
99protected Vertex<LongWritable, DoubleWritable, FloatWritable>
100 handleException(Text line, JSONArray jsonVertex, JSONException e) {
101thrownew IllegalArgumentException(
102"Couldn't get vertex from line " + line, e);
103 }
104105 }
106 }