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 net.iharder.Base64;
22import org.apache.giraph.edge.Edge;
23import org.apache.giraph.graph.Vertex;
24import org.apache.hadoop.io.Text;
25import org.apache.hadoop.io.Writable;
26import org.apache.hadoop.io.WritableComparable;
27import org.apache.hadoop.mapreduce.TaskAttemptContext;
28import org.json.JSONArray;
29import org.json.JSONException;
30import org.json.JSONObject;
3132import java.io.ByteArrayOutputStream;
33import java.io.DataOutput;
34import java.io.DataOutputStream;
35import java.io.IOException;
3637/**38 * Simple way to represent the structure of the graph with a JSON object.39 * The actual vertex ids, values, edges are stored by the40 * Writable serialized bytes that are Byte64 encoded.41 * Works with {@link JsonBase64VertexInputFormat}42 *43 * @param <I> Vertex index value44 * @param <V> Vertex value45 * @param <E> Edge value46 */47 @SuppressWarnings("rawtypes")
48publicclass JsonBase64VertexOutputFormat<I extends WritableComparable,
49 V extends Writable, E extends Writable> extends50 TextVertexOutputFormat<I, V, E> {
5152 @Override
53publicTextVertexWriter createVertexWriter(TaskAttemptContext context) {
54returnnewJsonBase64VertexWriter();
55 }
5657/**58 * Simple writer that supports {@link JsonBase64VertexOutputFormat}59 */60protectedclassJsonBase64VertexWriterextendsTextVertexWriterToEachLine {
6162 @Override
63protected Text convertVertexToLine(Vertex<I, V, E> vertex)
64throws IOException {
65 ByteArrayOutputStream outputStream =
66new ByteArrayOutputStream();
67 DataOutput output = new DataOutputStream(outputStream);
68 JSONObject vertexObject = new JSONObject();
69 vertex.getId().write(output);
70try {
71 vertexObject.put(
72 JsonBase64VertexFormat.VERTEX_ID_KEY,
73 Base64.encodeBytes(outputStream.toByteArray()));
74 } catch (JSONException e) {
75thrownew IllegalStateException(
76"writerVertex: Failed to insert vertex id", e);
77 }
78 outputStream.reset();
79 vertex.getValue().write(output);
80try {
81 vertexObject.put(
82 JsonBase64VertexFormat.VERTEX_VALUE_KEY,
83 Base64.encodeBytes(outputStream.toByteArray()));
84 } catch (JSONException e) {
85thrownew IllegalStateException(
86"writerVertex: Failed to insert vertex value", e);
87 }
88 JSONArray edgeArray = new JSONArray();
89for (Edge<I, E> edge : vertex.getEdges()) {
90 outputStream.reset();
91 edge.getTargetVertexId().write(output);
92 edge.getValue().write(output);
93 edgeArray.put(Base64.encodeBytes(outputStream.toByteArray()));
94 }
95try {
96 vertexObject.put(
97 JsonBase64VertexFormat.EDGE_ARRAY_KEY,
98 edgeArray);
99 } catch (JSONException e) {
100thrownew IllegalStateException(
101"writerVertex: Failed to insert edge array", e);
102 }
103returnnew Text(vertexObject.toString());
104 }
105106 }
107108 }