/
database_kafka_subscribe.go
107 lines (96 loc) · 2.24 KB
/
database_kafka_subscribe.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package elem
import (
"os"
"os/signal"
"github.com/Bitspark/slang/pkg/core"
"github.com/Shopify/sarama"
"github.com/google/uuid"
)
var databaseKafkaSubscribeId = uuid.MustParse("b6cb78ca-bbfd-475e-a11f-3593ce295e3c")
var databaseKafkaSubscribeCfg = &builtinConfig{
safe: true,
blueprint: core.Blueprint{
Id: databaseKafkaSubscribeId,
Meta: core.BlueprintMetaDef{
Name: "Kafka subscribe",
ShortDescription: "subscribes at a Kafka topic",
Icon: "",
Tags: []string{"database", "kafka"},
DocURL: "https://bitspark.de/slang/docs/operator/kafka-subscribe",
},
ServiceDefs: map[string]*core.ServiceDef{
core.MAIN_SERVICE: {
In: core.TypeDef{
Type: "trigger",
},
Out: core.TypeDef{
Type: "stream",
Stream: &core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"key": {
Type: "string",
},
"value": {
Type: "binary",
},
},
},
},
},
},
DelegateDefs: map[string]*core.DelegateDef{},
PropertyDefs: map[string]*core.TypeDef{
"brokers": {
Type: "stream",
Stream: &core.TypeDef{
Type: "string",
},
},
"topic": {
Type: "string",
},
},
},
opFunc: func(op *core.Operator) {
topic := op.Property("topic").(string)
brokers := []string{}
for _, broker := range op.Property("brokers").([]interface{}) {
brokers = append(brokers, broker.(string))
}
in := op.Main().In()
out := op.Main().Out()
outKeyStream := out.Stream().Map("key")
outValueStream := out.Stream().Map("value")
for {
i := in.Pull()
if core.IsMarker(i) {
out.Push(i)
continue
}
config := sarama.NewConfig()
consumer, err := sarama.NewConsumer(brokers, config)
if err != nil {
panic(err)
}
partitionConsumer, err := consumer.ConsumePartition(topic, 0, sarama.OffsetNewest)
if err != nil {
panic(err)
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
out.PushBOS()
ConsumerLoop:
for {
select {
case msg := <-partitionConsumer.Messages():
outKeyStream.Push(msg.Key)
outValueStream.Push(core.Binary(msg.Value))
case <-signals:
break ConsumerLoop
}
}
out.PushEOS()
}
},
}