Search in sources :

Example 16 with RawLocalFileSystem

use of org.apache.hadoop.fs.RawLocalFileSystem in project apex-core by apache.

the class FSUtil method copyToLocalFileSystem.

/**
 * Download the file from dfs to local file.
 *
 * @param fs
 * @param destinationFile
 * @param dfsFile
 * @param conf
 * @return
 * @throws IOException
 */
public static File copyToLocalFileSystem(FileSystem fs, String destinationPath, String destinationFile, String dfsFile, Configuration conf) throws IOException {
    File destinationDir = new File(destinationPath);
    if (!destinationDir.exists() && !destinationDir.mkdirs()) {
        throw new RuntimeException("Unable to create local directory");
    }
    try (RawLocalFileSystem localFileSystem = new RawLocalFileSystem()) {
        // allow app user to access local dir
        FsPermission permissions = new FsPermission(FsAction.ALL, FsAction.NONE, FsAction.NONE);
        localFileSystem.setPermission(new Path(destinationDir.getAbsolutePath()), permissions);
        Path dfsFilePath = new Path(dfsFile);
        File localFile = new File(destinationDir, destinationFile);
        FileUtil.copy(fs, dfsFilePath, localFile, false, conf);
        // set permissions on actual file to be read-only for user
        permissions = new FsPermission(FsAction.READ, FsAction.NONE, FsAction.NONE);
        localFileSystem.setPermission(new Path(localFile.getAbsolutePath()), permissions);
        return localFile;
    }
}
Also used : Path(org.apache.hadoop.fs.Path) RawLocalFileSystem(org.apache.hadoop.fs.RawLocalFileSystem) FsPermission(org.apache.hadoop.fs.permission.FsPermission) File(java.io.File)

Aggregations

RawLocalFileSystem (org.apache.hadoop.fs.RawLocalFileSystem)16 Path (org.apache.hadoop.fs.Path)9 IOException (java.io.IOException)7 FileSystem (org.apache.hadoop.fs.FileSystem)7 Test (org.junit.Test)6 File (java.io.File)5 Configuration (org.apache.hadoop.conf.Configuration)5 FileStatus (org.apache.hadoop.fs.FileStatus)3 LocalFileSystem (org.apache.hadoop.fs.LocalFileSystem)3 FileNotFoundException (java.io.FileNotFoundException)2 URISyntaxException (java.net.URISyntaxException)2 FsPermission (org.apache.hadoop.fs.permission.FsPermission)2 FloatWritable (org.apache.hadoop.io.FloatWritable)2 IntWritable (org.apache.hadoop.io.IntWritable)2 NullWritable (org.apache.hadoop.io.NullWritable)2 Text (org.apache.hadoop.io.Text)2 Counters (org.apache.hadoop.mapred.Counters)2 Counter (org.apache.hadoop.mapred.Counters.Counter)2 Writer (org.apache.hadoop.mapred.IFile.Writer)2 JobConf (org.apache.hadoop.mapred.JobConf)2