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.conf.ImmutableClassesGiraphConfiguration;
22  import org.apache.giraph.conf.StrConfOption;
23  import org.apache.giraph.io.MappingInputFormat;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.io.Writable;
26  import org.apache.hadoop.io.WritableComparable;
27  import org.json.JSONArray;
28  import org.json.JSONException;
29  
30  import com.google.common.collect.Lists;
31  
32  import java.util.List;
33  
34  /**
35   * Description of the mapping input format - holds mapping input format class
36   * and all parameters specifically set for that mapping input format.
37   *
38   * Used only with {@link MultiMappingInputFormat}
39   *
40   * @param <I> Vertex id
41   * @param <V> Vertex data
42   * @param <E> Edge data
43   * @param <B> Mapping target
44   */
45  public class MappingInputFormatDescription<I extends WritableComparable,
46      V extends Writable, E extends Writable, B extends Writable>
47      extends InputFormatDescription<MappingInputFormat<I, V, E, B>> {
48    /**
49     * MappingInputFormats description - JSON array containing a JSON array for
50     * each mapping input. Mapping input JSON arrays contain one or two elements -
51     * first one is the name of mapping input class, and second one is JSON object
52     * with all specific parameters for this mapping input. For example:
53     * [["VIF1",{"p":"v1"}],["VIF2",{"p":"v2","q":"v"}]]
54     */
55    public static final StrConfOption MAPPING_INPUT_FORMAT_DESCRIPTIONS =
56        new StrConfOption("giraph.multiMappingInput.descriptions", null,
57            "MappingInputFormats description - JSON array containing a JSON " +
58            "array for each mapping input. Mapping input JSON arrays contain " +
59            "one or two elements - first one is the name of mapping input " +
60            "class, and second one is JSON object with all specific parameters " +
61            "for this mapping input. For example: [[\"VIF1\",{\"p\":\"v1\"}]," +
62            "[\"VIF2\",{\"p\":\"v2\",\"q\":\"v\"}]]\"");
63  
64    /**
65     * Constructor with mapping input format class
66     *
67     * @param mappingInputFormatClass Mapping input format class
68     */
69    public MappingInputFormatDescription(
70      Class<? extends MappingInputFormat<I, V, E, B>> mappingInputFormatClass
71    ) {
72      super(mappingInputFormatClass);
73    }
74  
75    /**
76     * Constructor with json string describing this input format
77     *
78     * @param description Json string describing this input format
79     */
80    public MappingInputFormatDescription(String description) {
81      super(description);
82    }
83  
84    /**
85     * Create a copy of configuration which additionally has all parameters for
86     * this input format set
87     *
88     * @param conf Configuration which we want to create a copy from
89     * @return Copy of configuration
90     */
91    private ImmutableClassesGiraphConfiguration<I, V, E>
92    createConfigurationCopy(
93        ImmutableClassesGiraphConfiguration<I, V, E> conf) {
94      ImmutableClassesGiraphConfiguration<I, V, E> confCopy =
95          new ImmutableClassesGiraphConfiguration<I, V, E>(conf);
96      confCopy.setMappingInputFormatClass(getInputFormatClass());
97      putParametersToConfiguration(confCopy);
98      return confCopy;
99    }
100 
101   /**
102    * Get descriptions of mapping input formats from configuration.
103    *
104    * @param conf Configuration
105    * @param <I>  Vertex id
106    * @param <V>  Vertex data
107    * @param <E>  Edge data
108    * @param <B>  Mapping target
109    * @return List of mapping input format descriptions
110    */
111   public static <I extends WritableComparable, V extends Writable,
112       E extends Writable, B extends Writable>
113   List<MappingInputFormatDescription<I, V, E, B>>
114   getMappingInputFormatDescriptions(Configuration conf) {
115     String mappingInputFormatDescriptions =
116         MAPPING_INPUT_FORMAT_DESCRIPTIONS.get(conf);
117     if (mappingInputFormatDescriptions == null) {
118       return Lists.newArrayList();
119     }
120     try {
121       JSONArray inputFormatsJson = new JSONArray(
122         mappingInputFormatDescriptions);
123       List<MappingInputFormatDescription<I, V, E, B>> descriptions =
124           Lists.newArrayListWithCapacity(inputFormatsJson.length());
125       for (int i = 0; i < inputFormatsJson.length(); i++) {
126         descriptions.add(new MappingInputFormatDescription<I, V, E, B>(
127             inputFormatsJson.getString(i)));
128       }
129       return descriptions;
130     } catch (JSONException e) {
131       throw new IllegalStateException("getMappingInputFormatDescriptions: " +
132           "JSONException occurred while trying to process " +
133           mappingInputFormatDescriptions, e);
134     }
135   }
136 
137   /**
138    * Create all mapping input formats
139    *
140    * @param conf Configuration
141    * @param <I> Vertex id
142    * @param <V> Vertex data
143    * @param <E> Edge data
144    * @param <B> Mapping target data
145    * @return List with all mapping input formats
146    */
147   public static <I extends WritableComparable, V extends Writable,
148       E extends Writable, B extends Writable>
149   List<MappingInputFormat<I, V, E, B>> createMappingInputFormats(
150       ImmutableClassesGiraphConfiguration<I, V, E> conf) {
151     List<MappingInputFormatDescription<I, V, E, B>> descriptions =
152         getMappingInputFormatDescriptions(conf);
153     List<MappingInputFormat<I, V, E, B>> mappingInputFormats =
154         Lists.newArrayListWithCapacity(descriptions.size());
155     for (MappingInputFormatDescription<I, V, E, B> description : descriptions) {
156       ImmutableClassesGiraphConfiguration<I, V, E> confCopy =
157           description.createConfigurationCopy(conf);
158       mappingInputFormats.add((MappingInputFormat<I, V, E, B>)
159                                 confCopy.createWrappedMappingInputFormat());
160     }
161     return mappingInputFormats;
162   }
163 }