-
Notifications
You must be signed in to change notification settings - Fork 3
/
AvroSchemaTranslator.java
69 lines (58 loc) · 2.28 KB
/
AvroSchemaTranslator.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
/*
* Copyright 2020 Confluent Inc.
*
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.confluent.ksql.serde.avro;
import com.google.common.collect.ImmutableMap;
import io.confluent.connect.avro.AvroData;
import io.confluent.connect.avro.AvroDataConfig;
import io.confluent.kafka.schemaregistry.ParsedSchema;
import io.confluent.kafka.schemaregistry.avro.AvroSchema;
import io.confluent.ksql.serde.connect.ConnectSchemaTranslator;
import io.confluent.ksql.util.KsqlException;
import java.util.Map;
import java.util.Objects;
import org.apache.avro.SchemaParseException;
import org.apache.kafka.connect.data.Schema;
/**
* Translates between Connect and Avro Schema Registry schema types.
*/
class AvroSchemaTranslator implements ConnectSchemaTranslator {
private AvroData avroData = new AvroData(new AvroDataConfig(ImmutableMap.of()));
private final AvroProperties formatProps;
AvroSchemaTranslator(final AvroProperties formatProps) {
this.formatProps = Objects.requireNonNull(formatProps, "formatProps");
}
@Override
public String name() {
return AvroSchema.TYPE;
}
@Override
public void configure(final Map<String, ?> configs) {
avroData = new AvroData(new AvroDataConfig(configs));
}
@Override
public Schema toConnectSchema(final ParsedSchema schema) {
return avroData.toConnectSchema(((AvroSchema) schema).rawSchema());
}
@Override
public ParsedSchema fromConnectSchema(final Schema schema) {
final Schema avroCompatibleSchema = AvroSchemas
.getAvroCompatibleConnectSchema(schema, formatProps.getFullSchemaName());
try {
return new AvroSchema(avroData.fromConnectSchema(avroCompatibleSchema));
} catch (final SchemaParseException e) {
throw new KsqlException("Schema is not compatible with Avro: " + e.getMessage(), e);
}
}
}