DefaultFilenamePolicy extends FilenamePolicy { static ResourceId constructName( ResourceId baseFilename, String shardTemplate, String suffix, int shardNum, int numShards, @Nullable String paneStr, @Nullable String windowStr) { String prefix = extractFilename(baseFilename); StringBuffer sb = new StringBuffer(); sb.append(prefix); Matcher m = SHARD_FORMAT_RE.matcher(shardTemplate); while (m.find()) { boolean isCurrentShardNum = (m.group(1).charAt(0) == 'S'); boolean isNumberOfShards = (m.group(1).charAt(0) == 'N'); boolean isPane = (m.group(1).charAt(0) == 'P') && paneStr != null; boolean isWindow = (m.group(1).charAt(0) == 'W') && windowStr != null; char[] zeros = new char[m.end() - m.start()]; Arrays.fill(zeros, '0'); DecimalFormat df = new DecimalFormat(String.valueOf(zeros)); if (isCurrentShardNum) { String formatted = df.format(shardNum); m.appendReplacement(sb, formatted); } else if (isNumberOfShards) { String formatted = df.format(numShards); m.appendReplacement(sb, formatted); } else if (isPane) { m.appendReplacement(sb, paneStr); } else if (isWindow) { m.appendReplacement(sb, windowStr); } } m.appendTail(sb); sb.append(suffix); return baseFilename .getCurrentDirectory() .resolve(sb.toString(), StandardResolveOptions.RESOLVE_FILE); } @VisibleForTesting  DefaultFilenamePolicy(Params params); static DefaultFilenamePolicy fromStandardParameters(
      ValueProvider<ResourceId> baseFilename,
      @Nullable String shardTemplate,
      @Nullable String filenameSuffix,
      boolean windowedWrites); static DefaultFilenamePolicy fromParams(Params params); @Override @Nullable ResourceId unwindowedFilename(
      int shardNumber, int numShards, OutputFileHints outputFileHints); @Override ResourceId windowedFilename(
      int shardNumber,
      int numShards,
      BoundedWindow window,
      PaneInfo paneInfo,
      OutputFileHints outputFileHints); @Override void populateDisplayData(DisplayData.Builder builder); static final String DEFAULT_UNWINDOWED_SHARD_TEMPLATE; static final String DEFAULT_WINDOWED_SHARD_TEMPLATE; }
@Test public void testConstructName() { assertEquals( "/path/to/output-001-of-123.txt", constructName("/path/to/output", "-SSS-of-NNN", ".txt", 1, 123, null, null)); assertEquals( "/path/to/out.txt/part-00042", constructName("/path/to/out.txt", "/part-SSSSS", "", 42, 100, null, null)); assertEquals("/path/to/out.txt", constructName("/path/to/ou", "t.t", "xt", 1, 1, null, null)); assertEquals( "/path/to/out0102shard.txt", constructName("/path/to/out", "SSNNshard", ".txt", 1, 2, null, null)); assertEquals( "/path/to/out-2/1.part-1-of-2.txt", constructName("/path/to/out", "-N/S.part-S-of-N", ".txt", 1, 2, null, null)); }
@Test public void testConstructNameWithLargeShardCount() { assertEquals( "/out-100-of-5000.txt", constructName("/out", "-SS-of-NN", ".txt", 100, 5000, null, null)); }
@Test public void testConstructWindowedName() { assertEquals( "/path/to/output-001-of-123.txt", constructName("/path/to/output", "-SSS-of-NNN", ".txt", 1, 123, null, null)); assertEquals( "/path/to/output-001-of-123-PPP-W.txt", constructName("/path/to/output", "-SSS-of-NNN-PPP-W", ".txt", 1, 123, null, null)); assertEquals( "/path/to/out" + ".txt/part-00042-myPaneStr-myWindowStr", constructName( "/path/to/out.txt", "/part-SSSSS-P-W", "", 42, 100, "myPaneStr", "myWindowStr")); assertEquals( "/path/to/out.txt", constructName("/path/to/ou", "t.t", "xt", 1, 1, "myPaneStr2", "anotherWindowStr")); assertEquals( "/path/to/out0102shard-oneMoreWindowStr-anotherPaneStr.txt", constructName( "/path/to/out", "SSNNshard-W-P", ".txt", 1, 2, "anotherPaneStr", "oneMoreWindowStr")); assertEquals( "/out-2/1.part-1-of-2-slidingWindow1-myPaneStr3-windowslidingWindow1-" + "panemyPaneStr3.txt", constructName( "/out", "-N/S.part-S-of-N-W-P-windowW-paneP", ".txt", 1, 2, "myPaneStr3", "slidingWindow1")); assertEquals( "/out.txt/part-00042-myWindowStr-pane-11-true-false", constructName( "/out.txt", "/part-SSSSS-W-P", "", 42, 100, "pane-11-true-false", "myWindowStr")); assertEquals( "/path/to/out.txt", constructName("/path/to/ou", "t.t", "xt", 1, 1, "pane", "anotherWindowStr")); assertEquals( "/out0102shard-oneMoreWindowStr-pane--1-false-false-pane--1-false-false.txt", constructName( "/out", "SSNNshard-W-P-P", ".txt", 1, 2, "pane--1-false-false", "oneMoreWindowStr")); assertEquals( "/path/to/out-2/1.part-1-of-2-sWindow1-winsWindow1-ppaneL.txt", constructName( "/path/to/out", "-N/S.part-S-of-N-W-winW-pP", ".txt", 1, 2, "paneL", "sWindow1")); }