-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
KafkaAvroDeserializer.java
129 lines (106 loc) · 4.02 KB
/
KafkaAvroDeserializer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright 2018 Confluent Inc.
*
* Licensed 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 io.confluent.kafka.serializers;
import org.apache.avro.Schema;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.serialization.Deserializer;
import java.io.IOException;
import java.util.Map;
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient;
import static io.confluent.kafka.serializers.KafkaAvroDeserializerConfig.SPECIFIC_AVRO_KEY_TYPE_CONFIG;
import static io.confluent.kafka.serializers.KafkaAvroDeserializerConfig.SPECIFIC_AVRO_READER_CONFIG;
import static io.confluent.kafka.serializers.KafkaAvroDeserializerConfig.SPECIFIC_AVRO_VALUE_TYPE_CONFIG;
import org.apache.avro.specific.SpecificRecord;
import org.apache.kafka.common.config.ConfigException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class KafkaAvroDeserializer extends AbstractKafkaAvroDeserializer
implements Deserializer<Object> {
private static final Logger log = LoggerFactory.getLogger(KafkaAvroDeserializer.class);
/**
* Constructor used by Kafka consumer.
*/
public KafkaAvroDeserializer() {
}
public KafkaAvroDeserializer(SchemaRegistryClient client) {
this.schemaRegistry = client;
this.ticker = ticker(client);
}
public KafkaAvroDeserializer(SchemaRegistryClient client, Map<String, ?> props) {
this(client, props, false);
}
public KafkaAvroDeserializer(SchemaRegistryClient client, Map<String, ?> props, boolean isKey) {
this.schemaRegistry = client;
this.ticker = ticker(client);
configure(props, isKey);
}
@Override
public void configure(Map<String, ?> props, boolean isKey) {
this.isKey = isKey;
final String specificAvroClassLookupKey = isKey
? SPECIFIC_AVRO_KEY_TYPE_CONFIG
: SPECIFIC_AVRO_VALUE_TYPE_CONFIG;
final KafkaAvroDeserializerConfig config = deserializerConfig(props);
final Class<?> type = config.getClass(specificAvroClassLookupKey);
if (type != null && !config.getBoolean(SPECIFIC_AVRO_READER_CONFIG)) {
if (log.isWarnEnabled()) {
log.warn(
String.format(
"'%s' value of '%s' is ignored because '%s' is false",
specificAvroClassLookupKey,
type.getName(),
SPECIFIC_AVRO_READER_CONFIG
)
);
}
}
if (type != null && !SpecificRecord.class.isAssignableFrom(type)) {
throw new ConfigException(
String.format("Value '%s' specified for '%s' is not a '%s'",
type.getName(),
specificAvroClassLookupKey,
SpecificRecord.class.getName()
)
);
}
configure(deserializerConfig(props), type);
}
@Override
public Object deserialize(String topic, byte[] bytes) {
return deserialize(topic, null, bytes);
}
@Override
public Object deserialize(String topic, Headers headers, byte[] bytes) {
return deserialize(topic, isKey, headers, bytes, specificAvroReaderSchema);
}
/**
* Pass a reader schema to get an Avro projection
*/
public Object deserialize(String topic, byte[] bytes, Schema readerSchema) {
return deserialize(topic, isKey, bytes, readerSchema);
}
public Object deserialize(String topic, Headers headers, byte[] bytes, Schema readerSchema) {
return deserialize(topic, isKey, headers, bytes, readerSchema);
}
@Override
public void close() {
try {
super.close();
} catch (IOException e) {
throw new RuntimeException("Exception while closing deserializer", e);
}
}
}