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