elasticstream/source/elastic/open.go

44 lines
737 B
Go

package elastic
import (
"context"
"elasticstream/opencdc"
"elasticstream/source"
"github.com/elastic/go-elasticsearch/v8"
)
func (c *Client) Open(ctx context.Context, positions []source.Position) error {
// 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
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
}
}
c.offsets[index] = offset
NewWorker(c, index, offset)
}
return nil
}