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 | 0 | public class JsonLongDoubleFloatDoubleVertexOutputFormat extends |
38 | |
TextVertexOutputFormat<LongWritable, DoubleWritable, |
39 | |
FloatWritable> { |
40 | |
|
41 | |
@Override |
42 | |
public TextVertexWriter createVertexWriter( |
43 | |
TaskAttemptContext context) { |
44 | 0 | return new JsonLongDoubleFloatDoubleVertexWriter(); |
45 | |
} |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | private class JsonLongDoubleFloatDoubleVertexWriter extends |
52 | |
TextVertexWriterToEachLine { |
53 | |
@Override |
54 | |
public Text convertVertexToLine( |
55 | |
Vertex<LongWritable, DoubleWritable, FloatWritable> vertex |
56 | |
) throws IOException { |
57 | 0 | JSONArray jsonVertex = new JSONArray(); |
58 | |
try { |
59 | 0 | jsonVertex.put(vertex.getId().get()); |
60 | 0 | jsonVertex.put(vertex.getValue().get()); |
61 | 0 | JSONArray jsonEdgeArray = new JSONArray(); |
62 | 0 | for (Edge<LongWritable, FloatWritable> edge : vertex.getEdges()) { |
63 | 0 | JSONArray jsonEdge = new JSONArray(); |
64 | 0 | jsonEdge.put(edge.getTargetVertexId().get()); |
65 | 0 | jsonEdge.put(edge.getValue().get()); |
66 | 0 | jsonEdgeArray.put(jsonEdge); |
67 | 0 | } |
68 | 0 | jsonVertex.put(jsonEdgeArray); |
69 | 0 | } catch (JSONException e) { |
70 | 0 | throw new IllegalArgumentException( |
71 | |
"writeVertex: Couldn't write vertex " + vertex); |
72 | 0 | } |
73 | 0 | return new Text(jsonVertex.toString()); |
74 | |
} |
75 | |
} |
76 | |
} |