esgo2dump/internal/es/client.go
2024-03-22 18:05:47 +08:00

66 lines
1.3 KiB
Go

package es
import (
"esgo2dump/internal/interfaces"
"fmt"
elastic "github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/sirupsen/logrus"
"net/url"
)
func NewClient(url *url.URL) (interfaces.DumpIO, error) {
var (
err error
endpoint = fmt.Sprintf("%s://%s:%s", url.Scheme, url.Host, url.Port())
c *elastic.Client
infoResp *esapi.Response
)
logrus.Debugf("es.NewClient: endpoint=%s", endpoint)
if c, err = elastic.NewClient(
elastic.Config{
Addresses: []string{endpoint},
Username: "",
Password: "",
CACert: nil,
RetryOnStatus: []int{429},
MaxRetries: 3,
RetryBackoff: nil,
},
); err != nil {
logrus.Debugf("es.NewClient: elastic new client with endpont=%s err=%v", endpoint, err)
return nil, err
}
if infoResp, err = c.Info(); err != nil {
return nil, err
}
if infoResp.StatusCode != 200 {
return nil, fmt.Errorf("info es status=%d", infoResp.StatusCode)
}
return &client{c: c}, nil
}
type client struct {
c *elastic.Client
}
func (c *client) Close() error {
return nil
}
func (c client) Write(docs []map[string]any) (int, error) {
//TODO implement me
panic("implement me")
}
func (c client) Read(i int) ([]map[string]any, error) {
//TODO implement me
panic("implement me")
}