1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.giraph.io.formats;
20
21 import org.apache.giraph.edge.Edge;
22 import org.apache.giraph.graph.Vertex;
23 import org.apache.hadoop.io.DoubleWritable;
24 import org.apache.hadoop.io.FloatWritable;
25 import org.apache.hadoop.io.LongWritable;
26 import org.apache.hadoop.io.Text;
27 import org.apache.hadoop.mapreduce.TaskAttemptContext;
28 import org.json.JSONArray;
29 import org.json.JSONException;
30
31 import java.io.IOException;
32
33
34
35
36
37 public class JsonLongDoubleFloatDoubleVertexOutputFormat extends
38 TextVertexOutputFormat<LongWritable, DoubleWritable,
39 FloatWritable> {
40
41 @Override
42 public TextVertexWriter createVertexWriter(
43 TaskAttemptContext context) {
44 return new JsonLongDoubleFloatDoubleVertexWriter();
45 }
46
47
48
49
50
51 private class JsonLongDoubleFloatDoubleVertexWriter extends
52 TextVertexWriterToEachLine {
53 @Override
54 public Text convertVertexToLine(
55 Vertex<LongWritable, DoubleWritable, FloatWritable> vertex
56 ) throws IOException {
57 JSONArray jsonVertex = new JSONArray();
58 try {
59 jsonVertex.put(vertex.getId().get());
60 jsonVertex.put(vertex.getValue().get());
61 JSONArray jsonEdgeArray = new JSONArray();
62 for (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) {
70 throw new IllegalArgumentException(
71 "writeVertex: Couldn't write vertex " + vertex);
72 }
73 return new Text(jsonVertex.toString());
74 }
75 }
76 }