-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka.go
46 lines (39 loc) · 804 Bytes
/
kafka.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"context"
"encoding/json"
"log"
"github.com/mmcdole/gofeed"
"github.com/segmentio/kafka-go"
)
type KafkaOutput struct {
Broker string
Topic string
}
func (out KafkaOutput) Write(feed *gofeed.Feed, item gofeed.Item, identifier string) error {
w := &kafka.Writer{
Addr: kafka.TCP(out.Broker),
Topic: out.Topic,
Balancer: &kafka.LeastBytes{},
}
data, err := json.Marshal(item)
if err != nil {
log.Fatal("Failed to marshal", item, err)
return err
}
err = w.WriteMessages(context.Background(),
kafka.Message{
Key: []byte("Item"),
Value: data,
},
)
if err != nil {
log.Fatal("failed to write messages:", err)
return err
}
if err := w.Close(); err != nil {
log.Fatal("failed to close writer:", err)
return err
}
return nil
}