Hadoop API 通过租户代理访问 Kerberos 安全 Hadoop 集群

概述

访问HDFS其实很简单

这里记录一些方法,具备下面的特征:

  • 通过租户代理访问 Kerberos 认证的 HADOOP 资源
  • 获取 FileSystem 对象的技巧:FileSystem.get(URI, configuration)
  • 通用 doAs模板
  • 通用 hdfsCommand模板,使用 try-with-resources。
  • 截取 hdfs://namenode/user/...,使得 namenode对应得 FileSystem 能够缓存。
  • FileSystem.getDefaultUri(...) 如果使用这个方法,则路径必须以core-site.xml得 fs.defaultFs 为开头。会出现错误 Wrong FS: hdfs://dc1, expected: viewfs://ssss。
  • 通过指定 FileSystem.get() 第一个参数,能保证和实际得 namenode 一致。
  • 因此能同时访问 hdfs://或者viewfs://。

通用代理

  public static <T> T doAs(Supplier<T> command, String clusterName) {
    return UserGroupInformation.createProxyUser(clusterName, SecureYarn.ugi).doAs((PrivilegedAction<T>) command::get);
  }

  public static URI getFsUri(String filePath) {
    URI fsUri;
    if (filePath.startsWith("hdfs://")) {
      String uri = filePath.substring(0, filePath.indexOf("/user"));
      fsUri = new Path(uri).toUri();
    } else {
      fsUri = null;
    }
    return fsUri;
  }

listFiles 和 hdfsCommand 模板

  public static FileStatus[] listFiles(String filePath, String clusterName, Configuration configuration) {
    return doAs(() -> hdfsCommand(c -> c.listStatus(new Path(filePath)), getFsUri(filePath), configuration), clusterName);
  }

  public static <V> V hdfsCommand(ThrowingFunction<FileSystem, V, Exception> command, URI path, Configuration hdfsConfig) {
    try (FileSystem fileSystem = FileSystem.get(path == null ? FileSystem.getDefaultUri(hdfsConfig):path, hdfsConfig)) {
      return command.apply(fileSystem);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
posted @   一杯半盏  阅读(44)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2022-11-30 根据类查找缺少的jar包,在已有jar包内查找类
2021-11-30 困扰多年的Quartz重复调度的问题,终于找到原因
点击右上角即可分享
微信分享提示