-
Notifications
You must be signed in to change notification settings - Fork 99
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
SNOW-1737840: Adapt record mapping in RecordService #969
Merged
sfc-gh-wtrefon
merged 14 commits into
master
from
wtrefon/SNOW-1737840-record-service-iceberg-mapping
Oct 25, 2024
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d079526
Refactor RecordService
sfc-gh-wtrefon d6bec27
Reuse mapper
sfc-gh-wtrefon 6ef1ef7
Add mapping for iceberg
sfc-gh-wtrefon 7a3c56d
Fix mapping of headers
sfc-gh-wtrefon a9af57b
Handle null scenario
sfc-gh-wtrefon e30c7e0
Add tests
sfc-gh-wtrefon ea5505b
Add skip metadata test
sfc-gh-wtrefon 92beb53
Merge branch 'master' into wtrefon/SNOW-1737840-record-service-iceber…
sfc-gh-wtrefon 15f2062
Add headers
sfc-gh-wtrefon ba08b7c
Uncomment int64
sfc-gh-wtrefon 6197a2e
Add complex object test
sfc-gh-wtrefon c335ca4
Fix test
sfc-gh-wtrefon 47f1ddd
CR changes
sfc-gh-wtrefon 89b11e6
Move schematizationEnabled
sfc-gh-wtrefon 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
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
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
104 changes: 104 additions & 0 deletions
104
src/main/java/com/snowflake/kafka/connector/records/IcebergTableStreamingRecordMapper.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,104 @@ | ||
package com.snowflake.kafka.connector.records; | ||
|
||
import static com.snowflake.kafka.connector.Utils.TABLE_COLUMN_CONTENT; | ||
import static com.snowflake.kafka.connector.Utils.TABLE_COLUMN_METADATA; | ||
import static com.snowflake.kafka.connector.records.RecordService.*; | ||
|
||
import com.snowflake.kafka.connector.Utils; | ||
import java.util.AbstractMap; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import net.snowflake.client.jdbc.internal.fasterxml.jackson.core.JsonProcessingException; | ||
import net.snowflake.client.jdbc.internal.fasterxml.jackson.core.type.TypeReference; | ||
import net.snowflake.client.jdbc.internal.fasterxml.jackson.databind.JsonNode; | ||
import net.snowflake.client.jdbc.internal.fasterxml.jackson.databind.ObjectMapper; | ||
import net.snowflake.client.jdbc.internal.fasterxml.jackson.databind.node.NumericNode; | ||
|
||
class IcebergTableStreamingRecordMapper implements StreamingRecordMapper { | ||
private final ObjectMapper mapper; | ||
|
||
private static final TypeReference<Map<String, Object>> OBJECTS_MAP_TYPE_REFERENCE = | ||
new TypeReference<Map<String, Object>>() {}; | ||
|
||
public IcebergTableStreamingRecordMapper(ObjectMapper objectMapper) { | ||
this.mapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> processSnowflakeRecord( | ||
SnowflakeTableRow row, boolean schematizationEnabled, boolean includeAllMetadata) | ||
throws JsonProcessingException { | ||
final Map<String, Object> streamingIngestRow = new HashMap<>(); | ||
for (JsonNode node : row.getContent().getData()) { | ||
if (schematizationEnabled) { | ||
streamingIngestRow.putAll(getMapForSchematization(node)); | ||
} else { | ||
streamingIngestRow.put(TABLE_COLUMN_CONTENT, getMapForNoSchematization(node)); | ||
} | ||
} | ||
if (includeAllMetadata) { | ||
streamingIngestRow.put(TABLE_COLUMN_METADATA, getMapForMetadata(row.getMetadata())); | ||
} | ||
return streamingIngestRow; | ||
} | ||
|
||
private Map<String, Object> getMapForNoSchematization(JsonNode node) { | ||
return mapper.convertValue(node, OBJECTS_MAP_TYPE_REFERENCE); | ||
} | ||
|
||
private Map<String, Object> getMapForSchematization(JsonNode node) { | ||
// we need to quote the keys on the first level of the map as they are column names in the table | ||
// the rest must stay as is as the nested objects are not column names but fields name with case | ||
// sensitivity | ||
return mapper.convertValue(node, OBJECTS_MAP_TYPE_REFERENCE).entrySet().stream() | ||
.map( | ||
entry -> | ||
new AbstractMap.SimpleEntry<>( | ||
Utils.quoteNameIfNeeded(entry.getKey()), entry.getValue())) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
private Map<String, Object> getMapForMetadata(JsonNode metadataNode) | ||
throws JsonProcessingException { | ||
Map<String, Object> values = mapper.convertValue(metadataNode, OBJECTS_MAP_TYPE_REFERENCE); | ||
// we don't want headers to be serialized as Map<String, Object> so we overwrite it as | ||
// Map<String, String> | ||
Map<String, String> headers = convertHeaders(metadataNode.findValue(HEADERS)); | ||
values.put(HEADERS, headers); | ||
return values; | ||
} | ||
|
||
private Map<String, String> convertHeaders(JsonNode headersNode) throws JsonProcessingException { | ||
final Map<String, String> headers = new HashMap<>(); | ||
|
||
if (headersNode == null || headersNode.isNull() || headersNode.isEmpty()) { | ||
return headers; | ||
} | ||
|
||
Iterator<String> fields = headersNode.fieldNames(); | ||
while (fields.hasNext()) { | ||
String key = fields.next(); | ||
JsonNode valueNode = headersNode.get(key); | ||
String value; | ||
if (valueNode.isTextual()) { | ||
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. nit: extract |
||
value = valueNode.textValue(); | ||
} else if (valueNode.isNull()) { | ||
value = null; | ||
} else { | ||
value = writeValueAsStringOrNan(valueNode); | ||
} | ||
headers.put(key, value); | ||
} | ||
return headers; | ||
} | ||
|
||
private String writeValueAsStringOrNan(JsonNode columnNode) throws JsonProcessingException { | ||
if (columnNode instanceof NumericNode && ((NumericNode) columnNode).isNaN()) { | ||
return "NaN"; | ||
} else { | ||
return mapper.writeValueAsString(columnNode); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
nit: maybe just
boolean includeMetadata
?