This project has retired. For details please refer to its Attic page.
MultiEdgeInputFormat 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.conf.ImmutableClassesGiraphConfiguration;
22  import org.apache.giraph.io.EdgeInputFormat;
23  import org.apache.giraph.io.EdgeReader;
24  import org.apache.giraph.io.internal.WrappedEdgeReader;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.io.Writable;
27  import org.apache.hadoop.io.WritableComparable;
28  import org.apache.hadoop.mapreduce.InputSplit;
29  import org.apache.hadoop.mapreduce.JobContext;
30  import org.apache.hadoop.mapreduce.TaskAttemptContext;
31  
32  import java.io.DataInput;
33  import java.io.DataOutput;
34  import java.io.IOException;
35  import java.util.List;
36  
37  /**
38   * Edge input format which wraps several edge input formats.
39   * Provides the way to read data from multiple sources,
40   * using several different input formats.
41   *
42   * @param <I> Vertex id
43   * @param <E> Edge data
44   */
45  public class MultiEdgeInputFormat<I extends WritableComparable,
46      E extends Writable> extends EdgeInputFormat<I, E> {
47    /** Edge input formats */
48    private List<EdgeInputFormat<I, E>> edgeInputFormats;
49  
50    @Override public void checkInputSpecs(Configuration conf) {
51      for (EdgeInputFormat edgeInputFormat : edgeInputFormats) {
52        edgeInputFormat.checkInputSpecs(conf);
53      }
54    }
55  
56    @Override
57    public void setConf(
58        ImmutableClassesGiraphConfiguration<I, Writable, E> conf) {
59      super.setConf(conf);
60      edgeInputFormats =
61          EdgeInputFormatDescription.createEdgeInputFormats(getConf());
62      if (edgeInputFormats.isEmpty()) {
63        throw new IllegalStateException("setConf: Using MultiEdgeInputFormat " +
64            "without specifying edge inputs");
65      }
66    }
67  
68    @Override
69    public EdgeReader<I, E> createEdgeReader(InputSplit inputSplit,
70        TaskAttemptContext context) throws IOException {
71      if (inputSplit instanceof InputSplitWithInputFormatIndex) {
72        // When multithreaded input is used we need to make sure other threads
73        // don't change context's configuration while we use it
74        synchronized (context) {
75          InputSplitWithInputFormatIndex split =
76              (InputSplitWithInputFormatIndex) inputSplit;
77          EdgeInputFormat<I, E> edgeInputFormat =
78              edgeInputFormats.get(split.getInputFormatIndex());
79          EdgeReader<I, E> edgeReader =
80              edgeInputFormat.createEdgeReader(split.getSplit(), context);
81          return new WrappedEdgeReader<I, E>(
82              edgeReader, edgeInputFormat.getConf()) {
83            @Override
84            public void initialize(InputSplit inputSplit,
85                TaskAttemptContext context) throws IOException,
86                InterruptedException {
87              // When multithreaded input is used we need to make sure other
88              // threads don't change context's configuration while we use it
89              synchronized (context) {
90                super.initialize(inputSplit, context);
91              }
92            }
93          };
94        }
95      } else {
96        throw new IllegalStateException("createEdgeReader: Got InputSplit which" +
97            " was not created by this class: " + inputSplit.getClass().getName());
98      }
99    }
100 
101   @Override
102   public List<InputSplit> getSplits(JobContext context,
103       int minSplitCountHint) throws IOException, InterruptedException {
104     // When multithreaded input is used we need to make sure other threads don't
105     // change context's configuration while we use it
106     synchronized (context) {
107       return MultiInputUtils.getSplits(
108           context, minSplitCountHint, edgeInputFormats);
109     }
110   }
111 
112   @Override
113   public void writeInputSplit(InputSplit inputSplit,
114       DataOutput dataOutput) throws IOException {
115     MultiInputUtils.writeInputSplit(inputSplit, dataOutput, edgeInputFormats);
116   }
117 
118   @Override
119   public InputSplit readInputSplit(
120       DataInput dataInput) throws IOException, ClassNotFoundException {
121     return MultiInputUtils.readInputSplit(dataInput, edgeInputFormats);
122   }
123 }