Example usage for org.apache.hadoop.fs FileAlreadyExistsException FileAlreadyExistsException

List of usage examples for org.apache.hadoop.fs FileAlreadyExistsException FileAlreadyExistsException

Introduction

In this page you can find the example usage for org.apache.hadoop.fs FileAlreadyExistsException FileAlreadyExistsException.

Prototype

public FileAlreadyExistsException(String msg) 

Source Link

Usage

From source file:com.aliyun.fs.oss.nat.NativeOssFileSystem.java

License:Apache License

@Override
public FSDataOutputStream create(Path f, FsPermission permission, boolean overwrite, int bufferSize,
        short replication, long blockSize, Progressable progress) throws IOException {
    FileStatus status = null;/*  w w  w . j  a v  a  2s.c  o m*/
    try {
        // get the status or throw an FNFE
        status = getFileStatus(f);

        // if the thread reaches here, there is something at the path
        if (status.isDirectory()) {
            // path references a directory: automatic error
            throw new FileAlreadyExistsException(f + " is a directory");
        }
        if (!overwrite) {
            // path references a file and overwrite is disabled
            throw new FileAlreadyExistsException(f + " already exists");
        }
        LOG.debug("Overwriting file " + f);
    } catch (FileNotFoundException e) {
        // this means the file is not found

    }

    Path absolutePath = makeAbsolute(f);
    String key = pathToKey(absolutePath);
    return new FSDataOutputStream(
            new NativeOssFsOutputStream(getConf(), store, key, false, progress, bufferSize), statistics);
}

From source file:com.aliyun.fs.oss.nat.NativeOssFileSystem.java

License:Apache License

@Override
public FSDataOutputStream createNonRecursive(Path path, FsPermission permission, EnumSet<CreateFlag> flags,
        int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
    Path parent = path.getParent();
    if (parent != null) {
        // expect this to raise an exception if there is no parent
        if (!getFileStatus(parent).isDirectory()) {
            throw new FileAlreadyExistsException("Not a directory: " + parent);
        }/*from w  w w  .j  a v a 2  s.co m*/
    }
    return create(path, permission, flags.contains(CreateFlag.OVERWRITE), bufferSize, replication, blockSize,
            progress);
}

From source file:com.aliyun.fs.oss.nat.NativeOssFileSystem.java

License:Apache License

public boolean mkdir(Path f) throws IOException {
    try {/*  w  w w .j  a  v a 2s .  c o  m*/
        FileStatus fileStatus = getFileStatus(f);
        if (!fileStatus.isDir()) {
            throw new FileAlreadyExistsException(
                    String.format("Can't make directory for path '%s' since it is a file.", f));
        }
    } catch (FileNotFoundException e) {
        LOG.debug("Making dir '" + f + "' in OSS");
        String key = pathToKey(f);
        store.storeEmptyFile(key + PATH_DELIMITER);
    }
    return true;
}

From source file:com.aliyun.fs.oss.nat.NativeOssFileSystem.java

License:Apache License

@Override
public boolean rename(Path src, Path dst) throws IOException {

    String srcKey = pathToKey(makeAbsolute(src));

    if (srcKey.length() == 0) {
        // Cannot rename root of file system
        return false;
    }/*from  ww w .j  a  v a  2s  .  co m*/

    final String debugPreamble = "Renaming '" + src + "' to '" + dst + "' - ";

    // Figure out the final destination
    String dstKey;
    try {
        boolean dstIsFile = !getFileStatus(dst).isDir();
        if (dstIsFile) {
            LOG.debug(debugPreamble + "returning false as dst is an already " + "existing file");
            // If dst is not a directory
            throw new FileAlreadyExistsException(
                    String.format("Failed to rename %s to %s, file already exists!", src, dst));
        } else {
            LOG.debug(debugPreamble + "using dst as output directory");
            dstKey = pathToKey(makeAbsolute(new Path(dst, src.getName())));
        }
    } catch (FileNotFoundException e) {
        LOG.debug(debugPreamble + "using dst as output destination");
        dstKey = pathToKey(makeAbsolute(dst));
        try {
            if (!getFileStatus(dst.getParent()).isDir()) {
                LOG.debug(debugPreamble + "returning false as dst parent exists and " + "is a file");
                return false;
            }
        } catch (FileNotFoundException ex) {
            LOG.debug(debugPreamble + "returning false as dst parent does not exist");
            throw ex;
        }
    }

    boolean srcIsFile;
    try {
        srcIsFile = !getFileStatus(src).isDir();
    } catch (FileNotFoundException e) {
        LOG.debug(debugPreamble + "returning false as src does not exist");
        throw e;
    }
    if (srcIsFile) {
        LOG.debug(debugPreamble + "src is file, so doing copy then delete in Oss");
        store.copy(srcKey, dstKey);
        store.delete(srcKey);
    } else {
        LOG.debug(debugPreamble + "src is directory, so copying contents");
        store.storeEmptyFile(dstKey + PATH_DELIMITER);

        List<String> keysToDelete = new ArrayList<String>();
        String priorLastKey = null;
        do {
            PartialListing listing = store.list(srcKey, OSS_MAX_LISTING_LENGTH, priorLastKey, true);
            for (FileMetadata file : listing.getFiles()) {
                keysToDelete.add(file.getKey());
                store.copy(file.getKey(), dstKey + file.getKey().substring(srcKey.length()));
            }
            priorLastKey = listing.getPriorLastKey();
        } while (priorLastKey != null);

        LOG.debug(debugPreamble + "all files in src copied, now removing " + "src files");
        for (String key : keysToDelete) {
            store.delete(key);
        }

        try {
            store.delete(srcKey + FOLDER_SUFFIX);
        } catch (FileNotFoundException e) {
            //this is fine, we don't require a marker
        }
        LOG.debug(debugPreamble + "done");
    }

    return true;
}

From source file:com.aliyun.odps.fs.VolumeFileSystem.java

License:Apache License

@Override
public boolean rename(Path src, Path dst) throws IOException {
    statistics.incrementWriteOps(1);/*from   ww  w  . ja v a  2s  .com*/
    Path absSrc = fixRelativePart(src);
    Path absDst = fixRelativePart(dst);
    if (!exists(absSrc)) {
        throw new FileNotFoundException("Source path " + src + " does not exist");
    }
    if (isDirectory(absDst)) {
        // destination is a directory: rename goes underneath it with the
        // source name
        absDst = new Path(absDst, absSrc.getName());
    }
    if (exists(absDst)) {
        throw new FileAlreadyExistsException("Destination path " + dst + " already exists");
    }
    if (absDst.getParent() != null && !exists(absDst.getParent())) {
        throw new FileNotFoundException(
                VolumeFSErrorMessageGenerator.noSuchFileOrDirectory(absDst.getParent().toString()));
    }

    if (VolumeFSUtil.isParentOf(absSrc, absDst)) {
        throw new IOException("Cannot rename " + absSrc + " under itself" + " : " + absDst);
    }
    String srcPath = getPathName(absSrc);
    String dstPath = getPathName(absDst);
    try {
        return volumeClient.rename(srcPath, dstPath);
    } catch (VolumeException e) {
        logException(e);
        throw wrapExceptions(srcPath, e);
    }
}

From source file:com.aliyun.odps.fs.VolumeFileSystem.java

License:Apache License

private IOException wrapExceptions(String path, VolumeException e) {
    if (VolumeFSErrorCode.NoSuchPath.equalsIgnoreCase(e.getErrCode())) {
        return new FileNotFoundException(VolumeFSErrorMessageGenerator.noSuchFileOrDirectory(path));
    }// ww w  . ja  v  a  2s.  c  o  m
    if (VolumeFSErrorCode.InvalidPath.equalsIgnoreCase(e.getErrCode())) {
        throw new IllegalArgumentException(VolumeFSErrorMessageGenerator.isNotAValidODPSVolumeFSFilename(path));
    }
    if (VolumeFSErrorCode.NotAcceptableOperation.equalsIgnoreCase(e.getErrCode())) {
        throw new UnsupportedOperationException(e.getMessage());
    }
    if (VolumeFSErrorCode.PathAlreadyExists.equalsIgnoreCase(e.getErrCode())) {
        return new FileAlreadyExistsException(e.getMessage());
    }
    if (VolumeFSErrorCode.ParentNotDirectory.equalsIgnoreCase(e.getErrCode())) {
        return new ParentNotDirectoryException(e.getMessage());
    } else {
        return new IOException(e.getMessage(), e);
    }
}

From source file:com.ceph.rados.fs.hdfs.RadosFileSystem.java

License:Apache License

/**
 * @param permission Currently ignored.// w w w .j a  va2s.  c  o m
 */
@Override
public FSDataOutputStream create(Path file, FsPermission permission, boolean overwrite, int bufferSize,
        short replication, long blockSize, Progressable progress) throws IOException {

    INode inode = store.retrieveINode(makeAbsolute(file));
    if (inode != null) {
        if (overwrite) {
            delete(file, true);
        } else {
            throw new FileAlreadyExistsException("File already exists: " + file);
        }
    } else {
        Path parent = file.getParent();
        if (parent != null) {
            if (!mkdirs(parent)) {
                throw new IOException("Mkdirs failed to create " + parent.toString());
            }
        }
    }
    return new FSDataOutputStream(new RadosHDFSOutputStream(store, makeAbsolute(file).toString()));
}

From source file:com.flipkart.fdp.migration.distcp.core.MirrorFileRecordReader.java

License:Apache License

private void initializeStreams() throws IOException {

    String destPath = srcPath;//from  w w w .ja v a 2  s. c  om

    if (status.isInputTransformed()) {
        if (status.isInputCompressed()) {
            destPath = MirrorUtils.stripExtension(destPath);
        }

        if (status.isOutputCompressed()) {
            destPath = destPath + "." + dcmConfig.getSinkConfig().getCompressionCodec();
        }
    } else {

    }

    String basePath = fSplit.getDestHostConfig().getPath();
    if (basePath != null && basePath.trim().length() > 1) {
        destPath = basePath + "/" + destPath;
    }
    basePath = dcmConfig.getSinkConfig().getPath();

    if (basePath != null && basePath.trim().length() > 1) {
        destPath = basePath + "/" + destPath;
    }

    status.setInputPath(srcPath);
    status.setOutputPath(destPath);

    if (!dcmConfig.getSinkConfig().isOverwriteFiles()) {
        if (outCodec.isExistsPath(destPath)) {
            throw new FileAlreadyExistsException(destPath);
        }
    }

    in = inCodec.createInputStream(srcPath, status.isInputCompressed());
    out = outCodec.createOutputStream(destPath + DCMConstants.DCM_TEMP_EXTENSION, status.isOutputCompressed(),
            dcmConfig.getSinkConfig().getCompressionCodec(), dcmConfig.getSinkConfig().isAppend());

    String statusMesg = "Processing: " + srcPath + " -> " + destPath;
    context.setStatus(statusMesg);
    System.out.println("Status: " + statusMesg);
}

From source file:com.github.sadikovi.riff.FileWriter.java

License:Open Source License

/**
 * Create file writer for path./*from   www . j  a v a2 s  .co m*/
 * Configuration is passed separately and not reused from `fs.getConf`. This is to be explicit
 * about separate configuration from most of the hadoop settings. Actual user-facing API will
 * allow providing configuration for both file system and internal options.
 * @param fs file system to use
 * @param conf configuration
 * @param path path to the header file, also used to create data path
 * @param td type description for rows
 * @param codec compression codec
 * @throws IOException
 * @throws FileAlreadyExistsException
 */
FileWriter(FileSystem fs, Configuration conf, Path path, TypeDescription td, CompressionCodec codec)
        throws IOException {
    this.fs = fs;
    this.filePath = fs.makeQualified(path);
    this.writePrepared = false;
    this.writeFinished = false;
    if (this.fs.exists(filePath)) {
        throw new FileAlreadyExistsException("Already exists: " + filePath);
    }
    // this assumes that subsequent rows are provided for this schema
    this.td = td;
    this.numRowsInStripe = Riff.Options.numRowsInStripe(conf);
    this.bufferSize = Riff.Options.power2BufferSize(conf);
    this.hdfsBufferSize = Riff.Options.hdfsBufferSize(conf);
    this.columnFilterEnabled = Riff.Options.columnFilterEnabled(conf);
    this.codec = codec;
    // current stripe stats and filters
    this.stripeStats = null;
    this.stripeFilters = null;
    // file properties, by default not initialized
    this.fileProperties = null;
}

From source file:com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystemBase.java

License:Open Source License

/**
 * Makes the given path and all non-existent parents directories.
 * Has the semantics of Unix 'mkdir -p'.
 *
 * @param hadoopPath Given path./*  ww  w. ja  va2  s .co  m*/
 * @param permission Permissions to set on the given directory.
 * @return true on success, false otherwise.
 * @throws IOException if an error occurs.
 */
@Override
public boolean mkdirs(Path hadoopPath, FsPermission permission) throws IOException {

    long startTime = System.nanoTime();
    Preconditions.checkArgument(hadoopPath != null, "hadoopPath must not be null");

    checkOpen();

    LOG.debug("GHFS.mkdirs: {}, perm: {}", hadoopPath, permission);
    URI gcsPath = getGcsPath(hadoopPath);
    try {
        gcsfs.mkdirs(gcsPath);
    } catch (java.nio.file.FileAlreadyExistsException faee) {
        // Need to convert to the Hadoop flavor of FileAlreadyExistsException.
        throw (FileAlreadyExistsException) (new FileAlreadyExistsException(faee.getMessage()).initCause(faee));
    }

    long duration = System.nanoTime() - startTime;
    increment(Counter.MKDIRS);
    increment(Counter.MKDIRS_TIME, duration);

    return true;
}