-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
92 lines (69 loc) · 1.78 KB
/
main.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
package main
import (
"bytes"
"fmt"
"github.com/confluentinc/confluent-kafka-go/kafka"
"io/ioutil"
"net/http"
)
func CreateKafkaConsumer() *kafka.Consumer {
consumer, err := kafka.NewConsumer(&kafka.ConfigMap{
"bootstrap.servers": "localhost",
"group.id": "myGroup",
"auto.offset.reset": "earliest",
})
if err != nil {
panic(err)
}
return consumer
}
func SubscribeTopic(consumer *kafka.Consumer) {
consumer.Subscribe("postgres.public.Product",nil)
fmt.Println("Subscribed to product topic")
}
func ReadTopicMessages(consumer *kafka.Consumer) string {
var message string
for {
msg, err := consumer.ReadMessage(-1)
if err == nil {
fmt.Printf("Message on %s: %s\n", msg.TopicPartition, string(msg.Value))
//message = message + string(msg.Value)
} else {
// The client will automatically try to recover from all errors.
fmt.Printf("Consumer error: %v (%v)\n", err, msg)
}
}
CloseConsumer(consumer)
return message
}
func CloseConsumer(consumer *kafka.Consumer){
consumer.Close()
}
func RegisterConnector() *http.Response {
plan, _ := ioutil.ReadFile("./connectors/debezium-connector.json")
response, err := http.Post("http://localhost:8083/connectors/","application/json",bytes.NewBuffer(plan))
if err != nil{
panic(err)
}
return response
}
func CheckConnector() {
response, err := http.Get("http://localhost:8083/connectors/product_connector")
defer response.Body.Close()
if err != nil{
panic(err)
}
if response.StatusCode != 200 {
RegisterConnector()
}
// If you want to show response, uncomment following lines
//body, _ := ioutil.ReadAll(response.Body)
//fmt.Println(string(body))
}
func main() {
consumer := CreateKafkaConsumer()
CheckConnector()
SubscribeTopic(consumer)
ReadTopicMessages(consumer)
fmt.Scan()
}