【问题标题】:Sorting files working locally but not on kubernetes排序在本地工作但不在 kubernetes 上的文件
【发布时间】:2020-10-23 01:47:40
【问题描述】:

我有大约 1000 个名称中带有日期的文件,我想按文件名中的这个日期对它们进行排序,然后选择日期与参数相同或早于参数的最新文件。
我写了这个:

 Pattern PATTERN = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}-file.csv");
    try {
      deviceFiles = Files.list(filesDir.toPath())
          .filter(path -> PATTERN.matcher(path.getFileName().toString()).matches()
          && !getDate(path).isAfter(ARGUMENT_DATE))
          .collect(Collectors.toList());
      Arrays.sort(deviceFiles.toArray(), new FileNamePathDateComparator());
      logger.info("All files found are " + deviceFiles.stream().map(stream -> stream.toAbsolutePath().toString()).collect(Collectors.joining("\n")));
      if (deviceFiles.isEmpty())
        throw new IllegalStateException("There were no device files found");
      else {
        String deviceFilePath = deviceFiles.get(deviceFiles.size() - 1).toAbsolutePath().toString();
        logger.info("Found device file: " + deviceFilePath);
        return deviceFilePath;
      }
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }

getDate 方法:

 private LocalDate getDate(Path path)
  {
    try {
      String[] parts = path.getFileName().toString().split("-");
      return LocalDate.of(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
    } catch (NumberFormatException ex) {
      throw new IllegalArgumentException("bye", ex);
    }
  }

比较器:

 class FileNamePathDateComparator implements Comparator{
    @Override
    public int compare(Object o1, Object o2)
    {
      return getDate((Path)o1).compareTo(getDate((Path)o2));
    }
  }

当我在本地运行它时,我看到记录器打印了所有正确排序的文件,比较器工作得很好。
但是在 kubernetes 集群上,文件是随机打印的,我不明白。

【问题讨论】:

    标签: java kubernetes


    【解决方案1】:

    已修复!我已将比较器放在流中而不是最终列表中,它工作正常。
    如果同时有人可以提供解释,我将不胜感激。

     deviceFiles = Files.list(filesDir.toPath())
          .filter(path -> PATTERN.matcher(path.getFileName().toString()).matches()
          && !getDate(path).isAfter(executionDate))
          .sorted(new FileNamePathDateComparator()::compare)
          .collect(Collectors.toList());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      相关资源
      最近更新 更多