@@ -19,6 +19,7 @@ import (
19
19
"encoding/json"
20
20
"fmt"
21
21
"strings"
22
+ "time"
22
23
23
24
kafkago "github.com/segmentio/kafka-go"
24
25
@@ -46,10 +47,17 @@ type sinkConf struct {
46
47
}
47
48
48
49
type kafkaConf struct {
49
- MaxAttempts int `json:"maxAttempts"`
50
- RequiredACKs int `json:"requiredACKs"`
51
- Key string `json:"key"`
52
- Headers interface {} `json:"headers"`
50
+ MaxAttempts int `json:"maxAttempts"`
51
+ RequiredACKs int `json:"requiredACKs"`
52
+ Key string `json:"key"`
53
+ Headers interface {} `json:"headers"`
54
+ WriterConf kafkaWriterConf `json:"writerConf"`
55
+ }
56
+
57
+ type kafkaWriterConf struct {
58
+ BatchSize int `json:"batchSize"`
59
+ BatchTimeout time.Duration `json:"batchTimeout"`
60
+ BatchBytes int64 `json:"batchBytes"`
53
61
}
54
62
55
63
func (m * kafkaSink ) Ping (_ string , props map [string ]interface {}) error {
@@ -92,10 +100,7 @@ func (m *kafkaSink) Configure(props map[string]interface{}) error {
92
100
return err
93
101
}
94
102
m .tlsConfig = tlsConfig
95
- kc := & kafkaConf {
96
- RequiredACKs : - 1 ,
97
- MaxAttempts : 1 ,
98
- }
103
+ kc := getDefaultKafkaConf ()
99
104
if err := cast .MapToStruct (props , kc ); err != nil {
100
105
return err
101
106
}
@@ -122,12 +127,15 @@ func (m *kafkaSink) buildKafkaWriter() error {
122
127
AllowAutoTopicCreation : true ,
123
128
MaxAttempts : m .kc .MaxAttempts ,
124
129
RequiredAcks : kafkago .RequiredAcks (m .kc .RequiredACKs ),
125
- BatchSize : 1 ,
130
+ BatchSize : m .kc .WriterConf .BatchSize ,
131
+ BatchBytes : m .kc .WriterConf .BatchBytes ,
132
+ BatchTimeout : m .kc .WriterConf .BatchTimeout ,
126
133
Transport : & kafkago.Transport {
127
134
SASL : mechanism ,
128
135
TLS : m .tlsConfig ,
129
136
},
130
137
}
138
+ conf .Log .Infof ("kafka writer batchSize:%v, batchTimeout:%v" , m .kc .WriterConf .BatchSize , m .kc .WriterConf .BatchTimeout .String ())
131
139
m .writer = w
132
140
return nil
133
141
}
@@ -310,3 +318,16 @@ func (m *kafkaSink) ping(address string) error {
310
318
defer c .Close ()
311
319
return nil
312
320
}
321
+
322
+ func getDefaultKafkaConf () * kafkaConf {
323
+ c := & kafkaConf {
324
+ RequiredACKs : - 1 ,
325
+ MaxAttempts : 1 ,
326
+ WriterConf : kafkaWriterConf {
327
+ BatchSize : 5000 ,
328
+ BatchTimeout : 200 * time .Millisecond ,
329
+ BatchBytes : 1048576 * 10 , // 10MB
330
+ },
331
+ }
332
+ return c
333
+ }
0 commit comments