1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.giraph.io.formats.multi;
2021import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
22import org.apache.giraph.conf.StrConfOption;
23import org.apache.giraph.io.MappingInputFormat;
24import org.apache.hadoop.conf.Configuration;
25import org.apache.hadoop.io.Writable;
26import org.apache.hadoop.io.WritableComparable;
27import org.json.JSONArray;
28import org.json.JSONException;
2930import com.google.common.collect.Lists;
3132import java.util.List;
3334/**35 * Description of the mapping input format - holds mapping input format class36 * and all parameters specifically set for that mapping input format.37 *38 * Used only with {@link MultiMappingInputFormat}39 *40 * @param <I> Vertex id41 * @param <V> Vertex data42 * @param <E> Edge data43 * @param <B> Mapping target44 */45publicclass MappingInputFormatDescription<I extends WritableComparable,
46 V extends Writable, E extends Writable, B extends Writable>
47extends InputFormatDescription<MappingInputFormat<I, V, E, B>> {
48/**49 * MappingInputFormats description - JSON array containing a JSON array for50 * 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 object52 * with all specific parameters for this mapping input. For example:53 * [["VIF1",{"p":"v1"}],["VIF2",{"p":"v2","q":"v"}]]54 */55publicstaticfinalStrConfOption MAPPING_INPUT_FORMAT_DESCRIPTIONS =
56newStrConfOption("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\"}]]\"");
6364/**65 * Constructor with mapping input format class66 *67 * @param mappingInputFormatClass Mapping input format class68 */69publicMappingInputFormatDescription(
70 Class<? extends MappingInputFormat<I, V, E, B>> mappingInputFormatClass
71 ) {
72super(mappingInputFormatClass);
73 }
7475/**76 * Constructor with json string describing this input format77 *78 * @param description Json string describing this input format79 */80publicMappingInputFormatDescription(String description) {
81super(description);
82 }
8384/**85 * Create a copy of configuration which additionally has all parameters for86 * this input format set87 *88 * @param conf Configuration which we want to create a copy from89 * @return Copy of configuration90 */91private ImmutableClassesGiraphConfiguration<I, V, E>
92 createConfigurationCopy(
93 ImmutableClassesGiraphConfiguration<I, V, E> conf) {
94 ImmutableClassesGiraphConfiguration<I, V, E> confCopy =
95new ImmutableClassesGiraphConfiguration<I, V, E>(conf);
96 confCopy.setMappingInputFormatClass(getInputFormatClass());
97 putParametersToConfiguration(confCopy);
98return confCopy;
99 }
100101/**102 * Get descriptions of mapping input formats from configuration.103 *104 * @param conf Configuration105 * @param <I> Vertex id106 * @param <V> Vertex data107 * @param <E> Edge data108 * @param <B> Mapping target109 * @return List of mapping input format descriptions110 */111publicstatic <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);
117if (mappingInputFormatDescriptions == null) {
118return Lists.newArrayList();
119 }
120try {
121 JSONArray inputFormatsJson = new JSONArray(
122 mappingInputFormatDescriptions);
123 List<MappingInputFormatDescription<I, V, E, B>> descriptions =
124 Lists.newArrayListWithCapacity(inputFormatsJson.length());
125for (int i = 0; i < inputFormatsJson.length(); i++) {
126 descriptions.add(new MappingInputFormatDescription<I, V, E, B>(
127 inputFormatsJson.getJSONArray(i).toString()));
128 }
129return descriptions;
130 } catch (JSONException e) {
131thrownew IllegalStateException("getMappingInputFormatDescriptions: " +
132"JSONException occurred while trying to process " +
133 mappingInputFormatDescriptions, e);
134 }
135 }
136137/**138 * Create all mapping input formats139 *140 * @param conf Configuration141 * @param <I> Vertex id142 * @param <V> Vertex data143 * @param <E> Edge data144 * @param <B> Mapping target data145 * @return List with all mapping input formats146 */147publicstatic <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());
155for (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 }
161return mappingInputFormats;
162 }
163 }