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;
  }
相关推荐
猿小猴子1 天前
主流 AI IDE 之一的 华为云码道「CodeArts」 介绍
ide·人工智能·ai·华为云
念陌曦2 天前
Flink总结
大数据·flink
岁岁种桃花儿3 天前
Flink从入门到上天系列第二十五篇:Flink和Kafka连接时的精准一次性
大数据·flink·kafka
岁岁种桃花儿4 天前
Flink从入门到上天系列第二十四篇:Flink中的保存点
大数据·flink
yumgpkpm4 天前
华为昇腾910B 开源软件GPUStack的介绍(Cloudera CDH、CDP)
人工智能·hadoop·elasticsearch·flink·kafka·企业微信·big data
岁岁种桃花儿5 天前
Flink从入门到上天系列第二十二篇:Flink中通过UI查看检查点
大数据·ui·flink
D愿你归来仍是少年5 天前
Apache Flink 算子(Operator)深度解析
大数据·flink·apache
岁岁种桃花儿5 天前
Flink从入门到上天系列第二十一篇:Flink当中的检查点配置
大数据·flink
岁岁种桃花儿5 天前
Flink从入门到上天系列第二十三篇:Flink中增量检查点和最终检查点
大数据·flink
yumgpkpm5 天前
Apache Spark 和 Flink,处理实时大数据流对比(Cloudera CDH、CDP)
flink·spark·apache