47 lines
822 B
Go
47 lines
822 B
Go
package elastic
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
|
|
"elasticstream/opencdc"
|
|
"elasticstream/source"
|
|
|
|
"github.com/elastic/go-elasticsearch/v8"
|
|
)
|
|
|
|
func (c *Client) Open(ctx context.Context, positions []source.Position) error {
|
|
log.Println(">>>> elastic.Open()")
|
|
defer log.Println("<<<< elastic.Open()")
|
|
|
|
// 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
|
|
}
|