001/*
002 * Copyright (c) 2007-2016 Concurrent, Inc. All Rights Reserved.
003 *
004 * Project and contact information: http://www.cascading.org/
005 *
006 * This file is part of the Cascading project.
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package cascading.tap.hadoop;
022
023import java.io.IOException;
024import java.net.URI;
025import java.util.ArrayList;
026import java.util.Collection;
027
028import cascading.flow.FlowProcess;
029import cascading.tap.Tap;
030import cascading.tap.TapException;
031import org.apache.hadoop.conf.Configuration;
032import org.apache.hadoop.fs.Path;
033import org.apache.hadoop.mapred.OutputCollector;
034import org.apache.hadoop.mapred.RecordReader;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038/**
039 *
040 */
041public class DistCacheTap extends BaseDistCacheTap
042  {
043  private static final Logger LOG = LoggerFactory.getLogger( DistCacheTap.class );
044
045  public static final String CASCADING_LOCAL_RESOURCES = "cascading.resources.local.";
046  public static final String CASCADING_REMOTE_RESOURCES = "cascading.resources.remote.";
047
048  public DistCacheTap( Tap<Configuration, RecordReader, OutputCollector> original )
049    {
050    super( original );
051    }
052
053  @Override
054  protected Path[] getLocalCacheFiles( FlowProcess<? extends Configuration> flowProcess ) throws IOException
055    {
056    String key = CASCADING_REMOTE_RESOURCES + Tap.id( this );
057    String property = flowProcess.getStringProperty( key );
058
059    if( property == null )
060      throw new TapException( "unable to find local resources property for: " + key );
061
062    String[] split = property.split( "," );
063    Path[] paths = new Path[ split.length ];
064
065    for( int i = 0; i < split.length; i++ )
066      paths[ i ] = new Path( split[ i ] );
067
068    return paths;
069    }
070
071  @Override
072  protected void addLocalCacheFiles( Configuration conf, URI uri )
073    {
074    String key = CASCADING_LOCAL_RESOURCES + Tap.id( this );
075    Collection<String> resources = conf.getStringCollection( key );
076
077    if( resources == null )
078      resources = new ArrayList<>();
079
080    resources.add( uri.toString() );
081
082    conf.setStrings( key, resources.toArray( new String[ resources.size() ] ) );
083    }
084  }