[HUDI-2937] Introduce a pulsar implementation of hoodie write commit … (#4217)
* [HUDI-2937] Introduce a pulsar implementation of hoodie write commit callback * [HUDI-2937] Introduce a pulsar implementation of hoodie write commit callback * [HUDI-2937] Introduce a pulsar implementation of hoodie write commit callback * [HUDI-2937] Introduce a pulsar implementation of hoodie write commit callback * [HUDI-2937] Introduce a pulsar implementation of hoodie write commit callback * [HUDI-2937] Introduce a pulsar implementation of hoodie write commit callback * [HUDI-2937] Introduce a pulsar implementation of hoodie write commit callback
This commit is contained in:
@@ -19,14 +19,26 @@
|
||||
|
||||
package org.apache.hudi.common.util;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class DateTimeUtils {
|
||||
private static final Map<String, ChronoUnit> LABEL_TO_UNIT_MAP =
|
||||
Collections.unmodifiableMap(initMap());
|
||||
|
||||
/**
|
||||
* Parse input String to a {@link java.time.Instant}.
|
||||
*
|
||||
* @param s Input String should be Epoch time in millisecond or ISO-8601 format.
|
||||
*/
|
||||
public static Instant parseDateTime(String s) throws DateTimeParseException {
|
||||
@@ -37,4 +49,141 @@ public class DateTimeUtils {
|
||||
return Instant.parse(s);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the given string to a java {@link Duration}. The string is in format "{length
|
||||
* value}{time unit label}", e.g. "123ms", "321 s". If no time unit label is specified, it will
|
||||
* be considered as milliseconds.
|
||||
*
|
||||
* <p>Supported time unit labels are:
|
||||
*
|
||||
* <ul>
|
||||
* <li>DAYS: "d", "day"
|
||||
* <li>HOURS: "h", "hour"
|
||||
* <li>MINUTES: "min", "minute"
|
||||
* <li>SECONDS: "s", "sec", "second"
|
||||
* <li>MILLISECONDS: "ms", "milli", "millisecond"
|
||||
* <li>MICROSECONDS: "µs", "micro", "microsecond"
|
||||
* <li>NANOSECONDS: "ns", "nano", "nanosecond"
|
||||
* </ul>
|
||||
*
|
||||
* @param text string to parse.
|
||||
*/
|
||||
public static Duration parseDuration(String text) {
|
||||
ValidationUtils.checkArgument(!StringUtils.isNullOrEmpty(text));
|
||||
|
||||
final String trimmed = text.trim();
|
||||
ValidationUtils.checkArgument(!trimmed.isEmpty(), "argument is an empty- or whitespace-only string");
|
||||
|
||||
final int len = trimmed.length();
|
||||
int pos = 0;
|
||||
|
||||
char current;
|
||||
while (pos < len && (current = trimmed.charAt(pos)) >= '0' && current <= '9') {
|
||||
pos++;
|
||||
}
|
||||
|
||||
final String number = trimmed.substring(0, pos);
|
||||
final String unitLabel = trimmed.substring(pos).trim().toLowerCase(Locale.US);
|
||||
|
||||
if (number.isEmpty()) {
|
||||
throw new NumberFormatException("text does not start with a number");
|
||||
}
|
||||
|
||||
final long value;
|
||||
try {
|
||||
value = Long.parseLong(number); // this throws a NumberFormatException on overflow
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"The value '"
|
||||
+ number
|
||||
+ "' cannot be re represented as 64bit number (numeric overflow).");
|
||||
}
|
||||
|
||||
if (unitLabel.isEmpty()) {
|
||||
return Duration.of(value, ChronoUnit.MILLIS);
|
||||
}
|
||||
|
||||
ChronoUnit unit = LABEL_TO_UNIT_MAP.get(unitLabel);
|
||||
if (unit != null) {
|
||||
return Duration.of(value, unit);
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"Time interval unit label '"
|
||||
+ unitLabel
|
||||
+ "' does not match any of the recognized units: "
|
||||
+ TimeUnit.getAllUnits());
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, ChronoUnit> initMap() {
|
||||
Map<String, ChronoUnit> labelToUnit = new HashMap<>();
|
||||
for (TimeUnit timeUnit : TimeUnit.values()) {
|
||||
for (String label : timeUnit.getLabels()) {
|
||||
labelToUnit.put(label, timeUnit.getUnit());
|
||||
}
|
||||
}
|
||||
return labelToUnit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enum which defines time unit, mostly used to parse value from configuration file.
|
||||
*/
|
||||
private enum TimeUnit {
|
||||
DAYS(ChronoUnit.DAYS, singular("d"), plural("day")),
|
||||
HOURS(ChronoUnit.HOURS, singular("h"), plural("hour")),
|
||||
MINUTES(ChronoUnit.MINUTES, singular("min"), plural("minute")),
|
||||
SECONDS(ChronoUnit.SECONDS, singular("s"), plural("sec"), plural("second")),
|
||||
MILLISECONDS(ChronoUnit.MILLIS, singular("ms"), plural("milli"), plural("millisecond")),
|
||||
MICROSECONDS(ChronoUnit.MICROS, singular("µs"), plural("micro"), plural("microsecond")),
|
||||
NANOSECONDS(ChronoUnit.NANOS, singular("ns"), plural("nano"), plural("nanosecond"));
|
||||
|
||||
private static final String PLURAL_SUFFIX = "s";
|
||||
|
||||
private final List<String> labels;
|
||||
|
||||
private final ChronoUnit unit;
|
||||
|
||||
TimeUnit(ChronoUnit unit, String[]... labels) {
|
||||
this.unit = unit;
|
||||
this.labels =
|
||||
Arrays.stream(labels)
|
||||
.flatMap(Arrays::stream)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param label the original label
|
||||
* @return the singular format of the original label
|
||||
*/
|
||||
private static String[] singular(String label) {
|
||||
return new String[] {label};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param label the original label
|
||||
* @return both the singular format and plural format of the original label
|
||||
*/
|
||||
private static String[] plural(String label) {
|
||||
return new String[] {label, label + PLURAL_SUFFIX};
|
||||
}
|
||||
|
||||
public List<String> getLabels() {
|
||||
return labels;
|
||||
}
|
||||
|
||||
public ChronoUnit getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public static String getAllUnits() {
|
||||
return Arrays.stream(TimeUnit.values())
|
||||
.map(TimeUnit::createTimeUnitString)
|
||||
.collect(Collectors.joining(", "));
|
||||
}
|
||||
|
||||
private static String createTimeUnitString(TimeUnit timeUnit) {
|
||||
return timeUnit.name() + ": (" + String.join(" | ", timeUnit.getLabels()) + ")";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user