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 */1819package org.apache.giraph.io.formats;
2021import org.apache.giraph.edge.Edge;
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.TaskAttemptContext;
28import org.json.JSONArray;
29import org.json.JSONException;
3031import java.io.IOException;
3233/**34 * VertexOutputFormat that supports JSON encoded vertices featuring35 * <code>double</code> values and <code>float</code> out-edge weights36 */37publicclassJsonLongDoubleFloatDoubleVertexOutputFormatextends38 TextVertexOutputFormat<LongWritable, DoubleWritable,
39 FloatWritable> {
4041 @Override
42publicTextVertexWriter createVertexWriter(
43 TaskAttemptContext context) {
44returnnewJsonLongDoubleFloatDoubleVertexWriter();
45 }
4647/**48 * VertexWriter that supports vertices with <code>double</code>49 * values and <code>float</code> out-edge weights.50 */51privateclassJsonLongDoubleFloatDoubleVertexWriterextends52TextVertexWriterToEachLine {
53 @Override
54public Text convertVertexToLine(
55 Vertex<LongWritable, DoubleWritable, FloatWritable> vertex
56 ) throws IOException {
57 JSONArray jsonVertex = new JSONArray();
58try {
59 jsonVertex.put(vertex.getId().get());
60 jsonVertex.put(vertex.getValue().get());
61 JSONArray jsonEdgeArray = new JSONArray();
62for (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) {
70thrownew IllegalArgumentException(
71"writeVertex: Couldn't write vertex " + vertex);
72 }
73returnnew Text(jsonVertex.toString());
74 }
75 }
76 }