Hadoop FileSystem API Hadoop

private void deleteDirectory(Job job, String path) throws IOException
{
    FileSystem fs = FileSystem.get(job.getConfiguration());
    Path filePath = new Path(path);
    if( fs.exists(filePath) )
        fs.delete(filePath, true);
}

org.apache.hadoop.mapred.FileAlreadyExistsException: XXXXXXXX directory output already exists

Hadoop MapReduce를 Local에서 테스트를하거나, 클러스트에 배포하여 테스트를 할경우 기존에 있는 파일디렉토리때문에 Exception이 발생하게 된다.
Hadoop FileSystem API를 사용하여 파일을 직접 핸들링하여 삭제하거나 생성할 수 있다.
LocalFileSystem, DistributedFileSystem 인지에 따라서 잘 동작되어야 하기때문에  org.apache.hadoop.fs.* 패키지로 접근을 한다.

Tag :

Leave Comments