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.VertexInputFormat;
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 vertex input format - holds vertex input format class and36 * all parameters specifically set for that vertex input format.37 *38 * Used only with {@link MultiVertexInputFormat}39 *40 * @param <I> Vertex id41 * @param <V> Vertex data42 * @param <E> Edge data43 */44publicclass VertexInputFormatDescription<I extends WritableComparable,
45 V extends Writable, E extends Writable>
46extends InputFormatDescription<VertexInputFormat<I, V, E>> {
47/**48 * VertexInputFormats description - JSON array containing a JSON array for49 * each vertex input. Vertex input JSON arrays contain one or two elements -50 * first one is the name of vertex input class, and second one is JSON object51 * with all specific parameters for this vertex input. For example:52 * [["VIF1",{"p":"v1"}],["VIF2",{"p":"v2","q":"v"}]]53 */54publicstaticfinalStrConfOption VERTEX_INPUT_FORMAT_DESCRIPTIONS =
55newStrConfOption("giraph.multiVertexInput.descriptions", null,
56"VertexInputFormats description - JSON array containing a JSON " +
57"array for each vertex input. Vertex input JSON arrays contain " +
58"one or two elements - first one is the name of vertex input " +
59"class, and second one is JSON object with all specific parameters " +
60"for this vertex input. For example: [[\"VIF1\",{\"p\":\"v1\"}]," +
61"[\"VIF2\",{\"p\":\"v2\",\"q\":\"v\"}]]\"");
6263/**64 * Constructor with vertex input format class65 *66 * @param vertexInputFormatClass Vertex input format class67 */68publicVertexInputFormatDescription(
69 Class<? extends VertexInputFormat<I, V, E>> vertexInputFormatClass) {
70super(vertexInputFormatClass);
71 }
7273/**74 * Constructor with json string describing this input format75 *76 * @param description Json string describing this input format77 */78publicVertexInputFormatDescription(String description) {
79super(description);
80 }
8182/**83 * Create a copy of configuration which additionally has all parameters for84 * this input format set85 *86 * @param conf Configuration which we want to create a copy from87 * @return Copy of configuration88 */89private ImmutableClassesGiraphConfiguration<I, V, E>
90 createConfigurationCopy(
91 ImmutableClassesGiraphConfiguration<I, V, E> conf) {
92 ImmutableClassesGiraphConfiguration<I, V, E> confCopy =
93new ImmutableClassesGiraphConfiguration<I, V, E>(conf);
94 confCopy.setVertexInputFormatClass(getInputFormatClass());
95 putParametersToConfiguration(confCopy);
96return confCopy;
97 }
9899/**100 * Get descriptions of vertex input formats from configuration.101 *102 * @param conf Configuration103 * @param <I> Vertex id104 * @param <V> Vertex data105 * @param <E> Edge data106 * @return List of vertex input format descriptions107 */108publicstatic <I extends WritableComparable, V extends Writable,
109 E extends Writable>
110 List<VertexInputFormatDescription<I, V, E>> getVertexInputFormatDescriptions(
111 Configuration conf) {
112 String vertexInputFormatDescriptions =
113 VERTEX_INPUT_FORMAT_DESCRIPTIONS.get(conf);
114if (vertexInputFormatDescriptions == null) {
115return Lists.newArrayList();
116 }
117try {
118 JSONArray inputFormatsJson = new JSONArray(vertexInputFormatDescriptions);
119 List<VertexInputFormatDescription<I, V, E>> descriptions =
120 Lists.newArrayListWithCapacity(inputFormatsJson.length());
121for (int i = 0; i < inputFormatsJson.length(); i++) {
122 descriptions.add(new VertexInputFormatDescription<I, V, E>(
123 inputFormatsJson.getJSONArray(i).toString()));
124 }
125return descriptions;
126 } catch (JSONException e) {
127thrownew IllegalStateException("getVertexInputFormatDescriptions: " +
128"JSONException occurred while trying to process " +
129 vertexInputFormatDescriptions, e);
130 }
131 }
132133/**134 * Create all vertex input formats135 *136 * @param conf Configuration137 * @param <I> Vertex id138 * @param <V> Vertex data139 * @param <E> Edge data140 * @return List with all vertex input formats141 */142publicstatic <I extends WritableComparable, V extends Writable,
143 E extends Writable>
144 List<VertexInputFormat<I, V, E>> createVertexInputFormats(
145 ImmutableClassesGiraphConfiguration<I, V, E> conf) {
146 List<VertexInputFormatDescription<I, V, E>> descriptions =
147 getVertexInputFormatDescriptions(conf);
148 List<VertexInputFormat<I, V, E>> vertexInputFormats =
149 Lists.newArrayListWithCapacity(descriptions.size());
150for (VertexInputFormatDescription<I, V, E> description : descriptions) {
151 ImmutableClassesGiraphConfiguration<I, V, E> confCopy =
152 description.createConfigurationCopy(conf);
153 vertexInputFormats.add(confCopy.createWrappedVertexInputFormat());
154 }
155return vertexInputFormats;
156 }
157 }