hadoop使用java递归地将fspermission设置为dir

polhcujo  于 2021-06-02  发布在  Hadoop
关注(0)|答案(3)|浏览(481)

嗨,我有一个测试程序,加载文件到hdfs在这个路径 user/user1/data/app/type/file.gz 现在这个测试程序由多个用户运行多次。所以我想把文件权限设置为rwx,这样任何人都可以删除这个文件。我有以下代码

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

以上线给出 drwxrwxrwx 对于除file.gz以外的所有dir,它授予如下权限: -rw-r--r-- 为什么会这样?因为这个原因,除了我之外的其他用户无法通过测试程序删除这个文件。我可以通过测试程序删除文件,因为我有完整的权限。
请引导。我是hadoop新手。提前谢谢。

toiithl6

toiithl61#

我想 Hadoop 不提供java api以在您使用的版本中递归地提供权限。代码实际上是在授予dir权限 user/user1/data 没有别的了。你最好用 FileSystem.listStatus(Path f) 方法列出目录中的所有文件并使用 Filsystem.setPermission 对他们个人来说。它目前正在我的2.0.5-alpha-gphd-2.1.0.0集群上工作。
但是从 Hadoop 2.2.0 继续一个新的构造器 FsPermission(FsAction u, FsAction g, FsAction o, boolean sb) 正在提供。布尔字段可能是您想要的递归标志。但是文档(包括param name)太差,无法推断出具体的内容。
另外看看为什么“hadoopfs-mkdir”在权限被拒绝的情况下失败?尽管你的处境可能不同(对我来说 dfs.permissions.enabled 这仍然是事实)。

vbkedwbf

vbkedwbf2#

我是用scala写的,但我认为你可以很容易地修改它:

def changeUserGroup(user:String,fs:FileSystem, path: Path): Boolean ={
  val changedPermission = new FsPermission(FsAction.ALL,FsAction.ALL,FsAction.ALL, true)
  val fileList = fs.listFiles(path, true)
  while (fileList.hasNext()) {
    fs.setPermission(fileList.next().getPath(),changedPermission)
  }
    return true
  }

您还必须为错误处理添加逻辑,我总是返回true。

wvt8vs2t

wvt8vs2t3#

使用fsshell api解决了我的dir权限问题。这可能不是最佳的方式,但由于我是解决它的测试代码,它应该是好的。

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);
      }

相关问题