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.multi; |
20 | |
|
21 | |
import org.apache.giraph.io.GiraphInputFormat; |
22 | |
import org.apache.hadoop.conf.Configuration; |
23 | |
import org.json.JSONArray; |
24 | |
import org.json.JSONException; |
25 | |
import org.json.JSONObject; |
26 | |
|
27 | |
import com.google.common.collect.Maps; |
28 | |
|
29 | |
import java.util.Iterator; |
30 | |
import java.util.Map; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public abstract class InputFormatDescription<IF extends GiraphInputFormat> { |
42 | |
|
43 | |
private Class<? extends IF> inputFormatClass; |
44 | |
|
45 | 0 | private final Map<String, String> parameters = Maps.newHashMap(); |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public InputFormatDescription(Class<? extends IF> inputFormatClass) { |
53 | 0 | this.inputFormatClass = inputFormatClass; |
54 | 0 | } |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
@SuppressWarnings("unchecked") |
62 | 0 | public InputFormatDescription(String description) { |
63 | |
try { |
64 | 0 | JSONArray jsonArray = new JSONArray(description); |
65 | 0 | inputFormatClass = |
66 | 0 | (Class<? extends IF>) Class.forName(jsonArray.getString(0)); |
67 | 0 | if (jsonArray.length() > 1) { |
68 | 0 | addParameters(jsonArray.getJSONObject(1)); |
69 | |
} |
70 | 0 | } catch (JSONException e) { |
71 | 0 | throw new IllegalStateException( |
72 | |
"Failed to parse JSON " + description, e); |
73 | 0 | } catch (ClassNotFoundException e) { |
74 | 0 | throw new IllegalStateException("Couldn't find " + |
75 | |
"input format class from description " + description, e); |
76 | 0 | } |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public void addParameter(String name, String value) { |
86 | 0 | parameters.put(name, value); |
87 | 0 | } |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public void addParameters(JSONObject parametersJson) { |
95 | 0 | Iterator<?> keys = parametersJson.keys(); |
96 | 0 | while (keys.hasNext()) { |
97 | 0 | String key = (String) keys.next(); |
98 | |
try { |
99 | 0 | addParameter(key, parametersJson.getString(key)); |
100 | 0 | } catch (JSONException e) { |
101 | 0 | throw new IllegalStateException("addParameters: Failed to parse " + |
102 | |
parametersJson, e); |
103 | 0 | } |
104 | 0 | } |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public JSONArray toJsonArray() { |
113 | 0 | JSONArray jsonArray = new JSONArray(); |
114 | 0 | jsonArray.put(inputFormatClass.getName()); |
115 | 0 | JSONObject jsonParameters = new JSONObject(); |
116 | 0 | for (Map.Entry<String, String> entry : parameters.entrySet()) { |
117 | |
try { |
118 | 0 | jsonParameters.put(entry.getKey(), entry.getValue()); |
119 | 0 | } catch (JSONException e) { |
120 | 0 | throw new IllegalStateException("toJsonArray: JSONException occurred " + |
121 | 0 | "while trying to process (" + entry.getKey() + ", " + |
122 | 0 | entry.getValue() + ")", e); |
123 | 0 | } |
124 | 0 | } |
125 | 0 | jsonArray.put(jsonParameters); |
126 | 0 | return jsonArray; |
127 | |
} |
128 | |
|
129 | |
public Class<? extends IF> getInputFormatClass() { |
130 | 0 | return inputFormatClass; |
131 | |
} |
132 | |
|
133 | |
public void setInputFormatClass(Class<? extends IF> inputFormatClass) { |
134 | 0 | this.inputFormatClass = inputFormatClass; |
135 | 0 | } |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public void putParametersToConfiguration(Configuration conf) { |
143 | 0 | for (Map.Entry<String, String> entry : parameters.entrySet()) { |
144 | 0 | conf.set(entry.getKey(), entry.getValue()); |
145 | 0 | } |
146 | 0 | } |
147 | |
|
148 | |
@Override |
149 | |
public String toString() { |
150 | 0 | return toJsonArray().toString(); |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
public static String toJsonString( |
160 | |
Iterable<? extends InputFormatDescription> descriptions) { |
161 | 0 | JSONArray jsonArray = new JSONArray(); |
162 | 0 | for (InputFormatDescription description : descriptions) { |
163 | 0 | jsonArray.put(description.toJsonArray()); |
164 | 0 | } |
165 | 0 | return jsonArray.toString(); |
166 | |
} |
167 | |
} |