public static void writeSchemaToFile ( Schema schema , Path filePath , Path tempFilePath , FileSystem fs , boolean overwrite , FsPermission perm ) throws IOException { boolean fileExists = fs . exists ( filePath ) ; if ( ! overwrite ) { Preconditions . checkState ( ! fileExists , filePath + " already exists" ) ; } else { // delete the target file now if not using a staging file if ( fileExists && null == tempFilePath ) { HadoopUtils . deletePath ( fs , filePath , true ) ; // file has been removed fileExists = false ; } } // If the file exists then write to a temp file to make the replacement as close to atomic as possible Path writeFilePath = fileExists ? tempFilePath : filePath ; try ( DataOutputStream dos = fs . create ( writeFilePath ) ) { dos . writeChars ( schema . toString ( ) ) ; } fs . setPermission ( writeFilePath , perm ) ; // Replace existing file with the staged file if ( fileExists ) { if ( ! fs . delete ( filePath , true ) ) { throw new IOException ( String . format ( "Failed to delete %s while renaming %s to %s" , filePath , tempFilePath , filePath ) ) ; } HadoopUtils . movePath ( fs , tempFilePath , fs , filePath , true , fs . getConf ( ) ) ; } }
Write a schema to a file