All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hotels.plunger.TapDataReader Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2015 Expedia Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hotels.plunger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.apache.hadoop.mapred.JobConf;

import cascading.flow.FlowProcess;
import cascading.flow.hadoop.HadoopFlowProcess;
import cascading.flow.local.LocalFlowProcess;
import cascading.tap.Tap;
import cascading.tuple.Fields;
import cascading.tuple.Tuple;
import cascading.tuple.TupleEntryIterator;

/**
 * Reads data from {@link Tap Taps} and returns them as a {@link Data} instance. Currently works only with hadoop and
 * local taps.
 */
class TapDataReader {

  private final Tap source;

  TapDataReader(Tap source) {
    this.source = source;
  }

  /**
   * Reads the {@link Tuple Tuples} from the {@link Tap} and returns them wrapped in a {@link Data} instance whose
   * {@link Fields} confirm to those supplied by {@link Tap#getSourceFields()}.
   */
  Data read() throws IOException {
    TupleEntryIterator tuples;
    Class tapConfigClass = TapTypeUtil.getTapConfigClass(source);

    if (JobConf.class.equals(tapConfigClass)) {
      tuples = getHadoopTupleEntryIterator();
    } else if (Properties.class.equals(tapConfigClass)) {
      tuples = getLocalTupleEntryIterator();
    } else {
      throw new IllegalArgumentException("Unsupported tap type: " + source.getClass());
    }
    List resultTuples = new ArrayList();
    while (tuples.hasNext()) {
      resultTuples.add(new Tuple(tuples.next().getTuple()));
    }
    return new Data(source.getSourceFields(), Collections.unmodifiableList(resultTuples));
  }

  private TupleEntryIterator getHadoopTupleEntryIterator() throws IOException {
    @SuppressWarnings("unchecked")
    Tap hadoopTap = (Tap) source;
    JobConf conf = new JobConf();
    FlowProcess flowProcess = new HadoopFlowProcess(conf);
    hadoopTap.sourceConfInit(flowProcess, conf);
    return hadoopTap.openForRead(flowProcess);
  }

  private TupleEntryIterator getLocalTupleEntryIterator() throws IOException {
    @SuppressWarnings("unchecked")
    Tap localTap = (Tap) source;
    Properties properties = new Properties();
    FlowProcess flowProcess = new LocalFlowProcess(properties);
    localTap.sourceConfInit(flowProcess, properties);
    return localTap.openForRead(flowProcess);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy