{"record":{"id":"8501e49921872f60","repo":"apache/hadoop","slug":"already-exists","errorCode":null,"errorMessage":"{} already exists","messagePattern":"(.+?) already exists","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java","lineNumber":252,"sourceCode":"  public FSDataOutputStream append(Path f, int bufferSize,\n      Progressable progress) throws IOException {\n    throw new IOException(\"Not supported\");\n  }\n\n  @Override\n  public FSDataOutputStream create(Path f, FsPermission permission,\n      boolean overwrite, int bufferSize, short replication, long blockSize,\n      Progressable progress) throws IOException {\n    FileStatus fileStatus;\n\n    try {\n      fileStatus = getFileStatus(f);\n      if (fileStatus.isDirectory()) {\n        throw new FileAlreadyExistsException(f + \" is a directory\");\n      }\n      if (!overwrite) {\n        // path references a file and overwrite is disabled\n        throw new FileAlreadyExistsException(f + \" already exists\");\n      }\n\n    } catch (FileNotFoundException e) {\n      LOG.debug(\"Creating a new file: [{}] in COS.\", f);\n    }\n\n    Path absolutePath = makeAbsolute(f);\n    String key = pathToKey(absolutePath);\n    return new FSDataOutputStream(\n        new CosNOutputStream(getConf(), store, key, blockSize,\n            this.boundedIOThreadPool), statistics);\n  }\n\n  private boolean rejectRootDirectoryDelete(boolean isEmptyDir,\n      boolean recursive) throws PathIOException {\n    if (isEmptyDir) {\n      return true;\n    }","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java#L234-L270","documentation":"CosNFileSystem.create throws FileAlreadyExistsException when the target path already exists as a file in the COS bucket and the caller did not set overwrite. Before creating the object, create() calls getFileStatus; a directory at the path produces the sibling 'is a directory' error, while an existing file with overwrite=false produces this one. This follows the Hadoop FileSystem contract that existing data must never be silently clobbered.","triggerScenarios":"fs.create(path, false); fs.create(path, permission, false /*overwrite*/, ...) ; createNonRecursive with an EnumSet that lacks CreateFlag.OVERWRITE; any committer or writer (FileOutputCommitter, Spark _temporary writes, Hive staged writes) targeting a path that already holds a file.","commonSituations":"Re-running a MapReduce/Spark/Hive job into the same output directory without cleaning it; a previous run crashed after committing output; checkpoint files already present; passing the overwrite argument in the wrong positional slot so it lands as false.","solutions":["Pass overwrite=true: fs.create(path, true), or include CreateFlag.OVERWRITE for createNonRecursive.","Delete the stale target first: if (fs.exists(p) && fs.getFileStatus(p).isFile()) fs.delete(p, false);","Write to a unique output path per run (jobId/timestamp suffix) or let the output committer promote files only on commit.","If the existing file is unexpected, inspect getFileStatus(p).getModificationTime() to find which process created it before deleting."],"exampleFix":"// before\nFSDataOutputStream out = fs.create(outPath, false); // FileAlreadyExistsException: ... already exists\n\n// after\nif (fs.exists(outPath) && fs.getFileStatus(outPath).isFile()) {\n  fs.delete(outPath, false);\n}\nFSDataOutputStream out = fs.create(outPath, true);","handlingStrategy":"validation","validationCode":"Path p = new Path('cosn://bucket/out/part-00000');\nboolean overwriteRequested = true;\nif (fs.exists(p) && fs.getFileStatus(p).isFile() && !overwriteRequested) {\n  throw new IllegalStateException('Refusing to overwrite existing file: ' + p);\n}","typeGuard":"static boolean isAlreadyExists(Throwable t) {\n  return t instanceof FileAlreadyExistsException;\n}","tryCatchPattern":"try {\n  try (FSDataOutputStream out = fs.create(p, false)) { /* write */ }\n} catch (FileAlreadyExistsException e) {\n  // expected on rerun: clean up and retry once, or treat as no-op\n  if (allowOverwrite) { fs.delete(p, false); /* retry create once */ } else { throw e; }\n}","preventionTips":["Always pass an explicit overwrite flag instead of relying on defaults","Clean or version output directories between job runs","Use an output committer so files are promoted only on commit"],"tags":["cosn","tencent-cloud","hadoop","filesystem","create","file-already-exists"],"backgroundTag":"file-already-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}