This project has retired. For details please refer to its Attic page.
JsonBase64VertexOutputFormat 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 net.iharder.Base64;
22  import org.apache.giraph.edge.Edge;
23  import org.apache.giraph.graph.Vertex;
24  import org.apache.hadoop.io.Text;
25  import org.apache.hadoop.io.Writable;
26  import org.apache.hadoop.io.WritableComparable;
27  import org.apache.hadoop.mapreduce.TaskAttemptContext;
28  import org.json.JSONArray;
29  import org.json.JSONException;
30  import org.json.JSONObject;
31  
32  import java.io.ByteArrayOutputStream;
33  import java.io.DataOutput;
34  import java.io.DataOutputStream;
35  import java.io.IOException;
36  
37  /**
38   * Simple way to represent the structure of the graph with a JSON object.
39   * The actual vertex ids, values, edges are stored by the
40   * Writable serialized bytes that are Byte64 encoded.
41   * Works with {@link JsonBase64VertexInputFormat}
42   *
43   * @param <I> Vertex index value
44   * @param <V> Vertex value
45   * @param <E> Edge value
46   */
47  @SuppressWarnings("rawtypes")
48  public class JsonBase64VertexOutputFormat<I extends WritableComparable,
49      V extends Writable, E extends Writable> extends
50      TextVertexOutputFormat<I, V, E> {
51  
52    @Override
53    public TextVertexWriter createVertexWriter(TaskAttemptContext context) {
54      return new JsonBase64VertexWriter();
55    }
56  
57    /**
58     * Simple writer that supports {@link JsonBase64VertexOutputFormat}
59     */
60    protected class JsonBase64VertexWriter extends TextVertexWriterToEachLine {
61  
62      @Override
63      protected Text convertVertexToLine(Vertex<I, V, E> vertex)
64        throws IOException {
65        ByteArrayOutputStream outputStream =
66            new ByteArrayOutputStream();
67        DataOutput output = new DataOutputStream(outputStream);
68        JSONObject vertexObject = new JSONObject();
69        vertex.getId().write(output);
70        try {
71          vertexObject.put(
72            JsonBase64VertexFormat.VERTEX_ID_KEY,
73            Base64.encodeBytes(outputStream.toByteArray()));
74        } catch (JSONException e) {
75          throw new IllegalStateException(
76              "writerVertex: Failed to insert vertex id", e);
77        }
78        outputStream.reset();
79        vertex.getValue().write(output);
80        try {
81          vertexObject.put(
82            JsonBase64VertexFormat.VERTEX_VALUE_KEY,
83            Base64.encodeBytes(outputStream.toByteArray()));
84        } catch (JSONException e) {
85          throw new IllegalStateException(
86              "writerVertex: Failed to insert vertex value", e);
87        }
88        JSONArray edgeArray = new JSONArray();
89        for (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        }
95        try {
96          vertexObject.put(
97            JsonBase64VertexFormat.EDGE_ARRAY_KEY,
98            edgeArray);
99        } catch (JSONException e) {
100         throw new IllegalStateException(
101             "writerVertex: Failed to insert edge array", e);
102       }
103       return new Text(vertexObject.toString());
104     }
105 
106   }
107 
108 }