-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-1936] Introduce a optional property for conditional upsert #3035
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cb94894
custom upsert
897c0ac
Test class
de45962
reverting to the test class
d337b1c
Merge pull request #1 from fanaticjo/customupsertlogic
fanaticjo cbe2e20
Merge pull request #2 from fanaticjo/master
fanaticjo f07f214
reverting to the hudi default checktyle
26dadb6
reverting to the hudi default checktyle
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
107 changes: 107 additions & 0 deletions
107
hudi-common/src/main/java/org/apache/hudi/common/model/OverwriteWithCustomAvroPayload.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,107 @@ | ||
| /* | ||
| * 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.hudi.common.util.Option; | ||
| import org.apache.hudi.exception.ColumnNotFoundException; | ||
| import org.apache.hudi.exception.UpdateKeyNotFoundException; | ||
| import org.apache.hudi.exception.WriteOperationException; | ||
|
|
||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.generic.GenericRecord; | ||
| import org.apache.avro.generic.IndexedRecord; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Properties; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * subclass of OverwriteWithLatestAvroPayload used for delta streamer. | ||
| * | ||
| * <ol> | ||
| * <li> combineAndGetUpdateValue - Accepts the column names to be updated; | ||
| * <li> splitKeys - Split keys based upon keys; | ||
| * </ol> | ||
| */ | ||
| public class OverwriteWithCustomAvroPayload extends OverwriteWithLatestAvroPayload { | ||
|
|
||
| public OverwriteWithCustomAvroPayload(GenericRecord record, Comparable orderingVal) { | ||
| super(record, orderingVal); | ||
| } | ||
|
|
||
| /** | ||
| * split keys over. | ||
| */ | ||
| public List<String> splitKeys(String keys) throws UpdateKeyNotFoundException { | ||
| if (keys == null) { | ||
| throw new UpdateKeyNotFoundException("keys cannot be null"); | ||
| } else if (keys.equals("")) { | ||
| throw new UpdateKeyNotFoundException("keys cannot be blank"); | ||
| } else { | ||
| return Arrays.stream(keys.split(",")).collect(Collectors.toList()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * check column exi. | ||
| */ | ||
| public boolean checkColumnExists(List<String> keys, Schema schema) { | ||
| List<Schema.Field> field = schema.getFields(); | ||
| List<Schema.Field> common = new ArrayList<>(); | ||
| for (Schema.Field columns : field) { | ||
| if (keys.contains(columns.name())) { | ||
| common.add(columns); | ||
| } | ||
| } | ||
| return common.size() == keys.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public Option<IndexedRecord> combineAndGetUpdateValue(IndexedRecord currentValue, Schema schema, Properties properties) | ||
| throws WriteOperationException, IOException, ColumnNotFoundException, UpdateKeyNotFoundException { | ||
|
|
||
| if (!properties.getProperty("hoodie.datasource.write.operation").equals("upsert")) { | ||
| throw new WriteOperationException("write should be upsert"); | ||
| } | ||
|
|
||
| Option<IndexedRecord> recordOption = getInsertValue(schema); | ||
|
|
||
| if (!recordOption.isPresent()) { | ||
| return Option.empty(); | ||
| } | ||
|
|
||
| GenericRecord existingRecord = (GenericRecord) currentValue; | ||
| GenericRecord incomingRecord = (GenericRecord) recordOption.get(); | ||
| List<String> keys = splitKeys(properties.getProperty("hoodie.update.keys")); | ||
fanaticjo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (checkColumnExists(keys, schema)) { | ||
| for (String key : keys) { | ||
| Object value = incomingRecord.get(key); | ||
| existingRecord.put(key, value); | ||
| } | ||
| return Option.of(existingRecord); | ||
| } else { | ||
| throw new ColumnNotFoundException("Update key not present please check the names"); | ||
| } | ||
| } | ||
|
|
||
| } | ||
25 changes: 25 additions & 0 deletions
25
hudi-common/src/main/java/org/apache/hudi/exception/ColumnNotFoundException.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,25 @@ | ||
| /* | ||
| * 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.exception; | ||
|
|
||
| public class ColumnNotFoundException extends HoodieIOException { | ||
| public ColumnNotFoundException(String message) { | ||
| super(message); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
hudi-common/src/main/java/org/apache/hudi/exception/UpdateKeyNotFoundException.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,25 @@ | ||
| /* | ||
| * 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.exception; | ||
|
|
||
| public class UpdateKeyNotFoundException extends HoodieIOException { | ||
| public UpdateKeyNotFoundException(String message) { | ||
| super(message); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
hudi-common/src/main/java/org/apache/hudi/exception/WriteOperationException.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,25 @@ | ||
| /* | ||
| * 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.exception; | ||
|
|
||
| public class WriteOperationException extends HoodieException { | ||
| public WriteOperationException(String message) { | ||
| super(message); | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
...common/src/test/java/org/apache/hudi/common/model/TestOverwriteWithCustomAvroPayload.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,62 @@ | ||
| /* | ||
| * 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.GenericData; | ||
| import org.apache.avro.generic.GenericRecord; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * Unit tests {@link OverwriteWithCustomAvroPayload}. | ||
| */ | ||
| public class TestOverwriteWithCustomAvroPayload { | ||
|
|
||
| private Schema schema; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() throws Exception { | ||
| schema = Schema.createRecord(Arrays.asList( | ||
| new Schema.Field("id", Schema.create(Schema.Type.STRING), "", null), | ||
| new Schema.Field("partition", Schema.create(Schema.Type.STRING), "", null), | ||
| new Schema.Field("ts", Schema.create(Schema.Type.LONG), "", null), | ||
| new Schema.Field("_hoodie_is_deleted", Schema.create(Schema.Type.BOOLEAN), "", false) | ||
| )); | ||
| } | ||
|
|
||
| @Test | ||
| public void testsplitKeys() throws IOException { | ||
| GenericRecord record1 = new GenericData.Record(schema); | ||
| record1.put("id", "1"); | ||
| record1.put("partition", "partition0"); | ||
| record1.put("ts", 0L); | ||
| record1.put("_hoodie_is_deleted", false); | ||
| OverwriteWithCustomAvroPayload payload1 = new OverwriteWithCustomAvroPayload(record1, 1); | ||
| assertEquals(payload1.splitKeys("id,ts").size(), Arrays.asList("id", "ts").size()); | ||
| assertEquals(payload1.splitKeys("id,ts"), Arrays.asList("id", "ts")); | ||
| } | ||
|
|
||
| } | ||
|
|
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.
we might have to check for delete record as well.
something like
Please do check OverwriteWithLatestAvroPayload impl
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.
yes user can set different values for different batches for cow it working , mor will test
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 class should only be considered only for upserts , so why delete is required ?
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.
i see one thing as a blocker is if a new column is introduced it makes it as null , any idea how to tackle this ? one idea i have is check what schema is mismatch and add that in the properties only in that new column will get values or is there any hudi way for that
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.
@nsivabalan any updates ?