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.EdgeInputFormat;
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 edge input format - holds edge input format class and all36 * parameters specifically set for that edge input format.37 *38 * Used only with {@link MultiEdgeInputFormat}39 *40 * @param <I> Vertex id41 * @param <E> Edge data42 */43publicclass EdgeInputFormatDescription<I extends WritableComparable,
44 E extends Writable> extends InputFormatDescription<EdgeInputFormat<I, E>> {
45/**46 * EdgeInputFormats description - JSON array containing a JSON array for47 * each edge input. Edge input JSON arrays contain one or two elements -48 * first one is the name of edge input class, and second one is JSON object49 * with all specific parameters for this edge input. For example:50 * [["EIF1",{"p":"v1"}],["EIF2",{"p":"v2","q":"v"}]]51 */52publicstaticfinalStrConfOption EDGE_INPUT_FORMAT_DESCRIPTIONS =
53newStrConfOption("giraph.multiEdgeInput.descriptions", null,
54"EdgeInputFormats description - JSON array containing a JSON array " +
55"for each edge input. Edge input JSON arrays contain one or two " +
56"elements - first one is the name of edge input class, and second " +
57"one is JSON object with all specific parameters for this edge " +
58"input. For example: [[\"EIF1\",{\"p\":\"v1\"}]," +
59"[\"EIF2\",{\"p\":\"v2\",\"q\":\"v\"}]]");
6061/**62 * Constructor with edge input format class63 *64 * @param edgeInputFormatClass Edge input format class65 */66publicEdgeInputFormatDescription(
67 Class<? extends EdgeInputFormat<I, E>> edgeInputFormatClass) {
68super(edgeInputFormatClass);
69 }
7071/**72 * Constructor with json string describing this input format73 *74 * @param description Json string describing this input format75 */76publicEdgeInputFormatDescription(String description) {
77super(description);
78 }
7980/**81 * Create a copy of configuration which additionally has all parameters for82 * this input format set83 *84 * @param conf Configuration which we want to create a copy from85 * @return Copy of configuration86 */87private ImmutableClassesGiraphConfiguration<I, Writable, E>
88 createConfigurationCopy(
89 ImmutableClassesGiraphConfiguration<I, Writable, E> conf) {
90 ImmutableClassesGiraphConfiguration<I, Writable, E> confCopy =
91new ImmutableClassesGiraphConfiguration<I, Writable, E>(conf);
92 confCopy.setEdgeInputFormatClass(getInputFormatClass());
93 putParametersToConfiguration(confCopy);
94return confCopy;
95 }
9697/**98 * Get descriptions of edge input formats from configuration.99 *100 * @param conf Configuration101 * @param <I> Vertex id102 * @param <E> Edge data103 * @return List of edge input format descriptions104 */105publicstatic <I extends WritableComparable, E extends Writable>
106 List<EdgeInputFormatDescription<I, E>> getEdgeInputFormatDescriptions(
107 Configuration conf) {
108 String edgeInputFormatDescriptions =
109 EDGE_INPUT_FORMAT_DESCRIPTIONS.get(conf);
110if (edgeInputFormatDescriptions == null) {
111return Lists.newArrayList();
112 }
113try {
114 JSONArray inputFormatsJson = new JSONArray(edgeInputFormatDescriptions);
115 List<EdgeInputFormatDescription<I, E>> descriptions =
116 Lists.newArrayListWithCapacity(inputFormatsJson.length());
117for (int i = 0; i < inputFormatsJson.length(); i++) {
118 descriptions.add(new EdgeInputFormatDescription<I, E>(
119 inputFormatsJson.getJSONArray(i).toString()));
120 }
121return descriptions;
122 } catch (JSONException e) {
123thrownew IllegalStateException("getEdgeInputFormatDescriptions: " +
124"JSONException occurred while trying to process " +
125 edgeInputFormatDescriptions, e);
126 }
127 }
128129/**130 * Create all edge input formats131 *132 * @param conf Configuration133 * @param <I> Vertex id134 * @param <E> Edge data135 * @return List with all edge input formats136 */137publicstatic <I extends WritableComparable,
138 E extends Writable> List<EdgeInputFormat<I, E>> createEdgeInputFormats(
139 ImmutableClassesGiraphConfiguration<I, Writable, E> conf) {
140 List<EdgeInputFormatDescription<I, E>> descriptions =
141 getEdgeInputFormatDescriptions(conf);
142 List<EdgeInputFormat<I, E>> edgeInputFormats =
143 Lists.newArrayListWithCapacity(descriptions.size());
144for (EdgeInputFormatDescription<I, E> description : descriptions) {
145 ImmutableClassesGiraphConfiguration<I, Writable, E> confCopy =
146 description.createConfigurationCopy(conf);
147 edgeInputFormats.add(confCopy.createWrappedEdgeInputFormat());
148 }
149return edgeInputFormats;
150 }
151 }