-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-1650] Custom avro kafka deserializer. #2619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
62c5d82
d3e2872
1181457
6a9a7f7
de50b68
15c058a
bbbf899
b73c0e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /* | ||
| * 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 org.apache.hudi.common.config.TypedProperties; | ||
| import org.apache.hudi.utilities.sources.helpers.SchemaTestProvider; | ||
| import org.apache.hudi.utilities.testutils.UtilitiesTestBase; | ||
|
|
||
| import io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient; | ||
| import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient; | ||
| import io.confluent.kafka.serializers.KafkaAvroDeserializerConfig; | ||
| import io.confluent.kafka.serializers.KafkaAvroSerializer; | ||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.generic.GenericData; | ||
| import org.apache.avro.generic.GenericRecord; | ||
| import org.apache.avro.generic.IndexedRecord; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
| import java.util.Properties; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| /** | ||
| * Tests {@link KafkaAvroSchemaDeserializer}. | ||
| */ | ||
| public class TestKafkaAvroSchemaDeserializer extends UtilitiesTestBase { | ||
|
|
||
| private final SchemaRegistryClient schemaRegistry; | ||
| private final KafkaAvroSerializer avroSerializer; | ||
| private final String topic; | ||
| private final Schema origSchema = createUserSchema(); | ||
| private final Schema evolSchema = createExtendUserSchema(); | ||
| private Properties defaultConfig = new Properties(); | ||
|
|
||
| public TestKafkaAvroSchemaDeserializer() { | ||
| defaultConfig.put(KafkaAvroDeserializerConfig.SCHEMA_REGISTRY_URL_CONFIG, "bogus"); | ||
| defaultConfig.put("hoodie.deltastreamer.schemaprovider.class", SchemaTestProvider.class.getName()); | ||
| schemaRegistry = new MockSchemaRegistryClient(); | ||
| avroSerializer = new KafkaAvroSerializer(schemaRegistry, new HashMap(defaultConfig)); | ||
| topic = "test"; | ||
| } | ||
|
|
||
| private Schema createUserSchema() { | ||
| String userSchema = "{\"namespace\": \"example.avro\", \"type\": \"record\", " | ||
| + "\"name\": \"User\"," | ||
| + "\"fields\": [{\"name\": \"name\", \"type\": \"string\"}]}"; | ||
| Schema.Parser parser = new Schema.Parser(); | ||
| Schema schema = parser.parse(userSchema); | ||
| return schema; | ||
| } | ||
|
|
||
| private IndexedRecord createUserRecord() { | ||
| Schema schema = createUserSchema(); | ||
| GenericRecord avroRecord = new GenericData.Record(schema); | ||
| avroRecord.put("name", "testUser"); | ||
| return avroRecord; | ||
| } | ||
|
|
||
| private Schema createExtendUserSchema() { | ||
| String userSchema = "{\"namespace\": \"example.avro\", \"type\": \"record\", " | ||
| + "\"name\": \"User\"," | ||
| + "\"fields\": [{\"name\": \"name\", \"type\": \"string\"}, " | ||
| + "{\"name\": \"age\", \"type\": [\"null\", \"int\"], \"default\": null}]}"; | ||
| Schema.Parser parser = new Schema.Parser(); | ||
| Schema schema = parser.parse(userSchema); | ||
| return schema; | ||
| } | ||
|
|
||
| private IndexedRecord createExtendUserRecord() { | ||
| Schema schema = createExtendUserSchema(); | ||
| GenericRecord avroRecord = new GenericData.Record(schema); | ||
| avroRecord.put("name", "testUser"); | ||
| avroRecord.put("age", 30); | ||
| return avroRecord; | ||
| } | ||
|
|
||
| /** | ||
| * Tests {@link KafkaAvroSchemaDeserializer#deserialize(boolean, String, Boolean, byte[], Schema)}. | ||
| */ | ||
| @Test | ||
| public void testKafkaAvroSchemaDeserializer() { | ||
| byte[] bytesOrigRecord; | ||
| IndexedRecord avroRecord = createUserRecord(); | ||
| SchemaTestProvider.schemaToReturn.set(origSchema); | ||
| KafkaAvroSchemaDeserializer avroDeserializer = new KafkaAvroSchemaDeserializer(schemaRegistry, new HashMap(defaultConfig)); | ||
| avroDeserializer.configure(new HashMap(defaultConfig), false); | ||
| bytesOrigRecord = avroSerializer.serialize(topic, avroRecord); | ||
| // record is serialized in orig schema and deserialized using same schema. | ||
| assertEquals(avroRecord, avroDeserializer.deserialize(false, topic, false, bytesOrigRecord, origSchema)); | ||
|
|
||
| IndexedRecord avroRecordWithAllField = createExtendUserRecord(); | ||
| byte[] bytesExtendedRecord = avroSerializer.serialize(topic, avroRecordWithAllField); | ||
|
|
||
| SchemaTestProvider.schemaToReturn.set(evolSchema); | ||
| avroDeserializer = new KafkaAvroSchemaDeserializer(schemaRegistry, new HashMap(defaultConfig)); | ||
| avroDeserializer.configure(new HashMap(defaultConfig), false); | ||
| // record is serialized w/ evolved schema, and deserialized w/ evolved schema | ||
| IndexedRecord avroRecordWithAllFieldActual = (IndexedRecord) avroDeserializer.deserialize(false, topic, false, bytesExtendedRecord, evolSchema); | ||
| assertEquals(avroRecordWithAllField, avroRecordWithAllFieldActual); | ||
| assertEquals(avroRecordWithAllFieldActual.getSchema(), evolSchema); | ||
|
|
||
| // read old record w/ evolved schema. | ||
| IndexedRecord actualRec = (IndexedRecord) avroDeserializer.deserialize(false, topic, false, bytesOrigRecord, origSchema); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vburenin @nsivabalan I guess the comment is somewhat misleading here. The comment says "read old record with evolved schema", however we are passing
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad, got it now. |
||
| // record won't be equal to original record as we read w/ evolved schema. "age" will be added w/ default value of null | ||
| assertNotEquals(avroRecord, actualRec); | ||
| GenericRecord genericRecord = (GenericRecord) actualRec; | ||
| GenericRecord origGenRec = (GenericRecord) avroRecord; | ||
| assertEquals(genericRecord.get("name").toString(), origGenRec.get("name").toString()); | ||
| assertEquals(actualRec.getSchema(), evolSchema); | ||
| assertNull(genericRecord.get("age")); | ||
| } | ||
|
|
||
| protected TypedProperties getConvertToTypedProperties(Map<String, ?> configs) { | ||
| TypedProperties typedProperties = new TypedProperties(); | ||
| for (Entry<String, ?> entry : configs.entrySet()) { | ||
| typedProperties.put(entry.getKey(), entry.getValue()); | ||
| } | ||
| return typedProperties; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * 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.sources.helpers; | ||
|
|
||
| import org.apache.hudi.common.config.TypedProperties; | ||
| import org.apache.hudi.common.testutils.HoodieTestDataGenerator; | ||
| import org.apache.hudi.utilities.schema.SchemaProvider; | ||
|
|
||
| import org.apache.avro.Schema; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| /** | ||
| * {@link SchemaProvider} for tests. | ||
| */ | ||
| public class SchemaTestProvider extends SchemaProvider { | ||
|
|
||
| public static AtomicReference<Schema> schemaToReturn = new AtomicReference<>(HoodieTestDataGenerator.AVRO_SCHEMA); | ||
|
|
||
| public SchemaTestProvider(TypedProperties props) { | ||
| super(props); | ||
| } | ||
|
|
||
| @Override | ||
| public Schema getSourceSchema() { | ||
| return schemaToReturn.get(); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may I know if this will work for any schemaProvider? or are we making any assumptions?
Bcoz, as of now, guess we instantiate schemaProvider for DeltaStreamer in UtilHelpers.createSchemaProvider(className, props, jsc).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, and it fails if I pass "null" for jsc, so here is a custom one.