Skip to content

Commit

Permalink
Improve deduplicateBatch logic (#168)
Browse files Browse the repository at this point in the history
* Improve deduplicateBatch logic

* Improve deduplicateBatch logic

* Improve deduplicateBatch logic
  • Loading branch information
ismailsimsek committed Jan 29, 2023
1 parent 180620a commit 5a0d4cb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ on:
- '.idea/**'
- '.run/**'

env:
SPARK_LOCAL_IP: 127.0.0.1

jobs:
build:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,20 @@ public class IcebergTableOperator {

private List<IcebergChangeEvent> deduplicateBatch(List<IcebergChangeEvent> events) {

ConcurrentHashMap<JsonNode, IcebergChangeEvent> icebergRecordsmap = new ConcurrentHashMap<>();

for (IcebergChangeEvent e : events) {

// deduplicate using key(PK) @TODO improve using map.merge
if (icebergRecordsmap.containsKey(e.key())) {

// replace it if it's new
if (this.compareByTsThenOp(icebergRecordsmap.get(e.key()).value(), e.value()) <= 0) {
icebergRecordsmap.put(e.key(), e);
}

} else {
icebergRecordsmap.put(e.key(), e);
}

}
return new ArrayList<>(icebergRecordsmap.values());
ConcurrentHashMap<JsonNode, IcebergChangeEvent> deduplicatedEvents = new ConcurrentHashMap<>();

events.forEach(e ->
// deduplicate using key(PK)
deduplicatedEvents.merge(e.key(), e, (oldValue, newValue) -> {
if (this.compareByTsThenOp(oldValue.value(), newValue.value()) <= 0) {
return newValue;
} else {
return oldValue;
}
})
);

return new ArrayList<>(deduplicatedEvents.values());
}

/**
Expand Down

0 comments on commit 5a0d4cb

Please sign in to comment.