1
1
package com .ably .kafka .connect .batch ;
2
2
3
+ import com .ably .kafka .connect .client .BatchSpec ;
3
4
import com .ably .kafka .connect .config .ChannelSinkConnectorConfig ;
4
5
import com .ably .kafka .connect .mapping .MessageConverter ;
5
6
import com .ably .kafka .connect .mapping .RecordMapping ;
6
7
import com .ably .kafka .connect .mapping .RecordMappingException ;
7
- import com .google .common .collect .Lists ;
8
8
import io .ably .lib .types .Message ;
9
9
import org .apache .kafka .connect .sink .ErrantRecordReporter ;
10
10
import org .apache .kafka .connect .sink .SinkRecord ;
11
11
import org .slf4j .Logger ;
12
12
import org .slf4j .LoggerFactory ;
13
13
14
14
import javax .annotation .Nullable ;
15
- import java .util .HashMap ;
15
+ import java .util .Collections ;
16
16
import java .util .List ;
17
- import java .util .Map ;
17
+ import java .util .Objects ;
18
+ import java .util .stream .Collectors ;
18
19
19
- public class MessageGrouper {
20
- private static final Logger logger = LoggerFactory .getLogger (MessageGrouper .class );
20
+ public class MessageTransformer {
21
+ private static final Logger logger = LoggerFactory .getLogger (MessageTransformer .class );
21
22
22
23
private final RecordMapping channelMapping ;
23
24
private final RecordMapping messageNameMapping ;
24
25
private final ChannelSinkConnectorConfig .FailedRecordMappingAction actionOnFailure ;
25
- @ Nullable private final ErrantRecordReporter dlqReporter ;
26
+ @ Nullable
27
+ private final ErrantRecordReporter dlqReporter ;
26
28
27
29
/**
28
- * Construct a new message grouper , for generating Ably BatchSpecs and converting
30
+ * Construct a new message transformer , for generating Ably BatchSpecs and converting
29
31
* records to messages as needed.
30
32
*
31
- * @param channelMapping The RecordMapping to use to generate Ably channel names
33
+ * @param channelMapping The RecordMapping to use to generate Ably channel names
32
34
* @param messageNameMapping The RecordMapping to use to generate Ably Message names
33
- * @param actionOnFailure Action to perform when a message mapping attempt fails
34
- * @param dlqReporter dead letter queue for reporting bad records, or null if not in use
35
+ * @param actionOnFailure Action to perform when a message mapping attempt fails
36
+ * @param dlqReporter dead letter queue for reporting bad records, or null if not in use
35
37
*/
36
- public MessageGrouper (
38
+ public MessageTransformer (
37
39
RecordMapping channelMapping ,
38
40
RecordMapping messageNameMapping ,
39
41
ChannelSinkConnectorConfig .FailedRecordMappingAction actionOnFailure ,
@@ -45,44 +47,32 @@ public MessageGrouper(
45
47
}
46
48
47
49
/**
48
- * Construct a message group for an incoming batch of Kafka records
50
+ * Construct Ably messages for an incoming batch of Kafka records
49
51
*
50
- * @param records Kafka sink records to group by channel and transform to Ably messages
51
- * @return MessageGroup for outgoing message batch
52
+ * @param records Kafka sink records to transform to Ably messages
53
+ * @return List of Kafka sink records with transformed Ably messages
52
54
* @throws FatalBatchProcessingException if a fatal error occurred processing records
53
55
*/
54
- public MessageGroup group (List <SinkRecord > records ) throws FatalBatchProcessingException {
55
- final Map <String , List <MessageGroup .RecordMessagePair >> groupedRecords = new HashMap <>();
56
- for (SinkRecord record : records ) {
56
+ public List <RecordMessagePair > transform (List <SinkRecord > records ) throws FatalBatchProcessingException {
57
+ return records .stream ().map (record -> {
57
58
try {
58
- final String channel = channelMapping .map (record );
59
- final String messageName = messageNameMapping .map (record );
60
- final Message message = MessageConverter .toAblyMessage (messageName , record );
61
-
62
- groupedRecords .compute (channel , (ch , recs ) -> {
63
- final MessageGroup .RecordMessagePair pair = new MessageGroup .RecordMessagePair (record , message );
64
- if (recs != null ) {
65
- recs .add (pair );
66
- return recs ;
67
- } else {
68
- return Lists .newArrayList (pair );
69
- }
70
- });
71
-
59
+ String channel = channelMapping .map (record );
60
+ String messageName = messageNameMapping .map (record );
61
+ Message message = MessageConverter .toAblyMessage (messageName , record );
62
+ return new RecordMessagePair (record , new BatchSpec (Collections .singleton (channel ), Collections .singletonList (message )));
72
63
} catch (RecordMappingException mappingError ) {
73
64
handleMappingFailure (record , mappingError );
65
+ return null ;
74
66
}
75
- }
76
-
77
- return new MessageGroup (groupedRecords );
67
+ }).filter (Objects ::nonNull ).collect (Collectors .toList ());
78
68
}
79
69
80
70
81
71
/**
82
72
* Process a record that we're unable to forward to Ably due to a failed channel or
83
73
* message name mapping according to the configured handling behaviour.
84
74
*
85
- * @param record The SinkRecord we weren't able to map
75
+ * @param record The SinkRecord we weren't able to map
86
76
* @param mappingError The error raised by the RecordMapping
87
77
*/
88
78
private void handleMappingFailure (
0 commit comments