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

[ISSUE #150] sink support topic tag #151

Open
wants to merge 1 commit 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 @@ -19,25 +19,30 @@
package org.apache.rocketmq.connect.runtime.config;

import com.google.common.base.Splitter;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.connect.runtime.common.ConnectKeyValue;

public class SinkConnectorConfig extends ConnectConfig {



public static Set<String> parseTopicList(ConnectKeyValue taskConfig) {
String messageQueueStr = taskConfig.getString(RuntimeConfigDefine.CONNECT_TOPICNAME);
if (StringUtils.isBlank(messageQueueStr)) {
return null;
public static Map<String, String> parseTopicList(ConnectKeyValue taskConfig) {
String topicNameAndTagss = taskConfig.getString(RuntimeConfigDefine.CONNECT_TOPICNAME);
List<String> topicTagList = Splitter.on(COMMA).omitEmptyStrings().trimResults().splitToList(topicNameAndTagss);
Map<String, String> topicNameAndTagssMap = new HashMap<>(8);
for (String topicTagPair : topicTagList) {
List<String> topicAndTag = Splitter.on(SEMICOLON).omitEmptyStrings().trimResults().splitToList(topicTagPair);
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO, exchange COMMA and SEMICOLON to maintain upgrade compatible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When there is no tag, if you configure multiple topics, it should be topic1, topic2, topic3. If there is a tag, it should be topic1;tag1, topic2;tag2, topic3;tag3, if so, is there no compatibility problem?

if (topicAndTag.size() == 1) {
topicNameAndTagssMap.put(topicAndTag.get(0), "*");
} else {
topicNameAndTagssMap.put(topicAndTag.get(0), topicAndTag.get(1));
}
}
List<String> topicList = Splitter.on(SEMICOLON).omitEmptyStrings().trimResults().splitToList(messageQueueStr);
return new HashSet<>(topicList);
return topicNameAndTagssMap;
}

public static MessageQueue parseMessageQueueList(String messageQueueStr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public class WorkerSinkTask implements WorkerTask {

private WorkerSinkTaskContext sinkTaskContext;

private Map<String, String> topicTagMap;

private final TransformChain<ConnectRecord> transformChain;

public static final String BROKER_NAME = "brokerName";
Expand Down Expand Up @@ -273,7 +275,8 @@ private void setQueueOffset() {
}

private void registTopics() {
Set<String> topics = SinkConnectorConfig.parseTopicList(taskConfig);
topicTagMap = SinkConnectorConfig.parseTopicList(taskConfig);
Set<String> topics = topicTagMap.keySet();
if (org.apache.commons.collections4.CollectionUtils.isEmpty(topics)) {
throw new ConnectException("sink connector topics config can be null, please check sink connector config info");
}
Expand Down Expand Up @@ -372,7 +375,7 @@ private void pullMessageFromQueues() throws InterruptedException {
final long beginPullMsgTimestamp = System.currentTimeMillis();
try {
shouldStopPullMsg();
pullResult = consumer.pullBlockIfNotFound(entry.getKey(), "*", entry.getValue(), MAX_MESSAGE_NUM);
pullResult = consumer.pullBlockIfNotFound(entry.getKey(), topicTagMap.get(entry.getKey().getTopic()), entry.getValue(), MAX_MESSAGE_NUM);
pullMsgErrorCount = 0;
} catch (MQClientException e) {
pullMsgErrorCount++;
Expand Down