-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-12170: Fix for Connect Cast SMT to correctly transform a Byte array into a string #9950
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
Changes from 6 commits
dba4a40
fa801de
4213125
4e74475
3028511
411ceb3
99d26f4
e772b37
ff3239c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import org.apache.kafka.common.cache.SynchronizedCache; | ||
| import org.apache.kafka.common.config.ConfigDef; | ||
| import org.apache.kafka.common.config.ConfigException; | ||
| import org.apache.kafka.common.utils.Utils; | ||
| import org.apache.kafka.connect.connector.ConnectRecord; | ||
| import org.apache.kafka.connect.data.ConnectSchema; | ||
| import org.apache.kafka.connect.data.Date; | ||
|
|
@@ -39,6 +40,7 @@ | |
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.EnumSet; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
|
|
@@ -364,11 +366,25 @@ private static String castToString(Object value) { | |
| if (value instanceof java.util.Date) { | ||
| java.util.Date dateValue = (java.util.Date) value; | ||
| return Values.dateFormatFor(dateValue).format(dateValue); | ||
| } else if (value instanceof ByteBuffer) { | ||
| ByteBuffer byteBuffer = (ByteBuffer) value; | ||
| return castByteArrayToString(Utils.readBytes(byteBuffer)); | ||
| } else if (value instanceof byte[]) { | ||
| byte[] rawBytes = (byte[]) value; | ||
| return castByteArrayToString(rawBytes); | ||
| } else { | ||
| return value.toString(); | ||
| } | ||
| } | ||
|
|
||
| private static String castByteArrayToString(byte[] array) { | ||
| StringBuilder sbuf = new StringBuilder(); | ||
| for (byte b : array) { | ||
| sbuf.append(String.format("%02X", b)); | ||
|
Contributor
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. Also, I'm a bit curious. Why are we selecting hex here and not base64 for example? @rhauch wdyt about the representation here?
Contributor
Author
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. The reason I suggested Hex representation was that in the use case I encountered - database PK field encoded as BINARY retrieved via CDC Source Connector - the representation in the SQL CLI was Hex, so choosing the same format made it easy to compare source and target. I would suggest that base64 encoding would validate its own dedicated transformer, with an ability to choose padding, for example
Contributor
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. As noted in my previous comment, I agree with @kkonstantine that base64 would be preferable. Doing that would align better with the existing If we want to add support for multiple encodings, we would need to have a KIP since it would likely mean changing the SMT configuration.
Contributor
Author
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. If base64 is the better choice we shall go with that. Would we need to adjust the JIRA ticket to reflect this? I just ran a little test in the JShell: jshell> byte[] byteArray = new byte[] {(byte) 0xFE, (byte) 0xDC, (byte) 0xBA, (byte) 0x98, 0x76, 0x54, 0x32, 0x10}; jshell> Base64.getEncoder().encode(byteArray) jshell> Base64.getEncoder().encodeToString(byteArray) jshell> Base64.getEncoder().withoutPadding().encodeToString(byteArray) Would you suggest using padding or not?
Contributor
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. Connect's
Contributor
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. Yes, I agree. |
||
| } | ||
| return sbuf.toString(); | ||
| } | ||
|
|
||
| protected abstract Schema operatingSchema(R record); | ||
|
|
||
| protected abstract Object operatingValue(R record); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.