Java Code Examples for org.apache.hadoop.fs.FileAlreadyExistsException

The following examples show how to use org.apache.hadoop.fs.FileAlreadyExistsException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: hadoop   Source File: TestRegistryOperations.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOverwrite() throws Throwable {
  ServiceRecord written = putExampleServiceEntry(ENTRY_PATH, 0);
  ServiceRecord resolved1 = operations.resolve(ENTRY_PATH);
  resolved1.description = "resolved1";
  try {
    operations.bind(ENTRY_PATH, resolved1, 0);
    fail("overwrite succeeded when it should have failed");
  } catch (FileAlreadyExistsException expected) {
    // expected
  }

  // verify there's no changed
  ServiceRecord resolved2 = operations.resolve(ENTRY_PATH);
  assertMatches(written, resolved2);
  operations.bind(ENTRY_PATH, resolved1, BindFlags.OVERWRITE);
  ServiceRecord resolved3 = operations.resolve(ENTRY_PATH);
  assertMatches(resolved1, resolved3);
}
 
Example 2
Source Project: hadoop   Source File: JobHistoryEventHandler.java    License: Apache License 2.0 6 votes vote down vote up
private void mkdir(FileSystem fs, Path path, FsPermission fsp)
    throws IOException {
  if (!fs.exists(path)) {
    try {
      fs.mkdirs(path, fsp);
      FileStatus fsStatus = fs.getFileStatus(path);
      LOG.info("Perms after creating " + fsStatus.getPermission().toShort()
          + ", Expected: " + fsp.toShort());
      if (fsStatus.getPermission().toShort() != fsp.toShort()) {
        LOG.info("Explicitly setting permissions to : " + fsp.toShort()
            + ", " + fsp);
        fs.setPermission(path, fsp);
      }
    } catch (FileAlreadyExistsException e) {
      LOG.info("Directory: [" + path + "] already exists.");
    }
  }
}
 
Example 3
Source Project: big-c   Source File: NativeS3FileSystem.java    License: Apache License 2.0 6 votes vote down vote up
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 S3");
    }
    String key = pathToKey(f) + FOLDER_SUFFIX;
    store.storeEmptyFile(key);    
  }
  return true;
}
 
Example 4
Source Project: big-c   Source File: ViewFs.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public FSDataOutputStream createInternal(final Path f,
    final EnumSet<CreateFlag> flag, final FsPermission absolutePermission,
    final int bufferSize, final short replication, final long blockSize,
    final Progressable progress, final ChecksumOpt checksumOpt,
    final boolean createParent) throws AccessControlException,
    FileAlreadyExistsException, FileNotFoundException,
    ParentNotDirectoryException, UnsupportedFileSystemException,
    UnresolvedLinkException, IOException {
  InodeTree.ResolveResult<AbstractFileSystem> res;
  try {
    res = fsState.resolve(getUriPath(f), false);
  } catch (FileNotFoundException e) {
    if (createParent) {
      throw readOnlyMountTable("create", f);
    } else {
      throw e;
    }
  }
  assert(res.remainingPath != null);
  return res.targetFileSystem.createInternal(res.remainingPath, flag,
      absolutePermission, bufferSize, replication,
      blockSize, progress, checksumOpt,
      createParent);
}
 
Example 5
Source Project: big-c   Source File: AbstractContractCreateTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateFileOverExistingFileNoOverwrite() throws Throwable {
  describe("Verify overwriting an existing file fails");
  Path path = path("testCreateFileOverExistingFileNoOverwrite");
  byte[] data = dataset(256, 'a', 'z');
  writeDataset(getFileSystem(), path, data, data.length, 1024, false);
  byte[] data2 = dataset(10 * 1024, 'A', 'Z');
  try {
    writeDataset(getFileSystem(), path, data2, data2.length, 1024, false);
    fail("writing without overwrite unexpectedly succeeded");
  } catch (FileAlreadyExistsException expected) {
    //expected
    handleExpectedException(expected);
  } catch (IOException relaxed) {
    handleRelaxedException("Creating a file over a file with overwrite==false",
                           "FileAlreadyExistsException",
                           relaxed);
  }
}
 
Example 6
/**
* Opens an FSDataOutputStream at the indicated Path with write-progress
* reporting. Same as create(), except fails if parent directory doesn't
* already exist.
* @param path the file name to open
* @param permission
* @param overwrite if a file with this name already exists, then if true,
* the file will be overwritten, and if false an error will be thrown.
* @param bufferSize the size of the buffer to be used.
* @param replication required block replication for the file.
* @param blockSize
* @param progress
* @throws IOException
* @see #setPermission(Path, FsPermission)
* @deprecated API only for 0.20-append
*/
@Deprecated
public FSDataOutputStream createNonRecursive(Path path, FsPermission permission,
    boolean overwrite,
    int bufferSize, short replication, long blockSize,
    Progressable progress) throws IOException {

  path = makeAbsolute(path);

  Path parent = path.getParent();

  if (parent != null) {
    CephStat stat = new CephStat();
    ceph.lstat(parent, stat); // handles FileNotFoundException case
    if (stat.isFile())
      throw new FileAlreadyExistsException(parent.toString());
  }

  return this.create(path, permission, overwrite,
      bufferSize, replication, blockSize, progress);
}
 
Example 7
Source Project: hadoop   Source File: FSDirRenameOp.java    License: Apache License 2.0 6 votes vote down vote up
private static void validateDestination(
    String src, String dst, INode srcInode)
    throws IOException {
  String error;
  if (srcInode.isSymlink() &&
      dst.equals(srcInode.asSymlink().getSymlinkString())) {
    throw new FileAlreadyExistsException("Cannot rename symlink " + src
        + " to its target " + dst);
  }
  // dst cannot be a directory or a file under src
  if (dst.startsWith(src)
      && dst.charAt(src.length()) == Path.SEPARATOR_CHAR) {
    error = "Rename destination " + dst
        + " is a directory or file under source " + src;
    NameNode.stateChangeLog.warn("DIR* FSDirectory.unprotectedRenameTo: "
        + error);
    throw new IOException(error);
  }
}
 
Example 8
Source Project: incubator-gobblin   Source File: ParallelRunner.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Move a {@link Path}.
 *
 * <p>
 *   This method submits a task to move a {@link Path} and returns immediately
 *   after the task is submitted.
 * </p>
 *
 * @param src path to be moved
 * @param dstFs the destination {@link FileSystem}
 * @param dst the destination path
 * @param overwrite true to overwrite the destination
 * @param group an optional group name for the destination path
 */
public void movePath(final Path src, final FileSystem dstFs, final Path dst, final boolean overwrite,
    final Optional<String> group) {
  this.futures.add(new NamedFuture(this.executor.submit(new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      Lock lock = ParallelRunner.this.locks.get(src.toString());
      lock.lock();
      try {
        if (ParallelRunner.this.fs.exists(src)) {
          HadoopUtils.movePath(ParallelRunner.this.fs, src, dstFs, dst, overwrite, dstFs.getConf());
          if (group.isPresent()) {
            HadoopUtils.setGroup(dstFs, dst, group.get());
          }
        }
        return null;
      } catch (FileAlreadyExistsException e) {
        LOGGER.warn(String.format("Failed to move %s to %s: dst already exists", src, dst), e);
        return null;
      } finally {
        lock.unlock();
      }
    }
  }), "Move " + src + " to " + dst));
}
 
Example 9
Source Project: big-c   Source File: FSDirMkdirOp.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * create a directory at path specified by parent
 */
private static INodesInPath unprotectedMkdir(FSDirectory fsd, long inodeId,
    INodesInPath parent, byte[] name, PermissionStatus permission,
    List<AclEntry> aclEntries, long timestamp)
    throws QuotaExceededException, AclException, FileAlreadyExistsException {
  assert fsd.hasWriteLock();
  assert parent.getLastINode() != null;
  if (!parent.getLastINode().isDirectory()) {
    throw new FileAlreadyExistsException("Parent path is not a directory: " +
        parent.getPath() + " " + DFSUtil.bytes2String(name));
  }
  final INodeDirectory dir = new INodeDirectory(inodeId, name, permission,
      timestamp);

  INodesInPath iip = fsd.addLastINode(parent, dir, true);
  if (iip != null && aclEntries != null) {
    AclStorage.updateINodeAcl(dir, aclEntries, Snapshot.CURRENT_STATE_ID);
  }
  return iip;
}
 
Example 10
Source Project: hadoop   Source File: ClientNamenodeProtocolTranslatorPB.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean mkdirs(String src, FsPermission masked, boolean createParent)
    throws AccessControlException, FileAlreadyExistsException,
    FileNotFoundException, NSQuotaExceededException,
    ParentNotDirectoryException, SafeModeException, UnresolvedLinkException,
    IOException {
  MkdirsRequestProto req = MkdirsRequestProto.newBuilder()
      .setSrc(src)
      .setMasked(PBHelper.convert(masked))
      .setCreateParent(createParent).build();

  try {
    return rpcProxy.mkdirs(null, req).getResult();
  } catch (ServiceException e) {
    throw ProtobufHelper.getRemoteException(e);
  }
}
 
Example 11
Source Project: big-c   Source File: FSDirRenameOp.java    License: Apache License 2.0 6 votes vote down vote up
private static void validateDestination(
    String src, String dst, INode srcInode)
    throws IOException {
  String error;
  if (srcInode.isSymlink() &&
      dst.equals(srcInode.asSymlink().getSymlinkString())) {
    throw new FileAlreadyExistsException("Cannot rename symlink " + src
        + " to its target " + dst);
  }
  // dst cannot be a directory or a file under src
  if (dst.startsWith(src)
      && dst.charAt(src.length()) == Path.SEPARATOR_CHAR) {
    error = "Rename destination " + dst
        + " is a directory or file under source " + src;
    NameNode.stateChangeLog.warn("DIR* FSDirectory.unprotectedRenameTo: "
        + error);
    throw new IOException(error);
  }
}
 
Example 12
Source Project: big-c   Source File: DFSClient.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Rename file or directory.
 * @see ClientProtocol#rename2(String, String, Options.Rename...)
 */
public void rename(String src, String dst, Options.Rename... options)
    throws IOException {
  checkOpen();
  TraceScope scope = getSrcDstTraceScope("rename2", src, dst);
  try {
    namenode.rename2(src, dst, options);
  } catch(RemoteException re) {
    throw re.unwrapRemoteException(AccessControlException.class,
                                   DSQuotaExceededException.class,
                                   FileAlreadyExistsException.class,
                                   FileNotFoundException.class,
                                   ParentNotDirectoryException.class,
                                   SafeModeException.class,
                                   NSQuotaExceededException.class,
                                   UnresolvedPathException.class,
                                   SnapshotAccessControlException.class);
  } finally {
    scope.close();
  }
}
 
Example 13
Source Project: hadoop   Source File: NativeS3FileSystem.java    License: Apache License 2.0 6 votes vote down vote up
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 S3");
    }
    String key = pathToKey(f) + FOLDER_SUFFIX;
    store.storeEmptyFile(key);    
  }
  return true;
}
 
Example 14
Source Project: hadoop   Source File: S3AFileSystem.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create an FSDataOutputStream at the indicated Path with write-progress
 * reporting.
 * @param f the file name to open
 * @param permission
 * @param overwrite if a file with this name already exists, then if true,
 *   the file will be overwritten, and if false an error will be thrown.
 * @param bufferSize the size of the buffer to be used.
 * @param replication required block replication for the file.
 * @param blockSize
 * @param progress
 * @throws IOException
 * @see #setPermission(Path, FsPermission)
 */
public FSDataOutputStream create(Path f, FsPermission permission, boolean overwrite, 
  int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
  String key = pathToKey(f);

  if (!overwrite && exists(f)) {
    throw new FileAlreadyExistsException(f + " already exists");
  }
  if (getConf().getBoolean(FAST_UPLOAD, DEFAULT_FAST_UPLOAD)) {
    return new FSDataOutputStream(new S3AFastOutputStream(s3, this, bucket,
        key, progress, statistics, cannedACL,
        serverSideEncryptionAlgorithm, partSize, (long)multiPartThreshold,
        threadPoolExecutor), statistics);
  }
  // We pass null to FSDataOutputStream so it won't count writes that are being buffered to a file
  return new FSDataOutputStream(new S3AOutputStream(getConf(), transfers, this,
    bucket, key, progress, cannedACL, statistics, 
    serverSideEncryptionAlgorithm), null);
}
 
Example 15
Source Project: incubator-gobblin   Source File: ParallelRunner.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Rename a {@link Path}.
 *
 * <p>
 *   This method submits a task to rename a {@link Path} and returns immediately
 *   after the task is submitted.
 * </p>
 *
 * @param src path to be renamed
 * @param dst new path after rename
 * @param group an optional group name for the destination path
 */
public void renamePath(final Path src, final Path dst, final Optional<String> group) {
  this.futures.add(new NamedFuture(this.executor.submit(new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      Lock lock = ParallelRunner.this.locks.get(src.toString());
      lock.lock();
      try {
        if (ParallelRunner.this.fs.exists(src)) {
          HadoopUtils.renamePath(ParallelRunner.this.fs, src, dst);
          if (group.isPresent()) {
            HadoopUtils.setGroup(ParallelRunner.this.fs, dst, group.get());
          }
        }
        return null;
      } catch (FileAlreadyExistsException e) {
        LOGGER.warn(String.format("Failed to rename %s to %s: dst already exists", src, dst), e);
        return null;
      } finally {
        lock.unlock();
      }
    }
  }), "Rename " + src + " to " + dst));
}
 
Example 16
Source Project: hadoop   Source File: ViewFs.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public FSDataOutputStream createInternal(final Path f,
    final EnumSet<CreateFlag> flag, final FsPermission absolutePermission,
    final int bufferSize, final short replication, final long blockSize,
    final Progressable progress, final ChecksumOpt checksumOpt,
    final boolean createParent) throws AccessControlException,
    FileAlreadyExistsException, FileNotFoundException,
    ParentNotDirectoryException, UnsupportedFileSystemException,
    UnresolvedLinkException, IOException {
  InodeTree.ResolveResult<AbstractFileSystem> res;
  try {
    res = fsState.resolve(getUriPath(f), false);
  } catch (FileNotFoundException e) {
    if (createParent) {
      throw readOnlyMountTable("create", f);
    } else {
      throw e;
    }
  }
  assert(res.remainingPath != null);
  return res.targetFileSystem.createInternal(res.remainingPath, flag,
      absolutePermission, bufferSize, replication,
      blockSize, progress, checksumOpt,
      createParent);
}
 
Example 17
Source Project: pravega   Source File: HDFSExceptionHelpers.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Translates HDFS specific Exceptions to Pravega-equivalent Exceptions.
 *
 * @param segmentName Name of the stream segment on which the exception occurs.
 * @param e           The exception to be translated.
 * @return  The exception to be thrown.
 */
static <T> StreamSegmentException convertException(String segmentName, Throwable e) {
    if (e instanceof RemoteException) {
        e = ((RemoteException) e).unwrapRemoteException();
    }

    if (e instanceof PathNotFoundException || e instanceof FileNotFoundException) {
        return new StreamSegmentNotExistsException(segmentName, e);
    } else if (e instanceof FileAlreadyExistsException || e instanceof AlreadyBeingCreatedException) {
        return new StreamSegmentExistsException(segmentName, e);
    } else if (e instanceof AclException) {
        return new StreamSegmentSealedException(segmentName, e);
    } else {
        throw Exceptions.sneakyThrow(e);
    }
}
 
Example 18
Source Project: big-c   Source File: ClientNamenodeProtocolTranslatorPB.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean mkdirs(String src, FsPermission masked, boolean createParent)
    throws AccessControlException, FileAlreadyExistsException,
    FileNotFoundException, NSQuotaExceededException,
    ParentNotDirectoryException, SafeModeException, UnresolvedLinkException,
    IOException {
  MkdirsRequestProto req = MkdirsRequestProto.newBuilder()
      .setSrc(src)
      .setMasked(PBHelper.convert(masked))
      .setCreateParent(createParent).build();

  try {
    return rpcProxy.mkdirs(null, req).getResult();
  } catch (ServiceException e) {
    throw ProtobufHelper.getRemoteException(e);
  }
}
 
Example 19
Source Project: hadoop   Source File: AbstractContractCreateTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateFileOverExistingFileNoOverwrite() throws Throwable {
  describe("Verify overwriting an existing file fails");
  Path path = path("testCreateFileOverExistingFileNoOverwrite");
  byte[] data = dataset(256, 'a', 'z');
  writeDataset(getFileSystem(), path, data, data.length, 1024, false);
  byte[] data2 = dataset(10 * 1024, 'A', 'Z');
  try {
    writeDataset(getFileSystem(), path, data2, data2.length, 1024, false);
    fail("writing without overwrite unexpectedly succeeded");
  } catch (FileAlreadyExistsException expected) {
    //expected
    handleExpectedException(expected);
  } catch (IOException relaxed) {
    handleRelaxedException("Creating a file over a file with overwrite==false",
                           "FileAlreadyExistsException",
                           relaxed);
  }
}
 
Example 20
Source Project: big-c   Source File: ClientNamenodeProtocolTranslatorPB.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void createSymlink(String target, String link, FsPermission dirPerm,
    boolean createParent) throws AccessControlException,
    FileAlreadyExistsException, FileNotFoundException,
    ParentNotDirectoryException, SafeModeException, UnresolvedLinkException,
    IOException {
  CreateSymlinkRequestProto req = CreateSymlinkRequestProto.newBuilder()
      .setTarget(target)
      .setLink(link)
      .setDirPerm(PBHelper.convert(dirPerm))
      .setCreateParent(createParent)
      .build();
  try {
    rpcProxy.createSymlink(null, req);
  } catch (ServiceException e) {
    throw ProtobufHelper.getRemoteException(e);
  }
}
 
Example 21
Source Project: big-c   Source File: TestRegistryOperations.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOverwrite() throws Throwable {
  ServiceRecord written = putExampleServiceEntry(ENTRY_PATH, 0);
  ServiceRecord resolved1 = operations.resolve(ENTRY_PATH);
  resolved1.description = "resolved1";
  try {
    operations.bind(ENTRY_PATH, resolved1, 0);
    fail("overwrite succeeded when it should have failed");
  } catch (FileAlreadyExistsException expected) {
    // expected
  }

  // verify there's no changed
  ServiceRecord resolved2 = operations.resolve(ENTRY_PATH);
  assertMatches(written, resolved2);
  operations.bind(ENTRY_PATH, resolved1, BindFlags.OVERWRITE);
  ServiceRecord resolved3 = operations.resolve(ENTRY_PATH);
  assertMatches(resolved1, resolved3);
}
 
Example 22
Source Project: big-c   Source File: JobHistoryEventHandler.java    License: Apache License 2.0 6 votes vote down vote up
private void mkdir(FileSystem fs, Path path, FsPermission fsp)
    throws IOException {
  if (!fs.exists(path)) {
    try {
      fs.mkdirs(path, fsp);
      FileStatus fsStatus = fs.getFileStatus(path);
      LOG.info("Perms after creating " + fsStatus.getPermission().toShort()
          + ", Expected: " + fsp.toShort());
      if (fsStatus.getPermission().toShort() != fsp.toShort()) {
        LOG.info("Explicitly setting permissions to : " + fsp.toShort()
            + ", " + fsp);
        fs.setPermission(path, fsp);
      }
    } catch (FileAlreadyExistsException e) {
      LOG.info("Directory: [" + path + "] already exists.");
    }
  }
}
 
Example 23
Source Project: big-c   Source File: HistoryFileManager.java    License: Apache License 2.0 6 votes vote down vote up
private void mkdir(FileContext fc, Path path, FsPermission fsp)
    throws IOException {
  if (!fc.util().exists(path)) {
    try {
      fc.mkdir(path, fsp, true);

      FileStatus fsStatus = fc.getFileStatus(path);
      LOG.info("Perms after creating " + fsStatus.getPermission().toShort()
          + ", Expected: " + fsp.toShort());
      if (fsStatus.getPermission().toShort() != fsp.toShort()) {
        LOG.info("Explicitly setting permissions to : " + fsp.toShort()
            + ", " + fsp);
        fc.setPermission(path, fsp);
      }
    } catch (FileAlreadyExistsException e) {
      LOG.info("Directory: [" + path + "] already exists.");
    }
  }
}
 
Example 24
Source Project: big-c   Source File: NativeS3FileSystem.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public FSDataOutputStream create(Path f, FsPermission permission,
    boolean overwrite, int bufferSize, short replication, long blockSize,
    Progressable progress) throws IOException {

  if (exists(f) && !overwrite) {
    throw new FileAlreadyExistsException("File already exists: " + f);
  }
  
  if(LOG.isDebugEnabled()) {
    LOG.debug("Creating new file '" + f + "' in S3");
  }
  Path absolutePath = makeAbsolute(f);
  String key = pathToKey(absolutePath);
  return new FSDataOutputStream(new NativeS3FsOutputStream(getConf(), store,
      key, progress, bufferSize), statistics);
}
 
Example 25
Source Project: hadoop-ozone   Source File: ContainerUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that this is indeed a new container.
 *
 * @param containerFile - Container File to verify
 * @throws FileAlreadyExistsException
 */
public static void verifyIsNewContainer(File containerFile) throws
    FileAlreadyExistsException {
  Logger log = LoggerFactory.getLogger(ContainerSet.class);
  Preconditions.checkNotNull(containerFile, "containerFile Should not be " +
      "null");
  if (containerFile.getParentFile().exists()) {
    log.error("Container already exists on disk. File: {}", containerFile
        .toPath());
    throw new FileAlreadyExistsException("container already exists on " +
        "disk.");
  }
}
 
Example 26
Source Project: hadoop-ozone   Source File: BasicOzoneClientAdapterImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public OzoneFSOutputStream createFile(String key, short replication,
    boolean overWrite, boolean recursive) throws IOException {
  incrementCounter(Statistic.OBJECTS_CREATED);
  try {
    OzoneOutputStream ozoneOutputStream = null;
    if (replication == ReplicationFactor.ONE.getValue()
        || replication == ReplicationFactor.THREE.getValue()) {
      ReplicationFactor clientReplication = ReplicationFactor
          .valueOf(replication);
      ozoneOutputStream = bucket.createFile(key, 0, replicationType,
          clientReplication, overWrite, recursive);
    } else {
      ozoneOutputStream = bucket.createFile(key, 0, replicationType,
          replicationFactor, overWrite, recursive);
    }
    return new OzoneFSOutputStream(ozoneOutputStream.getOutputStream());
  } catch (OMException ex) {
    if (ex.getResult() == OMException.ResultCodes.FILE_ALREADY_EXISTS
        || ex.getResult() == OMException.ResultCodes.NOT_A_FILE) {
      throw new FileAlreadyExistsException(
          ex.getResult().name() + ": " + ex.getMessage());
    } else {
      throw ex;
    }
  }
}
 
Example 27
Source Project: hadoop-ozone   Source File: BasicOzoneClientAdapterImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to create an directory specified by key name in bucket.
 *
 * @param keyName key name to be created as directory
 * @return true if the key is created, false otherwise
 */
@Override
public boolean createDirectory(String keyName) throws IOException {
  LOG.trace("creating dir for key:{}", keyName);
  incrementCounter(Statistic.OBJECTS_CREATED);
  try {
    bucket.createDirectory(keyName);
  } catch (OMException e) {
    if (e.getResult() == OMException.ResultCodes.FILE_ALREADY_EXISTS) {
      throw new FileAlreadyExistsException(e.getMessage());
    }
    throw e;
  }
  return true;
}
 
Example 28
@Override
public OzoneFSOutputStream createFile(String pathStr, short replication,
    boolean overWrite, boolean recursive) throws IOException {
  incrementCounter(Statistic.OBJECTS_CREATED);
  OFSPath ofsPath = new OFSPath(pathStr);
  if (ofsPath.isRoot() || ofsPath.isVolume() || ofsPath.isBucket()) {
    throw new IOException("Cannot create file under root or volume.");
  }
  String key = ofsPath.getKeyName();
  try {
    // Hadoop CopyCommands class always sets recursive to true
    OzoneBucket bucket = getBucket(ofsPath, recursive);
    OzoneOutputStream ozoneOutputStream = null;
    if (replication == ReplicationFactor.ONE.getValue()
        || replication == ReplicationFactor.THREE.getValue()) {
      ReplicationFactor clientReplication = ReplicationFactor
          .valueOf(replication);
      ozoneOutputStream = bucket.createFile(key, 0, replicationType,
          clientReplication, overWrite, recursive);
    } else {
      ozoneOutputStream = bucket.createFile(key, 0, replicationType,
          replicationFactor, overWrite, recursive);
    }
    return new OzoneFSOutputStream(ozoneOutputStream.getOutputStream());
  } catch (OMException ex) {
    if (ex.getResult() == OMException.ResultCodes.FILE_ALREADY_EXISTS
        || ex.getResult() == OMException.ResultCodes.NOT_A_FILE) {
      throw new FileAlreadyExistsException(
          ex.getResult().name() + ": " + ex.getMessage());
    } else {
      throw ex;
    }
  }
}
 
Example 29
/**
 * Helper method to create an directory specified by key name in bucket.
 *
 * @param pathStr path to be created as directory
 * @return true if the key is created, false otherwise
 */
@Override
public boolean createDirectory(String pathStr) throws IOException {
  LOG.trace("creating dir for path: {}", pathStr);
  incrementCounter(Statistic.OBJECTS_CREATED);
  OFSPath ofsPath = new OFSPath(pathStr);
  if (ofsPath.getVolumeName().isEmpty()) {
    // Volume name unspecified, invalid param, return failure
    return false;
  }
  if (ofsPath.getBucketName().isEmpty()) {
    // Create volume only
    objectStore.createVolume(ofsPath.getVolumeName());
    return true;
  }
  String keyStr = ofsPath.getKeyName();
  try {
    OzoneBucket bucket = getBucket(ofsPath, true);
    // Empty keyStr here indicates only volume and bucket is
    // given in pathStr, so getBucket above should handle the creation
    // of volume and bucket. We won't feed empty keyStr to
    // bucket.createDirectory as that would be a NPE.
    if (keyStr != null && keyStr.length() > 0) {
      bucket.createDirectory(keyStr);
    }
  } catch (OMException e) {
    if (e.getResult() == OMException.ResultCodes.FILE_ALREADY_EXISTS) {
      throw new FileAlreadyExistsException(e.getMessage());
    }
    throw e;
  }
  return true;
}
 
Example 30
Source Project: XLearning   Source File: TextMultiOutputFormat.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void checkOutputSpecs(FileSystem ignored, JobConf job)
    throws FileAlreadyExistsException,
    InvalidJobConfException, IOException {
  super.checkOutputSpecs(ignored, job);

  if (!getCompressOutput(job)) {
    throw new InvalidJobConfException("TextMultiOutputFormat only for compressed mode.");
  }

}