How to specify local file system in HDFS API?

Viewed 1981

I want to get access to a local file system via HDFS API. I have the following:

String filename;
//...
Path p = new Path(filename);
p.getFileSystem(new Configuration()).create(p);

The issue is I have HDFS node on the same machine and when I call p.getFileSystem(new Configuration()).create(p); it tries to create a HDFS file, not a local file. Is there a way to access a local filesystem via p.getFileSystem(new Configuration()).create(p)?

1 Answers

File name has to be fully qualified with file://

    String filePath="file:///tmp/test.txt";
    public  void createFile(String path,Configuration conf) throws IOException {
        FileSystem fs = FileSystem.get(URI.create(path), conf);
        fs.create(new Path(path));
        fs.close();
     }

Works for S3, HDFS and Local

Related