Created
November 13, 2022 04:38
-
-
Save kanguki/e8d7765a1fd8021139bb13c3132436f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/go-mysql-org/go-mysql/canal" | |
"github.com/siddontang/go-log/log" | |
) | |
type MyEventHandler struct { | |
canal.DummyEventHandler | |
} | |
func (h *MyEventHandler) OnRow(e *canal.RowsEvent) error { | |
log.Infof("%s %v\n", e.Action, e.Rows) | |
return nil | |
} | |
func (h *MyEventHandler) String() string { | |
return "MyEventHandler" | |
} | |
func main() { | |
cfg := canal.NewDefaultConfig() | |
cfg.Addr = "127.0.0.1:3306" | |
cfg.User = "root" | |
cfg.Password = "xxx" | |
// We only care table canal_test in test db | |
cfg.Dump.TableDB = "test" | |
// cfg.Dump.Tables = []string{"canal_test"} | |
c, err := canal.NewCanal(cfg) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Register a handler to handle RowsEvent | |
c.SetEventHandler(&MyEventHandler{}) | |
// Start canal | |
latestPos, err := c.GetMasterPos() | |
if err != nil { | |
panic(err) | |
} | |
c.RunFrom(latestPos) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment