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.google.cloud.hadoop.fs.gcs.GoogleHadoopOutputStream.java

License:Open Source License

/**
 * Constructs an instance of GoogleHadoopOutputStream object.
 *
 * @param ghfs Instance of GoogleHadoopFileSystemBase.
 * @param gcsPath Path of the file to write to.
 * @param bufferSize Size of the buffer to use.
 * @param statistics File system statistics object.
 * @throws IOException if an IO error occurs.
 *//*from ww w.j a v  a  2 s .c  o  m*/
GoogleHadoopOutputStream(GoogleHadoopFileSystemBase ghfs, URI gcsPath, int bufferSize,
        FileSystem.Statistics statistics, CreateFileOptions createFileOptions) throws IOException {
    LOG.debug("GoogleHadoopOutputStream({}, {})", gcsPath, bufferSize);
    this.ghfs = ghfs;
    this.gcsPath = gcsPath;
    this.statistics = statistics;
    initTime = System.nanoTime();
    try {
        channel = ghfs.getGcsFs().create(gcsPath, createFileOptions);
    } catch (java.nio.file.FileAlreadyExistsException faee) {
        // Need to convert to the Hadoop flavor of FileAlreadyExistsException.
        throw (FileAlreadyExistsException) (new FileAlreadyExistsException(faee.getMessage()).initCause(faee));
    }
    OutputStream rawStream = Channels.newOutputStream(channel);
    out = new BufferedOutputStream(rawStream, bufferSize);
}

From source file:com.quixey.hadoop.fs.oss.OSSFileSystem.java

License:Apache License

@Override
@Nonnull//w ww  .  j  av a2s  .  c  o m
public FSDataOutputStream create(Path path, FsPermission permission, boolean overwrite, int bufferSize,
        short replication, long blockSize, @Nullable Progressable progress) throws IOException {
    path = checkNotNull(path);
    if (exists(path) && !overwrite) {
        throw new FileAlreadyExistsException("File already exists: " + path);
    }

    LOG.debug("Creating new file '{}' in OSS", path);
    Path absolutePath = makeAbsolute(path);
    String key = pathToKey(absolutePath);
    return new FSDataOutputStream(new OSSFileOutputStream(getConf(), key, fromNullable(progress)), statistics);
}

From source file:com.quixey.hadoop.fs.oss.OSSFileSystem.java

License:Apache License

/**
 * Creates a single directory./*  w  w  w .  j  a  va 2s . c om*/
 *
 * @param f path
 * @return true iff the directory exists, or was created
 */
private boolean mkdir(Path f) throws IOException {
    try {
        FileStatus fileStatus = getFileStatus(f);
        if (fileStatus.isFile()) {
            throw new FileAlreadyExistsException(
                    String.format("Can't make directory for path '%s' since it is a file.", f));
        }
    } catch (FileNotFoundException e) {
        if (LOG.isDebugEnabled())
            LOG.debug("Making dir '" + f + "' in OSS");
        String key = pathToKey(f) + FOLDER_SUFFIX;
        store.storeEmptyFile(key);
    }
    return true;
}

From source file:gobblin.util.HadoopUtils.java

License:Apache License

/**
 * A wrapper around {@link FileSystem#rename(Path, Path)} which throws {@link IOException} if
 * {@link FileSystem#rename(Path, Path)} returns False.
 *///from  w w w .j  av a  2 s.c  o  m
public static void renamePath(FileSystem fs, Path oldName, Path newName, boolean overwrite) throws IOException {
    if (!fs.exists(oldName)) {
        throw new FileNotFoundException(
                String.format("Failed to rename %s to %s: src not found", oldName, newName));
    }
    if (fs.exists(newName)) {
        if (overwrite) {
            if (!fs.delete(newName, true)) {
                throw new IOException(String.format("Failed to delete %s while renaming %s to %s", newName,
                        oldName, newName));
            }
        } else {
            throw new FileAlreadyExistsException(
                    String.format("Failed to rename %s to %s: dst already exists", oldName, newName));
        }
    }
    if (!fs.rename(oldName, newName)) {
        throw new IOException(String.format("Failed to rename %s to %s", oldName, newName));
    }
}

From source file:io.pravega.segmentstore.storage.impl.hdfs.HDFSExceptionHelpers.java

License:Open Source License

/**
 * Returns a new instance of the HDFS equivalent of StreamSegmentExistsException.
 *
 * @param segmentName The name of the segment to construct the Exception for.
 * @return The new exception.//from ww  w  .  j  av a  2  s  .c  o m
 */
static FileAlreadyExistsException segmentExistsException(String segmentName) {
    return new FileAlreadyExistsException(segmentName);
}

From source file:io.pravega.segmentstore.storage.impl.hdfs.MockFileSystem.java

License:Open Source License

private FileData createInternal(Path f) throws IOException {
    synchronized (this.files) {
        FileData data = this.files.getOrDefault(f, null);
        if (data != null) {
            throw new FileAlreadyExistsException(f.getName());
        }// w  w  w  .j  a  va2 s  .  co m

        data = new FileData(f);
        this.files.put(f, data);
        return data;
    }
}

From source file:org.apache.gobblin.util.HadoopUtils.java

License:Apache License

/**
 * A wrapper around {@link FileSystem#rename(Path, Path)} which throws {@link IOException} if
 * {@link FileSystem#rename(Path, Path)} returns False.
 *///  ww w .  j a  v  a  2 s. c o  m
public static void renamePath(FileSystem fs, Path oldName, Path newName, boolean overwrite) throws IOException {
    if (!fs.exists(oldName)) {
        throw new FileNotFoundException(
                String.format("Failed to rename %s to %s: src not found", oldName, newName));
    }
    if (fs.exists(newName)) {
        if (overwrite) {
            HadoopUtils.moveToTrash(fs, newName);
        } else {
            throw new FileAlreadyExistsException(
                    String.format("Failed to rename %s to %s: dst already exists", oldName, newName));
        }
    }
    if (!fs.rename(oldName, newName)) {
        throw new IOException(String.format("Failed to rename %s to %s", oldName, newName));
    }
}

From source file:org.apache.ignite.internal.processors.hadoop.fs.GridHadoopRawLocalFileSystem.java

License:Apache License

/** {@inheritDoc} */
@Override//from   ww  w. j  a v  a 2  s.co m
public boolean mkdirs(Path f, FsPermission permission) throws IOException {
    if (f == null)
        throw new IllegalArgumentException("mkdirs path arg is null");

    Path parent = f.getParent();

    File p2f = convert(f);

    if (parent != null) {
        File parent2f = convert(parent);

        if (parent2f != null && parent2f.exists() && !parent2f.isDirectory())
            throw new FileAlreadyExistsException("Parent path is not a directory: " + parent);

    }

    return (parent == null || mkdirs(parent)) && (p2f.mkdir() || p2f.isDirectory());
}

From source file:org.apache.tez.mapreduce.examples.GroupByOrderByMRRTest.java

License:Apache License

@Override
public int run(String[] args) throws Exception {
    Configuration conf = getConf();

    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length != 2) {
        System.err.println("Usage: groupbyorderbymrrtest <in> <out>");
        ToolRunner.printGenericCommandUsage(System.err);
        return 2;
    }//ww  w.  j a v  a 2s.  c o  m

    String inputPath = otherArgs[0];
    String outputPath = otherArgs[1];

    UserGroupInformation.setConfiguration(conf);

    TezConfiguration tezConf = new TezConfiguration(conf);
    FileSystem fs = FileSystem.get(conf);

    if (fs.exists(new Path(outputPath))) {
        throw new FileAlreadyExistsException("Output directory " + outputPath + " already exists");
    }

    Map<String, LocalResource> localResources = new TreeMap<String, LocalResource>();

    String stagingDirStr = conf.get(TezConfiguration.TEZ_AM_STAGING_DIR,
            TezConfiguration.TEZ_AM_STAGING_DIR_DEFAULT) + Path.SEPARATOR
            + Long.toString(System.currentTimeMillis());
    Path stagingDir = new Path(stagingDirStr);
    FileSystem pathFs = stagingDir.getFileSystem(tezConf);
    pathFs.mkdirs(new Path(stagingDirStr));

    tezConf.set(TezConfiguration.TEZ_AM_STAGING_DIR, stagingDirStr);
    stagingDir = pathFs.makeQualified(new Path(stagingDirStr));

    TezClient tezClient = TezClient.create("groupbyorderbymrrtest", tezConf);
    tezClient.start();

    LOG.info("Submitting groupbyorderbymrrtest DAG as a new Tez Application");

    try {
        DAG dag = createDAG(conf, localResources, stagingDir, inputPath, outputPath, true);

        tezClient.waitTillReady();

        DAGClient dagClient = tezClient.submitDAG(dag);

        DAGStatus dagStatus = dagClient.waitForCompletionWithStatusUpdates(null);
        if (dagStatus.getState() != DAGStatus.State.SUCCEEDED) {
            LOG.error("groupbyorderbymrrtest failed, state=" + dagStatus.getState() + ", diagnostics="
                    + dagStatus.getDiagnostics());
            return -1;
        }
        LOG.info("Application completed. " + "FinalState=" + dagStatus.getState());
        return 0;
    } finally {
        tezClient.stop();
    }
}