public final void cleanup() throws Exception { if (outputFile != null) { LOG.info("Deleting temporary file {}", outputFile); // outputFile may be null if open() was not called or failed. FileSystems.delete( Collections.singletonList(outputFile), StandardMoveOptions.IGNORE_MISSING_FILES); } }
static void removeTemporaryFiles(Iterable<String> files) throws IOException { ImmutableList.Builder<ResourceId> fileResources = ImmutableList.builder(); for (String file : files) { fileResources.add(FileSystems.matchNewResource(file, false /* isDirectory */)); } FileSystems.delete(fileResources.build()); } }
@Test public void testDeleteIgnoreMissingFiles() throws Exception { Path existingPath = temporaryFolder.newFile().toPath(); Path nonExistentPath = existingPath.resolveSibling("non-existent"); createFileWithContent(existingPath, "content1"); FileSystems.delete( toResourceIds(ImmutableList.of(existingPath, nonExistentPath), false /* isDirectory */)); }
@Override void cleanup(PassThroughThenCleanup.ContextContainer c) throws Exception { PipelineOptions options = c.getPipelineOptions(); BigQueryOptions bqOptions = options.as(BigQueryOptions.class); String jobUuid = c.getJobId(); final String extractDestinationDir = resolveTempLocation(bqOptions.getTempLocation(), "BigQueryExtractTemp", jobUuid); final String executingProject = bqOptions.getProject(); JobReference jobRef = new JobReference() .setProjectId(executingProject) .setJobId(getExtractJobId(createJobIdToken(bqOptions.getJobName(), jobUuid))); Job extractJob = getBigQueryServices().getJobService(bqOptions).getJob(jobRef); if (extractJob != null) { List<ResourceId> extractFiles = getExtractFilePaths(extractDestinationDir, extractJob); if (extractFiles != null && !extractFiles.isEmpty()) { FileSystems.delete( extractFiles, MoveOptions.StandardMoveOptions.IGNORE_MISSING_FILES); } } } };
List<ResourceId> sourceFiles = filesForLoadJobs.get(jobRef.getProjectId(), jobRef.getJobId()); FileSystems.delete(sourceFiles);
FileSystems.delete(sourceFiles); return new JobStatus().setState("DONE");