This project has retired. For details please refer to its Attic page.
InputFormatDescription xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Description of the input format - holds input format class and all
34   * parameters specifically set for that input format.
35   *
36   * Used only with input formats which wrap several input formats into one
37   * ({@link MultiVertexInputFormat} and {@link MultiEdgeInputFormat})
38   *
39   * @param <IF> Input format type
40   */
41  public abstract class InputFormatDescription<IF extends GiraphInputFormat> {
42    /** Input format class */
43    private Class<? extends IF> inputFormatClass;
44    /** Parameters set specifically for this input format */
45    private final Map<String, String> parameters = Maps.newHashMap();
46  
47    /**
48     * Constructor with input format class
49     *
50     * @param inputFormatClass Input format class
51     */
52    public InputFormatDescription(Class<? extends IF> inputFormatClass) {
53      this.inputFormatClass = inputFormatClass;
54    }
55  
56    /**
57     * Constructor with json string describing this input format
58     *
59     * @param description Json string describing this input format
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     * Add parameter to this input format description
81     *
82     * @param name  Parameter name
83     * @param value Parameter value
84     */
85    public void addParameter(String name, String value) {
86      parameters.put(name, value);
87    }
88  
89    /**
90     * Add all parameters from json object
91     *
92     * @param parametersJson Json object to read parameters from
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    * Convert input format description to json array
109    *
110    * @return Json array representing this input format description
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    * Put parameters from this input format description to configuration
139    *
140    * @param conf Configuration to put parameters to
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    * Create JSON string for the InputFormatDescriptions
155    *
156    * @param descriptions InputFormatDescriptions
157    * @return JSON string describing these InputFormatDescriptions
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 }