ApacheHudiTimestampBasedKeyGenerator无法按年月分区的问题
创始人
2024-09-06 00:30:22
0

使用自定义的分区函数来解决按年月分区的问题。

示例代码如下:

import org.apache.hudi.common.util.TypedProperties;
import org.apache.hudi.keygen.TimestampBasedKeyGenerator;
import org.apache.hudi.util.DateUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CustomTimestampBasedKeyGenerator extends TimestampBasedKeyGenerator {

    public static final String PARTITION_BY_YEAR_MONTH_FIELD = "partition.by.year.month.field";

    private String partitionByYearMonthField;

    @Override
    public void init(TypedProperties props) {
        super.init(props);
        partitionByYearMonthField = props.getString(PARTITION_BY_YEAR_MONTH_FIELD, "");
    }

    @Override
    public String getPartitionPath(final String key, final Object value) {
        String partitionPath = "";
        if (partitionByYearMonthField.equals(FIELD_VAL_SEPARATOR)) {
            List parts = Arrays.asList(key.split(FIELD_SEPARATOR));
            partitionPath = DateUtils
                    .convertDateToPath(DateUtils.getDateTime(parts.get(partitionColumns.indexOf(partitionByYearMonthField))));
        } else if (!partitionByYearMonthField.isEmpty()) {
            partitionPath = DateUtils
                    .convertDateToPath(DateUtils.getDateTime(value.toString()));
        }
        return partitionPath;
    }
}

在这个自定义的TimestampBasedKeyGenerator类中,我们添加了新的配置项“partition.by.year.month.field”,它表示按年月分区的字段。

然后重写了getPartitionPath方法,通过该方法来生成分区路径。在这个方法中,我们首先根据“partition.by.year.month.field”字段的值来判断是否需要按年月分区,如果需要,我们就把该字段对应的值转换成年月格式的字符串,作为分区路径。

最后,在使用该自定义的分区函数时,只需要在相应的properties中设置“partition.by.year.month.field”的值为需要按年月分区的字段即可。例如:

TypedProperties props = new TypedProperties();
props.setProperty(TimestampBasedKeyGenerator.TIMESTAMP_PRECISION_FIELD_PROP, "seconds");
props.setProperty(TimestampBasedKeyGenerator.PARTITIONPATH_FIELD_PROP, "datestr");
props.setProperty(CustomTimestampBasedKeyGenerator.PARTITION_BY_YEAR_MONTH_FIELD, "yearMonth");
CustomTimestampBasedKeyGenerator keyGenerator = new CustomTimestampBasedKeyGenerator();
keyGenerator.init(props);

相关内容

热门资讯

外挂九分钟!德州微扑克辅助德扑... 外挂九分钟!德州微扑克辅助德扑线上有机器人(果真真的有挂)-今日头条;1、很好的工具软件,可以解锁游...
私人房三分钟!哈糖大菠萝可以弄... 私人房三分钟!哈糖大菠萝可以弄的wpk透视辅助测试(原来真的有挂)-小红书1.哈糖大菠萝可以弄的 a...
辅助7分钟!德州之星app有w... 辅助7分钟!德州之星app有wpk俱乐部24小时(都是真的有挂)-百度知乎1、玩家可以在wpk俱乐部...
免费四分钟!微扑克wpk有挂a... 免费四分钟!微扑克wpk有挂aapoker外挂实测(一贯真的有挂)-今日头条1)aapoker外挂实...
检测十分钟!德州之星有外挂德扑... 检测十分钟!德州之星有外挂德扑之星如何开房间(本来真的有挂)-知乎1、德扑之星如何开房间ai机器人多...
私人房三分钟!德州ai机器人w... 私人房三分钟!德州ai机器人wpk用有辅助器(总是真的有挂)-知乎1、操作简单,无需注册,只需要使用...
安卓版9分钟!wopoker轻... 您好,wopoker轻量版外挂这款游戏可以开挂的,确实是有挂的,需要了解加微【487309276】很...
教学7分钟!wpk德州ai辅助... 教学7分钟!wpk德州ai辅助神器aapoker线上规律(一贯真的有挂)-哔哩哔哩教学7分钟!wpk...
透明挂三分钟!微扑克可以用模拟... 透明挂三分钟!微扑克可以用模拟器wopoker辅助器ios(总是真的有挂)-微博客户端1、微扑克可以...
私人局4分钟!impoker有... 私人局4分钟!impoker有挂德扑数据软件(果然真的有挂)-小红书;1、德扑数据软件透视辅助简单,...