1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.examples.io.formats;
19
20 import java.io.IOException;
21 import java.util.List;
22
23 import org.apache.giraph.edge.Edge;
24 import org.apache.giraph.edge.EdgeFactory;
25 import org.apache.giraph.examples.utils.BrachaTouegDeadlockVertexValue;
26 import org.apache.giraph.graph.Vertex;
27 import org.apache.giraph.io.formats.TextVertexInputFormat;
28 import org.apache.hadoop.io.LongWritable;
29 import org.apache.hadoop.io.Text;
30 import org.apache.hadoop.mapreduce.InputSplit;
31 import org.apache.hadoop.mapreduce.TaskAttemptContext;
32 import org.json.JSONArray;
33 import org.json.JSONException;
34
35 import com.google.common.collect.Lists;
36
37
38
39
40
41 public class BrachaTouegDeadlockInputFormat extends
42 TextVertexInputFormat<LongWritable, BrachaTouegDeadlockVertexValue,
43 LongWritable> {
44
45 @Override
46 public TextVertexReader createVertexReader(InputSplit split,
47 TaskAttemptContext context) {
48 return new JsonLongLongLongLongVertexReader();
49 }
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 class JsonLongLongLongLongVertexReader
66 extends TextVertexReaderFromEachLineProcessedHandlingExceptions<JSONArray,
67 JSONException> {
68
69 @Override
70 protected JSONArray preprocessLine(Text line) throws JSONException {
71 return new JSONArray(line.toString());
72 }
73
74 @Override
75 protected LongWritable getId(JSONArray jsonVertex) throws JSONException,
76 IOException {
77 return new LongWritable(jsonVertex.getLong(0));
78 }
79
80 @Override
81 protected BrachaTouegDeadlockVertexValue getValue(JSONArray jsonVertex)
82 throws JSONException, IOException {
83
84 return new BrachaTouegDeadlockVertexValue();
85 }
86
87 @Override
88 protected Iterable<Edge<LongWritable, LongWritable>>
89 getEdges(JSONArray jsonVertex) throws JSONException, IOException {
90
91 JSONArray jsonEdgeArray = jsonVertex.getJSONArray(1);
92
93
94 List<Edge<LongWritable, LongWritable>> edges =
95 Lists.newArrayListWithCapacity(jsonEdgeArray.length());
96
97 for (int i = 0; i < jsonEdgeArray.length(); ++i) {
98 LongWritable targetId;
99 LongWritable tag;
100 JSONArray jsonEdge = jsonEdgeArray.getJSONArray(i);
101
102 targetId = new LongWritable(jsonEdge.getLong(0));
103 tag = new LongWritable((long) jsonEdge.getLong(1));
104 edges.add(EdgeFactory.create(targetId, tag));
105 }
106 return edges;
107 }
108
109 @Override
110 protected Vertex<LongWritable, BrachaTouegDeadlockVertexValue,
111 LongWritable> handleException(Text line, JSONArray jsonVertex,
112 JSONException e) {
113
114 throw new IllegalArgumentException(
115 "Couldn't get vertex from line " + line, e);
116 }
117 }
118 }