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;
  }
相关推荐
mengge.cloud11 小时前
0909华为云计算类综合服务小白实验教程
linux·运维·服务器·网络·华为云·云计算
暖核21 小时前
华为云运维三件套实战:云审计 + 云监控 + 云日志完整操作指南
linux·运维·华为云
mengge.cloud1 天前
0909华为云网络类服务实践小白实验教程
linux·运维·服务器·网络·华为云·php
Thomas21431 天前
kafka 存储flink changelog两种方式和形态
分布式·flink·kafka
智码看视界1 天前
Edge AI 与 IoT 全栈系列:Edge AI 全栈 2:ESP32 + MQTT + Flink 实时 IoT 数据管道
物联网·flink·esp32·全栈开发·iot平台·实时流处理·edgeai
逐流人1 天前
华为云服务全景指南:从基础架构到运维实践
运维·华为云·云计算·云服务·ecs
March.s2 天前
深入理解华为云存储云服务:从云硬盘到云备份的全景指南
华为云
fb_123452 天前
华为云存储服务实战全解:EVS 挂载、快照救数据、OBS 管理、SFS 共享、CBR 备份还原(保姆级教程)
华为云
SatanII2 天前
华为云ECS实践:从创建、镜像制作到弹性伸缩完整实操指南
linux·运维·服务器·学习·centos·华为云
丨晨 丨2 天前
华为云“基建狂魔”养成记:从“删库跑路”到“原地复活”的硬核求生指南
华为云