elasticstream/source/elastic/open.go

44 lines
737 B
Go
Raw Normal View History

2024-10-07 15:25:13 +05:30
package elastic
import (
"context"
2024-10-07 16:26:41 +05:30
"elasticstream/opencdc"
"elasticstream/source"
"github.com/elastic/go-elasticsearch/v8"
2024-10-07 15:25:13 +05:30
)
2024-10-07 16:26:41 +05:30
func (c *Client) Open(ctx context.Context, positions []source.Position) error {
2024-10-07 15:25:13 +05:30
// Open a connection with ElasticSearch
cfg := elasticsearch.Config{
Addresses: []string{
c.cfg.Host,
},
}
var err error
c.es, err = elasticsearch.NewClient(cfg)
if err != nil {
return err
}
// create a buffer channel
2024-10-07 16:26:41 +05:30
c.ch = make(chan opencdc.Data, c.cfg.BatchSize)
for _, index := range c.cfg.Indexes {
offset := 0
for _, position := range positions {
if index == position.Index {
offset = position.Pos
}
2024-10-07 15:25:13 +05:30
}
2024-10-07 16:26:41 +05:30
2024-10-07 15:25:13 +05:30
c.offsets[index] = offset
2024-10-07 16:26:41 +05:30
NewWorker(c, index, offset)
2024-10-07 15:25:13 +05:30
}
return nil
}