-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
KafkaAvroSchemaDeserializer.java
91 lines (80 loc) · 3.18 KB
/
KafkaAvroSchemaDeserializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* 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.hudi.utilities.deser;
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient;
import io.confluent.kafka.serializers.KafkaAvroDeserializer;
import org.apache.avro.Schema;
import org.apache.hudi.DataSourceWriteOptions;
import org.apache.hudi.common.config.TypedProperties;
import org.apache.hudi.common.util.ReflectionUtils;
import org.apache.hudi.exception.HoodieException;
import org.apache.hudi.utilities.schema.SchemaProvider;
import org.apache.kafka.common.errors.SerializationException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
/**
* Extending {@link KafkaAvroSchemaDeserializer} as we need to be able to inject reader schema during deserialization.
*/
public class KafkaAvroSchemaDeserializer extends KafkaAvroDeserializer {
private Schema sourceSchema;
public KafkaAvroSchemaDeserializer() {}
public KafkaAvroSchemaDeserializer(SchemaRegistryClient client, Map<String, ?> props) {
super(client, props);
}
@Override
public void configure(Map<String, ?> configs, boolean isKey) {
super.configure(configs, isKey);
try {
TypedProperties props = getConvertToTypedProperties(configs);
String className = props.getString(DataSourceWriteOptions.SCHEMA_PROVIDER_CLASS_PROP());
SchemaProvider schemaProvider = (SchemaProvider) ReflectionUtils.loadClass(className, props);
sourceSchema = Objects.requireNonNull(schemaProvider).getSourceSchema();
} catch (Throwable e) {
throw new HoodieException(e);
}
}
/**
* We need to inject sourceSchema instead of reader schema during deserialization or later stages of the pipeline.
*
* @param includeSchemaAndVersion
* @param topic
* @param isKey
* @param payload
* @param readerSchema
* @return
* @throws SerializationException
*/
@Override
protected Object deserialize(
boolean includeSchemaAndVersion,
String topic,
Boolean isKey,
byte[] payload,
Schema readerSchema)
throws SerializationException {
return super.deserialize(includeSchemaAndVersion, topic, isKey, payload, sourceSchema);
}
protected TypedProperties getConvertToTypedProperties(Map<String, ?> configs) {
TypedProperties typedProperties = new TypedProperties();
for (Entry<String, ?> entry : configs.entrySet()) {
typedProperties.put(entry.getKey(), entry.getValue());
}
return typedProperties;
}
}