Search in sources :

Example 6 with HoodieKeyGeneratorException

use of org.apache.hudi.exception.HoodieKeyGeneratorException in project hudi by apache.

the class CustomKeyGenerator method getPartitionPath.

private String getPartitionPath(Option<GenericRecord> record, Option<Row> row, Option<Pair<InternalRow, StructType>> internalRowStructTypePair) {
    if (getPartitionPathFields() == null) {
        throw new HoodieKeyException("Unable to find field names for partition path in cfg");
    }
    String partitionPathField;
    StringBuilder partitionPath = new StringBuilder();
    // Corresponds to no partition case
    if (getPartitionPathFields().size() == 1 && getPartitionPathFields().get(0).isEmpty()) {
        return "";
    }
    for (String field : getPartitionPathFields()) {
        String[] fieldWithType = field.split(customAvroKeyGenerator.SPLIT_REGEX);
        if (fieldWithType.length != 2) {
            throw new HoodieKeyGeneratorException("Unable to find field names for partition path in proper format");
        }
        partitionPathField = fieldWithType[0];
        CustomAvroKeyGenerator.PartitionKeyType keyType = CustomAvroKeyGenerator.PartitionKeyType.valueOf(fieldWithType[1].toUpperCase());
        switch(keyType) {
            case SIMPLE:
                if (record.isPresent()) {
                    partitionPath.append(new SimpleKeyGenerator(config, partitionPathField).getPartitionPath(record.get()));
                } else if (row.isPresent()) {
                    partitionPath.append(new SimpleKeyGenerator(config, partitionPathField).getPartitionPath(row.get()));
                } else {
                    partitionPath.append(new SimpleKeyGenerator(config, partitionPathField).getPartitionPath(internalRowStructTypePair.get().getKey(), internalRowStructTypePair.get().getValue()));
                }
                break;
            case TIMESTAMP:
                try {
                    if (record.isPresent()) {
                        partitionPath.append(new TimestampBasedKeyGenerator(config, partitionPathField).getPartitionPath(record.get()));
                    } else if (row.isPresent()) {
                        partitionPath.append(new TimestampBasedKeyGenerator(config, partitionPathField).getPartitionPath(row.get()));
                    } else {
                        partitionPath.append(new TimestampBasedKeyGenerator(config, partitionPathField).getPartitionPath(internalRowStructTypePair.get().getKey(), internalRowStructTypePair.get().getValue()));
                    }
                } catch (IOException ioe) {
                    throw new HoodieKeyGeneratorException("Unable to initialise TimestampBasedKeyGenerator class", ioe);
                }
                break;
            default:
                throw new HoodieKeyGeneratorException("Please provide valid PartitionKeyType with fields! You provided: " + keyType);
        }
        partitionPath.append(customAvroKeyGenerator.getDefaultPartitionPathSeparator());
    }
    partitionPath.deleteCharAt(partitionPath.length() - 1);
    return partitionPath.toString();
}
Also used : HoodieKeyGeneratorException(org.apache.hudi.exception.HoodieKeyGeneratorException) HoodieKeyException(org.apache.hudi.exception.HoodieKeyException) IOException(java.io.IOException)

Example 7 with HoodieKeyGeneratorException

use of org.apache.hudi.exception.HoodieKeyGeneratorException in project hudi by apache.

the class HoodieSparkKeyGeneratorFactory method getKeyGeneratorClassName.

public static String getKeyGeneratorClassName(TypedProperties props) {
    String keyGeneratorClass = props.getString(HoodieWriteConfig.KEYGENERATOR_CLASS_NAME.key(), null);
    if (StringUtils.isNullOrEmpty(keyGeneratorClass)) {
        String keyGeneratorType = props.getString(HoodieWriteConfig.KEYGENERATOR_TYPE.key(), KeyGeneratorType.SIMPLE.name());
        LOG.info("The value of {} is empty, use SIMPLE", HoodieWriteConfig.KEYGENERATOR_TYPE.key());
        KeyGeneratorType keyGeneratorTypeEnum;
        try {
            keyGeneratorTypeEnum = KeyGeneratorType.valueOf(keyGeneratorType.toUpperCase(Locale.ROOT));
        } catch (IllegalArgumentException e) {
            throw new HoodieKeyGeneratorException("Unsupported keyGenerator Type " + keyGeneratorType);
        }
        switch(keyGeneratorTypeEnum) {
            case SIMPLE:
                keyGeneratorClass = SimpleKeyGenerator.class.getName();
                break;
            case COMPLEX:
                keyGeneratorClass = ComplexKeyGenerator.class.getName();
                break;
            case TIMESTAMP:
                keyGeneratorClass = TimestampBasedKeyGenerator.class.getName();
                break;
            case CUSTOM:
                keyGeneratorClass = CustomKeyGenerator.class.getName();
                break;
            case NON_PARTITION:
                keyGeneratorClass = NonpartitionedKeyGenerator.class.getName();
                break;
            case GLOBAL_DELETE:
                keyGeneratorClass = GlobalDeleteKeyGenerator.class.getName();
                break;
            default:
                throw new HoodieKeyGeneratorException("Unsupported keyGenerator Type " + keyGeneratorType);
        }
    }
    return keyGeneratorClass;
}
Also used : HoodieKeyGeneratorException(org.apache.hudi.exception.HoodieKeyGeneratorException) GlobalDeleteKeyGenerator(org.apache.hudi.keygen.GlobalDeleteKeyGenerator) CustomKeyGenerator(org.apache.hudi.keygen.CustomKeyGenerator) ComplexKeyGenerator(org.apache.hudi.keygen.ComplexKeyGenerator) TimestampBasedKeyGenerator(org.apache.hudi.keygen.TimestampBasedKeyGenerator) SimpleKeyGenerator(org.apache.hudi.keygen.SimpleKeyGenerator) KeyGeneratorType(org.apache.hudi.keygen.constant.KeyGeneratorType) NonpartitionedKeyGenerator(org.apache.hudi.keygen.NonpartitionedKeyGenerator)

Aggregations

HoodieKeyGeneratorException (org.apache.hudi.exception.HoodieKeyGeneratorException)7 KeyGeneratorType (org.apache.hudi.keygen.constant.KeyGeneratorType)4 IOException (java.io.IOException)3 HoodieKeyException (org.apache.hudi.exception.HoodieKeyException)2 ComplexKeyGenerator (org.apache.hudi.keygen.ComplexKeyGenerator)2 CustomKeyGenerator (org.apache.hudi.keygen.CustomKeyGenerator)2 GlobalDeleteKeyGenerator (org.apache.hudi.keygen.GlobalDeleteKeyGenerator)2 NonpartitionedKeyGenerator (org.apache.hudi.keygen.NonpartitionedKeyGenerator)2 SimpleKeyGenerator (org.apache.hudi.keygen.SimpleKeyGenerator)2 TimestampBasedKeyGenerator (org.apache.hudi.keygen.TimestampBasedKeyGenerator)2 StandardCharsets (java.nio.charset.StandardCharsets)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 FileSystem (org.apache.hadoop.fs.FileSystem)1 HoodieFileStatus (org.apache.hudi.avro.model.HoodieFileStatus)1