Skip to content
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

Add connector version in RECORD_METADATA #766

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public class SnowflakeSinkConnectorConfig {
private static final String SNOWFLAKE_METADATA_FLAGS = "Snowflake Metadata Flags";
public static final String SNOWFLAKE_METADATA_CREATETIME = "snowflake.metadata.createtime";
public static final String SNOWFLAKE_METADATA_TOPIC = "snowflake.metadata.topic";
public static final String SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION =
"snowflake.metadata.sf.connector.version";
public static final String SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION_DEFAULT = "false";
public static final String SNOWFLAKE_METADATA_OFFSET_AND_PARTITION =
"snowflake.metadata.offset.and.partition";
public static final String SNOWFLAKE_METADATA_ALL = "snowflake.metadata.all";
Expand Down Expand Up @@ -486,6 +489,17 @@ static ConfigDef newConfigDef() {
2,
ConfigDef.Width.NONE,
SNOWFLAKE_METADATA_TOPIC)
.define(
SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION,
Type.BOOLEAN,
SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION_DEFAULT,
Importance.LOW,
"Flag to control whether Snowflake Connector version is collected in snowflake"
+ " metadata",
SNOWFLAKE_METADATA_FLAGS,
3,
ConfigDef.Width.NONE,
SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION)
.define(
SNOWFLAKE_METADATA_OFFSET_AND_PARTITION,
Type.BOOLEAN,
Expand All @@ -494,7 +508,7 @@ static ConfigDef newConfigDef() {
"Flag to control whether kafka partition and offset are collected in snowflake"
+ " metadata",
SNOWFLAKE_METADATA_FLAGS,
3,
4,
ConfigDef.Width.NONE,
SNOWFLAKE_METADATA_OFFSET_AND_PARTITION)
.define(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class RecordService {
static final String CONTENT = "content";
static final String META = "meta";
static final String SCHEMA_ID = "schema_id";
static final String SF_CONNECTOR_VERSION = "sf_connector_version";
private static final String KEY_SCHEMA_ID = "key_schema_id";
static final String HEADERS = "headers";

Expand Down Expand Up @@ -191,6 +192,9 @@ private SnowflakeTableRow processRecord(SinkRecord record) {
if (metadataConfig.topicFlag) {
meta.put(TOPIC, record.topic());
}
if (metadataConfig.sfConnectorVersionFlag) {
meta.put(SF_CONNECTOR_VERSION, Utils.VERSION);
}
if (metadataConfig.offsetAndPartitionFlag) {
meta.put(OFFSET, record.kafkaOffset());
meta.put(PARTITION, record.kafkaPartition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class SnowflakeMetadataConfig {
final boolean createtimeFlag;
final boolean topicFlag;
final boolean offsetAndPartitionFlag;
final boolean sfConnectorVersionFlag;
final boolean allFlag;

/** initialize with default config */
Expand All @@ -25,6 +26,7 @@ public SnowflakeMetadataConfig(Map<String, String> config) {
// these values are the default values of the configuration
boolean createtime = true;
boolean topic = true;
boolean version = false;
boolean offsetAndPartition = true;
boolean all = true;
if (config.containsKey(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_CREATETIME)
Expand All @@ -39,6 +41,12 @@ public SnowflakeMetadataConfig(Map<String, String> config) {
.equals(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_DEFAULT)) {
topic = false;
}
if (config.containsKey(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION)
&& config
.get(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION)
.equals(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_DEFAULT)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set this to true only when SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION is true right? default is false now!

version = true;
}
if (config.containsKey(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_OFFSET_AND_PARTITION)
&& !config
.get(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_OFFSET_AND_PARTITION)
Expand All @@ -54,6 +62,7 @@ public SnowflakeMetadataConfig(Map<String, String> config) {

createtimeFlag = createtime;
topicFlag = topic;
sfConnectorVersionFlag = version;
offsetAndPartitionFlag = offsetAndPartition;
allFlag = all;
}
Expand All @@ -64,6 +73,8 @@ public String toString() {
+ ", "
+ "topicFlag: "
+ topicFlag
+ "versionFlag: "
+ sfConnectorVersionFlag
+ ", "
+ "offsetAndPartitionFlag: "
+ offsetAndPartitionFlag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ConnectorIT {
SnowflakeSinkConnectorConfig.BUFFER_FLUSH_TIME_SEC,
SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_ALL,
SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_TOPIC,
SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION,
SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_OFFSET_AND_PARTITION,
SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_CREATETIME,
SnowflakeSinkConnectorConfig.TOPICS_TABLES_MAP,
Expand Down Expand Up @@ -78,6 +79,7 @@ static Map<String, String> getErrorConfig() {
config.put(SnowflakeSinkConnectorConfig.BUFFER_FLUSH_TIME_SEC, "-1");
config.put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_ALL, "falseee");
config.put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_TOPIC, "falseee");
config.put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION, "falseee");
config.put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_OFFSET_AND_PARTITION, "falseee");
config.put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_CREATETIME, "falseee");
config.put(SnowflakeSinkConnectorConfig.TOPICS_TABLES_MAP, "jfsja,,");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import net.snowflake.client.jdbc.internal.fasterxml.jackson.core.JsonProcessingException;
import net.snowflake.client.jdbc.internal.fasterxml.jackson.databind.JsonNode;
import net.snowflake.client.jdbc.internal.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.common.record.TimestampType;
Expand Down Expand Up @@ -38,6 +39,12 @@ public class MetaColumnTest {
put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_TOPIC, "false");
}
};
private HashMap<String, String> versionConfig =
new HashMap<String, String>() {
{
put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION, "false");
}
};
private HashMap<String, String> offsetAndPartitionConfig =
new HashMap<String, String>() {
{
Expand Down Expand Up @@ -113,6 +120,18 @@ public void testConfig() throws IOException {
assert result.get(META).has(RecordService.OFFSET);
assert result.get(META).has(RecordService.PARTITION);
assert result.get(META).has(record.timestampType().name);
assert !result.get(META).has(RecordService.SF_CONNECTOR_VERSION);

// test metadata configuration -- add version
metadataConfig = new SnowflakeMetadataConfig(versionConfig);
service.setMetadataConfig(metadataConfig);
result = mapper.readTree(service.getProcessedRecordForSnowpipe(record));
assert result.has(META);
assert !result.get(META).has(RecordService.SF_CONNECTOR_VERSION);
assert result.get(META).has(RecordService.OFFSET);
assert result.get(META).has(RecordService.PARTITION);
assert result.get(META).has(record.timestampType().name);
assert result.get(META).has(RecordService.TOPIC);

// test metadata configuration -- remove offset and partition
metadataConfig = new SnowflakeMetadataConfig(offsetAndPartitionConfig);
Expand All @@ -121,6 +140,7 @@ public void testConfig() throws IOException {
assert result.has(META);
assert !result.get(META).has(RecordService.OFFSET);
assert !result.get(META).has(RecordService.PARTITION);
assert !result.get(META).has(RecordService.SF_CONNECTOR_VERSION);
assert result.get(META).has(record.timestampType().name);
assert result.get(META).has(RecordService.TOPIC);

Expand All @@ -130,6 +150,7 @@ public void testConfig() throws IOException {
result = mapper.readTree(service.getProcessedRecordForSnowpipe(record));
assert result.has(META);
assert !result.get(META).has(record.timestampType().name);
assert !result.get(META).has(RecordService.SF_CONNECTOR_VERSION);
assert result.get(META).has(RecordService.TOPIC);
assert result.get(META).has(RecordService.OFFSET);
assert result.get(META).has(RecordService.PARTITION);
Expand All @@ -143,6 +164,42 @@ public void testConfig() throws IOException {
System.out.println("Config test success");
}

@Test
public void testSfConnectorVersion() throws JsonProcessingException {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is failing


SnowflakeConverter converter = new SnowflakeJsonConverter();
RecordService service = new RecordService();
SchemaAndValue input =
converter.toConnectData(
topic, ("{\"name\":\"test" + "\"}").getBytes(StandardCharsets.UTF_8));
long timestamp = System.currentTimeMillis();

SinkRecord record =
new SinkRecord(
topic,
partition,
Schema.STRING_SCHEMA,
"test",
input.schema(),
input.value(),
0,
timestamp,
TimestampType.CREATE_TIME);

SnowflakeMetadataConfig metadataConfig = new SnowflakeMetadataConfig(topicConfig);
service.setMetadataConfig(metadataConfig);

metadataConfig = new SnowflakeMetadataConfig(versionConfig);
service.setMetadataConfig(metadataConfig);
JsonNode result = mapper.readTree(service.getProcessedRecordForSnowpipe(record));
assert result.has(META);
assert result.get(META).has(RecordService.SF_CONNECTOR_VERSION);
assert result.get(META).has(RecordService.OFFSET);
assert result.get(META).has(RecordService.PARTITION);
assert result.get(META).has(record.timestampType().name);
assert result.get(META).has(RecordService.TOPIC);
}

@Test
public void testTimeStamp() throws IOException {
SnowflakeConverter converter = new SnowflakeJsonConverter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ public void testSchematizationArrayOfObject() throws JsonProcessingException {
service.setEnableSchematization(true);
String value =
"{\"players\":[{\"name\":\"John Doe\",\"age\":30},{\"name\":\"Jane Doe\",\"age\":30}]}";
byte[] valueContents = (value).getBytes(StandardCharsets.UTF_8);
String value2 =
"{\"cricket\":{\"team\":{\"MI\":{\"players\":[{\"name\":\"John"
+ " Doe\",\"age\":30},{\"name\":\"Jane Doe\",\"age\":30}]}}}}";
byte[] valueContents = (value2).getBytes(StandardCharsets.UTF_8);
Comment on lines +267 to +270
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need this?

SchemaAndValue sv = jsonConverter.toConnectData(topic, valueContents);

SinkRecord record =
Expand Down