Coverage Report - org.apache.giraph.io.formats.JsonBase64VertexOutputFormat
 
Classes in this File Line Coverage Branch Coverage Complexity
JsonBase64VertexOutputFormat
0%
0/3
N/A
4.5
JsonBase64VertexOutputFormat$JsonBase64VertexWriter
0%
0/29
0%
0/2
4.5
 
 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  0
 @SuppressWarnings("rawtypes")
 48  0
 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  0
     return new JsonBase64VertexWriter();
 55  
   }
 56  
 
 57  
   /**
 58  
    * Simple writer that supports {@link JsonBase64VertexOutputFormat}
 59  
    */
 60  0
   protected class JsonBase64VertexWriter extends TextVertexWriterToEachLine {
 61  
 
 62  
     @Override
 63  
     protected Text convertVertexToLine(Vertex<I, V, E> vertex)
 64  
       throws IOException {
 65  0
       ByteArrayOutputStream outputStream =
 66  
           new ByteArrayOutputStream();
 67  0
       DataOutput output = new DataOutputStream(outputStream);
 68  0
       JSONObject vertexObject = new JSONObject();
 69  0
       vertex.getId().write(output);
 70  
       try {
 71  0
         vertexObject.put(
 72  
           JsonBase64VertexFormat.VERTEX_ID_KEY,
 73  0
           Base64.encodeBytes(outputStream.toByteArray()));
 74  0
       } catch (JSONException e) {
 75  0
         throw new IllegalStateException(
 76  
             "writerVertex: Failed to insert vertex id", e);
 77  0
       }
 78  0
       outputStream.reset();
 79  0
       vertex.getValue().write(output);
 80  
       try {
 81  0
         vertexObject.put(
 82  
           JsonBase64VertexFormat.VERTEX_VALUE_KEY,
 83  0
           Base64.encodeBytes(outputStream.toByteArray()));
 84  0
       } catch (JSONException e) {
 85  0
         throw new IllegalStateException(
 86  
             "writerVertex: Failed to insert vertex value", e);
 87  0
       }
 88  0
       JSONArray edgeArray = new JSONArray();
 89  0
       for (Edge<I, E> edge : vertex.getEdges()) {
 90  0
         outputStream.reset();
 91  0
         edge.getTargetVertexId().write(output);
 92  0
         edge.getValue().write(output);
 93  0
         edgeArray.put(Base64.encodeBytes(outputStream.toByteArray()));
 94  0
       }
 95  
       try {
 96  0
         vertexObject.put(
 97  
           JsonBase64VertexFormat.EDGE_ARRAY_KEY,
 98  
           edgeArray);
 99  0
       } catch (JSONException e) {
 100  0
         throw new IllegalStateException(
 101  
             "writerVertex: Failed to insert edge array", e);
 102  0
       }
 103  0
       return new Text(vertexObject.toString());
 104  
     }
 105  
 
 106  
   }
 107  
 
 108  
 }