001/*
002 * Copyright (c) 2007-2017 Xplenty, 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;
035
036/**
037 *
038 */
039public class DistCacheTap extends BaseDistCacheTap
040  {
041  public static final String CASCADING_LOCAL_RESOURCES = "cascading.resources.local.";
042  public static final String CASCADING_REMOTE_RESOURCES = "cascading.resources.remote.";
043
044  public DistCacheTap( Tap<Configuration, RecordReader, OutputCollector> original )
045    {
046    super( original );
047    }
048
049  @Override
050  protected Path[] getLocalCacheFiles( FlowProcess<? extends Configuration> flowProcess ) throws IOException
051    {
052    String key = CASCADING_REMOTE_RESOURCES + Tap.id( this );
053    String property = flowProcess.getStringProperty( key );
054
055    if( property == null )
056      throw new TapException( "unable to find local resources property for: " + key );
057
058    String[] split = property.split( "," );
059    Path[] paths = new Path[ split.length ];
060
061    for( int i = 0; i < split.length; i++ )
062      paths[ i ] = new Path( split[ i ] );
063
064    return paths;
065    }
066
067  @Override
068  protected void addLocalCacheFiles( Configuration conf, URI uri )
069    {
070    String key = CASCADING_LOCAL_RESOURCES + Tap.id( this );
071    Collection<String> resources = conf.getStringCollection( key );
072
073    if( resources == null )
074      resources = new ArrayList<>();
075
076    resources.add( uri.toString() );
077
078    conf.setStrings( key, resources.toArray( new String[ resources.size() ] ) );
079    }
080  }