You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@gobblin.apache.org by GitBox <gi...@apache.org> on 2019/12/12 19:05:42 UTC

[GitHub] [incubator-gobblin] autumnust commented on a change in pull request #2846: [GOBBLIN-1001] Implement TimePartitionGlobFinder

autumnust commented on a change in pull request #2846: [GOBBLIN-1001] Implement TimePartitionGlobFinder
URL: https://github.com/apache/incubator-gobblin/pull/2846#discussion_r357318005
 
 

 ##########
 File path: gobblin-data-management/src/main/java/org/apache/gobblin/data/management/dataset/TimePartitionGlobFinder.java
 ##########
 @@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.gobblin.data.management.dataset;
+
+import java.io.IOException;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.gobblin.data.management.retention.profile.ConfigurableGlobDatasetFinder;
+import org.apache.gobblin.dataset.DatasetsFinder;
+import org.apache.gobblin.dataset.FileSystemDataset;
+import org.apache.gobblin.util.PathUtils;
+
+
+@Slf4j
+public class TimePartitionGlobFinder implements DatasetsFinder<FileSystemDataset> {
+  private static final String CONF_PREFIX = "timePartitionGlobFinder.";
+
+  public static final String PARTITION_PREFIX = CONF_PREFIX + "partitionPrefix";
+  public static final String TIME_FORMAT = CONF_PREFIX + "timeFormat";
+  public static final String TIME_ZONE = CONF_PREFIX + "timeZone";
+  public static final String ENABLE_EMPTY_PARTITION = CONF_PREFIX + "enableEmptyPartition";
+
+  private static final String DEFAULT_TIME_ZONE = "America/Los_Angeles";
+
+  private final String datasetPattern;
+  private final String datasetPartitionPattern;
+  private final Path yesterdayPartition;
+
+  private final Properties props;
+  private final FileSystem fs;
+  private final boolean enableEmptyPartition;
+
+  public TimePartitionGlobFinder(FileSystem fs, Properties properties) {
+    datasetPattern = properties.getProperty(ConfigurableGlobDatasetFinder.DATASET_FINDER_PATTERN_KEY);
+    Path datasetPartitionPath = new Path(datasetPattern);
+
+    String partitionPrefix = properties.getProperty(PARTITION_PREFIX, "");
+    String timeFormat = properties.getProperty(TIME_FORMAT).trim();
+    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(timeFormat);
+    datasetPartitionPattern = new Path(datasetPartitionPath,
+        partitionPrefix + derivePartitionPattern(timeFormat)).toString();
+    log.info("Dataset partition pattern is {}", datasetPartitionPattern);
+
+    ZonedDateTime curTime = ZonedDateTime.now(ZoneId.of(properties.getProperty(TIME_ZONE, DEFAULT_TIME_ZONE)));
+    yesterdayPartition = new Path(partitionPrefix + timeFormatter.format(curTime.minusDays(1)));
+    log.info("Yesterday partition is {}", yesterdayPartition);
+
+    enableEmptyPartition = Boolean.valueOf(properties.getProperty(ENABLE_EMPTY_PARTITION));
+
+    props = properties;
+    this.fs = fs;
+  }
+
+  /**
+   * Derive partition glob pattern from time format. For example:
+   * <p> given {@code timePattern=yyyy/MM/dd}, return '*'/'*'/'*' (adding single quote to avoid breaking comments)
+   * <p> given {@code timePattern=yyyy-MM-dd}, return *
+   */
+  private String derivePartitionPattern(String timeFormat) {
+    String[] parts = timeFormat.split("/");
+    StringBuilder pattern = new StringBuilder("*");
+    for (int i = 1; i < parts.length; i++) {
+      pattern.append("/*");
+    }
+    return pattern.toString();
+  }
+
+  @Override
+  public List<FileSystemDataset> findDatasets()
+      throws IOException {
+    try {
+      return doFindDatasets();
+    } finally {
+      // Recover ConfigurableGlobDatasetFinder.DATASET_FINDER_PATTERN_KEY config
+      this.props.setProperty(ConfigurableGlobDatasetFinder.DATASET_FINDER_PATTERN_KEY, datasetPattern);
+    }
+  }
+
+  private List<FileSystemDataset> doFindDatasets() throws IOException {
+    if (!enableEmptyPartition) {
+      log.info("Will not create empty file system dataset");
+      return findDatasets(datasetPartitionPattern);
+    }
+
+    // Find datasets
+    List<FileSystemDataset> datasets = findDatasets(datasetPattern);
+    // Identify yesterday dataset partitions
+    Set<Path> yesterdayDatasetPartitions = new HashSet<>();
+    datasets.forEach(dataset -> yesterdayDatasetPartitions.add(createYesterdayDatasetPartition(dataset)));
+
+    // Find all dataset time partitions
+    List<FileSystemDataset> datasetPartitions = findDatasets(datasetPartitionPattern);
 
 Review comment:
   Could this code block be optimized to be a single expression like : 
   `datasetPartitions.addAll(yesterdaypartitions.stream().allMatch(x->!datasetPartitions.contains(x)).map(x -> createEmptyFileDataset))` ? 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services