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

KafkaSinkTaskPut method improvement, pasrsingMessageKeyfield type specified as Array. #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
48 changes: 21 additions & 27 deletions src/main/java/com/kakao/connector/kafka/KafkaSinkTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,38 +85,32 @@ public void start(Map<String, String> props) {

@Override
public void put(Collection<SinkRecord> records) {
if (samplingEnabled) {
for (SinkRecord record : records) {
if (record.value() != null) {
try {
String value = record.value().toString();
boolean samplingCondition = Math.random() < samplingPercentage;
if (samplingCondition
&& isFilteringMatch(value)
) {
for (SinkRecord record : records) {
if (record.value() != null) {
try {
String value = record.value().toString();
if (!samplingEnabled && isFilteringMatch(value)) {
sendRecord(value);
} else if (samplingEnabled) {
if (shouldSendRecord(value)) {
sendRecord(value);
}
} catch (Exception e) {
log.error(e.getMessage() + " / " + connectorName, e);
}
}
}
} else {
for (SinkRecord record : records) {
if (record.value() != null) {
try {
String value = record.value().toString();
if (isFilteringMatch(value)) {
sendRecord(value);
}
} catch (Exception e) {
log.error(e.getMessage() + " / " + connectorName, e);
}
} catch (Exception e) {
logError(e);
}
}
}
}

private boolean shouldSendRecord(String value) {
return Math.random() < samplingPercentage && isFilteringMatch(value);
}

private void logError(Exception e) {
log.error(e.getMessage() + " / " + connectorName, e);
}

private void sendRecord(String value) {
ProducerRecord<String, String> sendRecord = getSinkRecord(value);
producer.send(sendRecord, new ProducerCallback());
Expand All @@ -143,21 +137,21 @@ private long parsingTimestamp(String value) {
LocalDateTime dateTime = LocalDateTime.parse(timestampFieldValue, formatter);
return Timestamp.valueOf(dateTime).getTime();
} catch (Exception e) {
log.error(e.getMessage() + " / " + connectorName, e);
logError(e);
return System.currentTimeMillis();
}
}

private String parsingMessageKey(String value) {
try {
StringBuilder messageKey = new StringBuilder();
List<String> fields = Arrays.asList(keyParsingField.split(","));
String[] fields = keyParsingField.split(",");
for (String field : fields) {
messageKey.append(JsonPath.read(value, field).toString());
}
return messageKey.toString();
} catch (Exception e) {
log.error(e.getMessage() + " / " + connectorName, e);
logError(e);
return null;
}
}
Expand Down