30、Flink中操作已经配置好的远程文件系统

背景:flink作业中既配置了obs作为chk的远程文件系统,又在作业中读取obs文件内容时,使用obsclient会导致任务无法创建chk目录而启动失败。

解决办法:使用flink-core里的fileSystem来操作 。这样就不用去使用对应文件系统的客户端了,而是直接使用的是当前flink中配置的远程文件系统。

java 复制代码
 public static Long getKafkaOffsetFromHoodie2(String hdfsPath) throws IOException {

    final Path path = new Path(hdfsPath);
    //获取文件系统
    final FileSystem fileSystem = path.getFileSystem();
    final FileStatus[] fileStatuses = fileSystem.listStatus(path);

    if (fileSystem.exists(path)) {

      if (fileStatuses.length > 0) {
        // 获取最新commit文件
        final FileStatus latestFile =
            Arrays.stream(fileStatuses)
                .filter(x -> x.getPath().getName().endsWith(".commit"))
                .max(Comparator.comparingLong(FileStatus::getModificationTime))
                .orElse(null);
        if (latestFile != null) {
          LOG.info("最新commit文件为:{}", latestFile.getPath().getPath());
          // 读取文件内容
          try (FSDataInputStream inputStream = fileSystem.open(latestFile.getPath());
              BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = reader.readLine()) != null) {
              stringBuilder.append(line);
            }
            JSONObject jsonObject = JSON.parseObject(stringBuilder.toString());

            if (jsonObject.containsKey("extraMetadata")) {
              JSONObject extraMetadata = jsonObject.getJSONObject("extraMetadata");
              if (extraMetadata.containsKey("deltastreamer.checkpoint.key")) {
                String string = extraMetadata.getString("deltastreamer.checkpoint.key");
                String offset = string.split(",")[1].split(":")[1];
                LOG.info("当前偏移量==>  " + offset);
                return Long.valueOf(offset);
              }
            } else {
              LOG.error("this is not an delta-stream mission");
              throw new IOException("this is not an delta-stream mission");
            }

          } catch (IOException e) {
            System.err.println("读取文件时发生错误: " + e.getMessage());
          }
        }

      } else {
        LOG.warn("{} 目录为空或无法访问", hdfsPath);
      }
    } else {
      LOG.error("{} 指定路径不是一个有效的目录", hdfsPath);
    }
    return null;
  }
相关推荐
渣渣盟14 小时前
Flink 流处理那些事儿:状态、时间与容错
大数据·flink
Justice Young18 小时前
Flink测试题目及知识点整理(一)
大数据·flink
渣渣盟18 小时前
构建企业级实时数据管道:Kafka + Flink 最佳实践
分布式·flink·kafka
juniperhan2 天前
Flink 系列第22篇:Flink SQL 参数配置与性能调优指南:从 Checkpoint 到聚合优化
大数据·数据仓库·分布式·sql·flink
不剪发的Tony老师2 天前
Flink CDC:一个基于流的实时数据集成工具
flink·etl
juniperhan2 天前
Flink 系列第21篇:Flink SQL 函数与 UDF 全解读:类型推导、开发要点与 Module 扩展
java·大数据·数据仓库·分布式·sql·flink
二十六画生的博客3 天前
Flink快照保留多久、多少个,设置参数
大数据·flink
渣渣盟3 天前
大数据技术栈全景图:从零到一的入门路线(深度实战版)
大数据·hadoop·python·flink·spark
hsD5mSMu53 天前
从零开始学Flink:Flink SQL 极简入门
大数据·sql·flink
亚马逊云开发者5 天前
EMR Core 节点部署 Flink Client 实战:Bootstrap Action 一次打包多次复用,解决调度系统提交任务的痛点
大数据·flink·bootstrap