-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
130 lines (119 loc) · 3.2 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"strconv"
"time"
cdc "github.com/Trendyol/go-pq-cdc-elasticsearch"
"github.com/Trendyol/go-pq-cdc-elasticsearch/config"
"github.com/Trendyol/go-pq-cdc-elasticsearch/elasticsearch"
cdcconfig "github.com/Trendyol/go-pq-cdc/config"
"github.com/Trendyol/go-pq-cdc/pq/publication"
"github.com/Trendyol/go-pq-cdc/pq/slot"
)
/*
psql "postgres://es_cdc_user:[email protected]/es_cdc_db?replication=database"
CREATE TABLE users (
id serial PRIMARY KEY,
name text NOT NULL,
created_on timestamptz
);
CREATE TABLE books (
id serial PRIMARY KEY,
name text NOT NULL,
created_on timestamptz
);
INSERT INTO users (name)
SELECT
'Oyleli' || i
FROM generate_series(1, 100) AS i;
INSERT INTO books (name)
SELECT
'Oyleli' || i
FROM generate_series(1, 100) AS i;
*/
func main() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
ctx := context.TODO()
cfg := config.Config{
CDC: cdcconfig.Config{
Host: "127.0.0.1",
Username: "es_cdc_user",
Password: "es_cdc_pass",
Database: "es_cdc_db",
DebugMode: false,
Publication: publication.Config{
CreateIfNotExists: true,
Name: "es_cdc_publication",
Operations: publication.Operations{
publication.OperationInsert,
publication.OperationDelete,
publication.OperationTruncate,
publication.OperationUpdate,
},
Tables: publication.Tables{
publication.Table{
Name: "users",
ReplicaIdentity: publication.ReplicaIdentityFull,
},
publication.Table{
Name: "books",
ReplicaIdentity: publication.ReplicaIdentityFull,
},
},
},
Slot: slot.Config{
CreateIfNotExists: true,
Name: "es_cdc_slot",
SlotActivityCheckerInterval: 3000,
},
Metric: cdcconfig.MetricConfig{
Port: 8081,
},
},
Elasticsearch: config.Elasticsearch{
Username: "elastic",
Password: "es_cdc_es_pass",
BatchSizeLimit: 10000,
BatchTickerDuration: time.Millisecond * 100,
TableIndexMapping: map[string]string{
"public.users": "users",
"public.books": "books",
},
TypeName: "_doc",
URLs: []string{"http://127.0.0.1:9200"},
},
}
connector, err := cdc.NewConnector(ctx, cfg, Handler)
if err != nil {
slog.Error("new connector", "error", err)
os.Exit(1)
}
defer connector.Close()
connector.Start(ctx)
}
func Handler(msg cdc.Message) []elasticsearch.Action {
slog.Info("message received", "type", msg.Type, "msg", fmt.Sprintf("%#v", msg))
switch msg.Type {
case cdc.InsertMessage:
b, _ := json.Marshal(msg.NewData)
return []elasticsearch.Action{
elasticsearch.NewIndexAction([]byte(strconv.Itoa(int(msg.NewData["id"].(int32)))), b, nil),
}
case cdc.DeleteMessage:
return []elasticsearch.Action{
elasticsearch.NewDeleteAction([]byte(strconv.Itoa(int(msg.OldData["id"].(int32)))), nil),
}
case cdc.UpdateMessage:
msg.NewData["old_name"] = msg.OldData["name"]
b, _ := json.Marshal(msg.NewData)
return []elasticsearch.Action{
elasticsearch.NewIndexAction([]byte(strconv.Itoa(int(msg.NewData["id"].(int32)))), b, nil),
}
default:
return nil
}
}