hadoop FileBasedCopyListing 源码

  • 2022-10-20
  • 浏览 (15)

haddop FileBasedCopyListing 代码

文件路径:/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/FileBasedCopyListing.java

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.hadoop.tools;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.security.Credentials;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * FileBasedCopyListing implements the CopyListing interface,
 * to create the copy-listing for DistCp,
 * by iterating over all source paths mentioned in a specified input-file.
 */
public class FileBasedCopyListing extends CopyListing {

  private final CopyListing globbedListing;
  /**
   * Constructor, to initialize base-class.
   * @param configuration The input Configuration object.
   * @param credentials - Credentials object on which the FS delegation tokens are cached. If null
   * delegation token caching is skipped
   */
  public FileBasedCopyListing(Configuration configuration, Credentials credentials) {
    super(configuration, credentials);
    globbedListing = new GlobbedCopyListing(getConf(), credentials);
  }

  /** {@inheritDoc} */
  @Override
  protected void validatePaths(DistCpContext context)
      throws IOException, InvalidInputException {
  }

  /**
   * Implementation of CopyListing::buildListing().
   *   Iterates over all source paths mentioned in the input-file.
   * @param pathToListFile Path on HDFS where the listing file is written.
   * @param context Distcp context with associated input options.
   * @throws IOException
   */
  @Override
  public void doBuildListing(Path pathToListFile, DistCpContext context)
      throws IOException {
    context.setSourcePaths(fetchFileList(context.getSourceFileListing()));
    globbedListing.buildListing(pathToListFile, context);
  }

  private List<Path> fetchFileList(Path sourceListing) throws IOException {
    List<Path> result = new ArrayList<Path>();
    FileSystem fs = sourceListing.getFileSystem(getConf());
    BufferedReader input = null;
    try {
      input = new BufferedReader(new InputStreamReader(fs.open(sourceListing),
          Charset.forName("UTF-8")));
      String line = input.readLine();
      while (line != null) {
        result.add(new Path(line));
        line = input.readLine();
      }
    } finally {
      IOUtils.closeStream(input);
    }
    return result;
  }

  /** {@inheritDoc} */
  @Override
  protected long getBytesToCopy() {
    return globbedListing.getBytesToCopy();
  }

  /** {@inheritDoc} */
  @Override
  protected long getNumberOfPaths() {
    return globbedListing.getNumberOfPaths();
  }
}

相关信息

hadoop 源码目录

相关文章

hadoop CopyFilter 源码

hadoop CopyListing 源码

hadoop CopyListingFileStatus 源码

hadoop DiffInfo 源码

hadoop DistCp 源码

hadoop DistCpConstants 源码

hadoop DistCpContext 源码

hadoop DistCpOptionSwitch 源码

hadoop DistCpOptions 源码

hadoop DistCpSync 源码

0  赞