Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hadoop setting fsPermission recursively to dir using Java

Tags:

java

hadoop

hdfs

Hi I have test program which loads files into hdfs at this path user/user1/data/app/type/file.gz Now this test program runs multiple times by multiple users. So I want to set file permission to rwx so that anyone can delete this file. I have the following code

fs.setPermission(new Path("user/user1/data"),new FsPermission(FsAction.ALL,FsAction.ALL,FsAction.ALL)) 

Above line gives drwxrwxrwx to all dirs but for the file.gz it gives permission as -rw-r--r-- why so? Because of this reason another user apart from me not able to delete this file through test program. I can delete file through test program because I have full permssion.

Please guide. I am new to Hadoop. Thanks in advance.

like image 274
Umesh K Avatar asked Jan 09 '23 14:01

Umesh K


2 Answers

Using FsShell APIs solved my dir permission problem. It may be not be optimal way but since I am solving it for test code it should be fine.

      FsShell shell=new FsShell(conf);
      try {
        shell.run(new String[]{"-chmod","-R","777","user/usr1/data"});
      }
     catch (  Exception e) {
        LOG.error("Couldnt change the file permissions ",e);
        throw new IOException(e);
      }
like image 193
Umesh K Avatar answered Jan 12 '23 02:01

Umesh K


I think Hadoop doesn't provide a java API to provide permission recursively in the version you are using. The code is actually giving permission to the dir user/user1/data and nothing else. You should better use FileSystem.listStatus(Path f) method to list down all files in the directory and use Filsystem.setPermission to them individually. It's working currently on my 2.0.5-alpha-gphd-2.1.0.0 cluster.

However from Hadoop 2.2.0 onward a new constructor

FsPermission(FsAction u, FsAction g, FsAction o, boolean sb)

is being provided. The boolean field may be the recursive flag you wanted. But the documentation(including param name) is too poor to infer something concrete.

Also have a look at Why does "hadoop fs -mkdir" fail with Permission Denied? although your sitaution may be different. (in my case dfs.permissions.enabled is still true).

like image 25
blackSmith Avatar answered Jan 12 '23 03:01

blackSmith