-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-1648] Added custom kafka meta fields and custom kafka avro decoder. #2598
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1068595
Added custom kafka fields and custom kafka avro decoder.
2d6112b
Reduced KafkaAvroSchemaDeserializer to just call a super class method…
2c86577
Added unit tests, minor code improvements.
1860266
Addresed style issues.
c706d1c
Merge remote-tracking branch 'upstream/master' into custom-deserializer
9bc13c2
Apparently reflection utils can't create a class if one of the parame…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
...ilities/src/main/java/org/apache/hudi/utilities/decoders/KafkaAvroSchemaDeserializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| /* | ||
| * 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.decoders; | ||
|
|
||
| import io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException; | ||
| import io.confluent.kafka.serializers.AbstractKafkaAvroDeserializer; | ||
| import io.confluent.kafka.serializers.KafkaAvroDeserializer; | ||
| import io.confluent.kafka.serializers.NonRecordContainer; | ||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.generic.GenericDatumReader; | ||
| import org.apache.avro.io.DatumReader; | ||
| import org.apache.avro.io.DecoderFactory; | ||
| import org.apache.hudi.common.config.TypedProperties; | ||
| import org.apache.hudi.utilities.UtilHelpers; | ||
| import org.apache.hudi.utilities.schema.SchemaProvider; | ||
| import org.apache.kafka.common.errors.SerializationException; | ||
| import org.codehaus.jackson.node.JsonNodeFactory; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| 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 static final String SCHEMA_PROVIDER_CLASS_PROP = "hoodie.deltastreamer.schemaprovider.class"; | ||
|
|
||
| private final DecoderFactory decoderFactory = DecoderFactory.get(); | ||
| private Schema sourceSchema; | ||
|
|
||
| public KafkaAvroSchemaDeserializer() {} | ||
|
|
||
| @Override | ||
| public void configure(Map<String, ?> configs, boolean isKey) { | ||
| super.configure(configs, isKey); | ||
| try { | ||
| TypedProperties props = getConvertToTypedProperties(configs); | ||
| SchemaProvider schemaProvider = UtilHelpers.createSchemaProvider( | ||
| props.getString(SCHEMA_PROVIDER_CLASS_PROP), props, null); | ||
| sourceSchema = Objects.requireNonNull(schemaProvider).getSourceSchema(); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Pretty much copy-paste from the {@link AbstractKafkaAvroDeserializer} except line 87: | ||
| * DatumReader reader = new GenericDatumReader(schema, sourceSchema); | ||
| * <p> | ||
| * We need to inject reader schema during deserialization or later stages of the pipeline break. | ||
| * | ||
| * @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 { | ||
| // Even if the caller requests schema & version, if the payload is null we cannot include it. | ||
| // The caller must handle | ||
| // this case. | ||
| if (payload == null) { | ||
| return null; | ||
| } | ||
| int id = -1; | ||
| try { | ||
| ByteBuffer buffer = getByteBuffer(payload); | ||
| id = buffer.getInt(); | ||
| Schema schema = schemaRegistry.getByID(id); | ||
|
|
||
| int length = buffer.limit() - 1 - idSize; | ||
| final Object result; | ||
| if (schema.getType().equals(Schema.Type.BYTES)) { | ||
| byte[] bytes = new byte[length]; | ||
| buffer.get(bytes, 0, length); | ||
| result = bytes; | ||
| } else { | ||
| int start = buffer.position() + buffer.arrayOffset(); | ||
| DatumReader reader = new GenericDatumReader(schema, sourceSchema); | ||
| Object object = | ||
| reader.read(null, decoderFactory.binaryDecoder(buffer.array(), start, length, null)); | ||
|
|
||
| if (schema.getType().equals(Schema.Type.STRING)) { | ||
| object = object.toString(); // Utf8 -> String | ||
| } | ||
| result = object; | ||
| } | ||
|
|
||
| if (includeSchemaAndVersion) { | ||
| // Annotate the schema with the version. Note that we only do this if the schema + | ||
| // version are requested, i.e. in Kafka Connect converters. This is critical because that | ||
| // code *will not* rely on exact schema equality. Regular deserializers *must not* include | ||
| // this information because it would return schemas which are not equivalent. | ||
| // | ||
| // Note, however, that we also do not fill in the connect.version field. This allows the | ||
| // Converter to let a version provided by a Kafka Connect source take priority over the | ||
| // schema registry's ordering (which is implicit by auto-registration time rather than | ||
| // explicit from the Connector). | ||
| Integer version = schemaRegistry.getVersion(getSubjectName(topic, isKey), schema); | ||
| if (schema.getType() == Schema.Type.UNION) { | ||
| // Can't set additional properties on a union schema since it's just a list, so set it | ||
| // on the first non-null entry | ||
| for (Schema memberSchema : schema.getTypes()) { | ||
| if (memberSchema.getType() != Schema.Type.NULL) { | ||
| memberSchema.addProp( | ||
| SCHEMA_REGISTRY_SCHEMA_VERSION_PROP, | ||
| JsonNodeFactory.instance.numberNode(version)); | ||
| break; | ||
| } | ||
| } | ||
| } else { | ||
| schema.addProp( | ||
| SCHEMA_REGISTRY_SCHEMA_VERSION_PROP, JsonNodeFactory.instance.numberNode(version)); | ||
| } | ||
| if (schema.getType().equals(Schema.Type.RECORD)) { | ||
| return result; | ||
| } else { | ||
| return new NonRecordContainer(schema, result); | ||
| } | ||
| } else { | ||
| return result; | ||
| } | ||
| } catch (IOException | RuntimeException e) { | ||
| // avro deserialization may throw AvroRuntimeException, NullPointerException, etc | ||
| throw new SerializationException("Error deserializing Avro message for id " + id, e); | ||
| } catch (RestClientException e) { | ||
| throw new SerializationException("Error retrieving Avro schema for id " + id, e); | ||
| } | ||
| } | ||
|
|
||
| private ByteBuffer getByteBuffer(byte[] payload) { | ||
| ByteBuffer buffer = ByteBuffer.wrap(payload); | ||
| if (buffer.get() != MAGIC_BYTE) { | ||
| throw new SerializationException("Unknown magic byte!"); | ||
| } | ||
| return buffer; | ||
| } | ||
|
|
||
| private TypedProperties getConvertToTypedProperties(Map<String, ?> configs) { | ||
| TypedProperties typedProperties = new TypedProperties(); | ||
| for (Entry<String, ?> entry : configs.entrySet()) { | ||
| typedProperties.put(entry.getKey(), entry.getValue()); | ||
| } | ||
| return typedProperties; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this looks good.
Uh oh!
There was an error while loading. Please reload this page.
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 whats the plan in keeping this file in sync with AbstractKafkaAvroDeserializer with version upgrades?
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.
does this work.
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.
It actually should work, there was a history behind this change that predates me that ended up with only sourceSchema change as I currently see. So yah, it will drastically simplify things. Thanks for catching it!
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.
one more clarification @vburenin. Is this approach (passing sourceSchema instead of reader schema) is going to be useful for everyone(kakfa source users), even if they are not interested in meta fields to assist in schema evolution? If yes, wondering if we should make this customer deserializer as default kafka deserializer for hudi going forward?
Can you throw some light here.
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.
It is definitely going to be useful for everybody. Kafka fields were added after this change was introduced. We had to keep up with the schema changes in the registry and this was the way to do that. The primary assumption, that is most likely true for every user, is that a schema is evolving... so, this is the way to keep up with the schema evolution.
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.
👍