-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[HUDI-1129] Deltastreamer Add support for schema evolution #2012
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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
45 changes: 45 additions & 0 deletions
45
hudi-common/src/main/java/org/apache/hudi/common/model/BaseAvroPayloadWithSchema.java
This file contains 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,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.common.model; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.avro.generic.GenericRecord; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Base class for AVRO record based payloads, which stores writer schema as well. | ||
*/ | ||
public abstract class BaseAvroPayloadWithSchema extends BaseAvroPayload implements Serializable { | ||
/** | ||
* Schema used to convert avro to bytes. | ||
*/ | ||
protected final Schema writerSchema; | ||
|
||
/** | ||
* Instantiate {@link BaseAvroPayloadWithSchema}. | ||
* | ||
* @param record Generic record for the payload. | ||
* @param orderingVal {@link Comparable} to be used in pre combine. | ||
*/ | ||
public BaseAvroPayloadWithSchema(GenericRecord record, Comparable orderingVal) { | ||
super(record, orderingVal); | ||
this.writerSchema = record != null ? record.getSchema() : null; | ||
} | ||
} |
This file contains 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 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 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 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 |
---|---|---|
|
@@ -168,6 +168,7 @@ public static void initClass() throws Exception { | |
UtilitiesTestBase.Helpers.copyToDFS("delta-streamer-config/sql-transformer.properties", dfs, | ||
dfsBasePath + "/sql-transformer.properties"); | ||
UtilitiesTestBase.Helpers.copyToDFS("delta-streamer-config/source.avsc", dfs, dfsBasePath + "/source.avsc"); | ||
UtilitiesTestBase.Helpers.copyToDFS("delta-streamer-config/source_evolved.avsc", dfs, dfsBasePath + "/source_evolved.avsc"); | ||
UtilitiesTestBase.Helpers.copyToDFS("delta-streamer-config/source-flattened.avsc", dfs, dfsBasePath + "/source-flattened.avsc"); | ||
UtilitiesTestBase.Helpers.copyToDFS("delta-streamer-config/target.avsc", dfs, dfsBasePath + "/target.avsc"); | ||
UtilitiesTestBase.Helpers.copyToDFS("delta-streamer-config/target-flattened.avsc", dfs, dfsBasePath + "/target-flattened.avsc"); | ||
|
@@ -696,6 +697,44 @@ public void testBulkInsertsAndUpsertsWithBootstrap() throws Exception { | |
assertTrue(fieldNames.containsAll(expectedFieldNames)); | ||
} | ||
|
||
@Test | ||
public void testSchemaEvolution() throws Exception { | ||
String tableBasePath = dfsBasePath + "/test_table_schema_evolution"; | ||
|
||
// Insert data produced with Schema A, pass Schema A | ||
HoodieDeltaStreamer.Config cfg = TestHelpers.makeConfig(tableBasePath, WriteOperationType.INSERT, Collections.singletonList(TestIdentityTransformer.class.getName())); | ||
cfg.configs.add("hoodie.deltastreamer.schemaprovider.source.schema.file=" + dfsBasePath + "/source.avsc"); | ||
cfg.configs.add("hoodie.deltastreamer.schemaprovider.target.schema.file=" + dfsBasePath + "/source.avsc"); | ||
new HoodieDeltaStreamer(cfg, jsc).sync(); | ||
TestHelpers.assertRecordCount(1000, tableBasePath + "/*/*.parquet", sqlContext); | ||
TestHelpers.assertCommitMetadata("00000", tableBasePath, dfs, 1); | ||
|
||
// Upsert data produced with Schema B, pass Schema B | ||
cfg = TestHelpers.makeConfig(tableBasePath, WriteOperationType.UPSERT, Collections.singletonList(TripsWithEvolvedOptionalFieldTransformer.class.getName())); | ||
cfg.configs.add("hoodie.deltastreamer.schemaprovider.source.schema.file=" + dfsBasePath + "/source_evolved.avsc"); | ||
cfg.configs.add("hoodie.deltastreamer.schemaprovider.target.schema.file=" + dfsBasePath + "/source_evolved.avsc"); | ||
new HoodieDeltaStreamer(cfg, jsc).sync(); | ||
TestHelpers.assertRecordCount(1450, tableBasePath + "/*/*.parquet", sqlContext); | ||
TestHelpers.assertCommitMetadata("00001", tableBasePath, dfs, 2); | ||
List<Row> counts = TestHelpers.countsPerCommit(tableBasePath + "/*/*.parquet", sqlContext); | ||
assertEquals(1450, counts.stream().mapToLong(entry -> entry.getLong(1)).sum()); | ||
|
||
sqlContext.read().format("org.apache.hudi").load(tableBasePath + "/*/*.parquet").createOrReplaceTempView("tmp_trips"); | ||
long recordCount = | ||
sqlContext.sparkSession().sql("select * from tmp_trips where evoluted_optional_union_field is not NULL").count(); | ||
assertEquals(950, recordCount); | ||
|
||
// Upsert data produced with Schema A, pass Schema B | ||
cfg = TestHelpers.makeConfig(tableBasePath, WriteOperationType.UPSERT, Collections.singletonList(TestIdentityTransformer.class.getName())); | ||
cfg.configs.add("hoodie.deltastreamer.schemaprovider.source.schema.file=" + dfsBasePath + "/source_evolved.avsc"); | ||
cfg.configs.add("hoodie.deltastreamer.schemaprovider.target.schema.file=" + dfsBasePath + "/source_evolved.avsc"); | ||
new HoodieDeltaStreamer(cfg, jsc).sync(); | ||
TestHelpers.assertRecordCount(1900, tableBasePath + "/*/*.parquet", sqlContext); | ||
TestHelpers.assertCommitMetadata("00002", tableBasePath, dfs, 3); | ||
counts = TestHelpers.countsPerCommit(tableBasePath + "/*/*.parquet", sqlContext); | ||
assertEquals(1900, counts.stream().mapToLong(entry -> entry.getLong(1)).sum()); | ||
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. similarly, would be good to add a comment here as to how 1900 is expected. |
||
} | ||
|
||
@Test | ||
public void testUpsertsCOWContinuousMode() throws Exception { | ||
testUpsertsContinuousMode(HoodieTableType.COPY_ON_WRITE, "continuous_cow"); | ||
|
@@ -1659,4 +1698,16 @@ public Dataset<Row> apply(JavaSparkContext jsc, SparkSession sparkSession, Datas | |
return rowDataset; | ||
} | ||
} | ||
|
||
/** | ||
* Add new field evoluted_optional_union_field with value of the field rider. | ||
*/ | ||
public static class TripsWithEvolvedOptionalFieldTransformer implements Transformer { | ||
|
||
@Override | ||
public Dataset<Row> apply(JavaSparkContext jsc, SparkSession sparkSession, Dataset<Row> rowDataset, | ||
TypedProperties properties) { | ||
return rowDataset.withColumn("evoluted_optional_union_field", functions.col("rider")); | ||
} | ||
} | ||
} |
This file contains 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.
can you add a comment here as to how 1450 is expected.
// batch 1: 1000 inserts
// batch2: total records to be generated 1000. 50% inserts. 50% updates. 50 count deletes. and so, 500 new inserts. 450 updates from previous batch. 50 deletes. in all, 1450.