Compare commits
5 Commits
v0.0.3
...
3a462cfce6
Author | SHA1 | Date | |
---|---|---|---|
3a462cfce6 | |||
486ffba682 | |||
f75e31ffbb | |||
f990923dd8 | |||
91ddffe752 |
@ -3,9 +3,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@ -28,6 +26,10 @@ func run(cmd *cobra.Command, args []string) error {
|
|||||||
logrus.SetLevel(logrus.DebugLevel)
|
logrus.SetLevel(logrus.DebugLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if f_limit == 0 || f_limit > 10000 {
|
||||||
|
return fmt.Errorf("invalid limit(1 - 10000)")
|
||||||
|
}
|
||||||
|
|
||||||
switch f_type {
|
switch f_type {
|
||||||
case "data", "mapping", "setting":
|
case "data", "mapping", "setting":
|
||||||
default:
|
default:
|
||||||
@ -84,33 +86,73 @@ func run(cmd *cobra.Command, args []string) error {
|
|||||||
func executeData(ctx context.Context, input, output interfaces.DumpIO) error {
|
func executeData(ctx context.Context, input, output interfaces.DumpIO) error {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
lines []*interfaces.ESSource
|
ch = make(chan []*interfaces.ESSource, 1)
|
||||||
succeed int
|
errCh = make(chan error)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// write goroutine
|
||||||
|
go func(c context.Context) {
|
||||||
|
var (
|
||||||
|
lines []*interfaces.ESSource
|
||||||
|
)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
close(ch)
|
||||||
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
select {
|
||||||
if lines, err = input.ReadData(ctx, f_limit); err != nil {
|
case <-c.Done():
|
||||||
if errors.Is(err, io.EOF) {
|
return
|
||||||
return nil
|
default:
|
||||||
}
|
if lines, err = input.ReadData(c, f_limit); err != nil {
|
||||||
|
errCh <- err
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(lines) == 0 {
|
if len(lines) == 0 {
|
||||||
return nil
|
ch <- lines
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if succeed, err = output.WriteData(ctx, lines); err != nil {
|
ch <- lines
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(ctx)
|
||||||
|
|
||||||
|
var (
|
||||||
|
succeed int
|
||||||
|
total int
|
||||||
|
docs []*interfaces.ESSource
|
||||||
|
ok bool
|
||||||
|
)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
case err = <-errCh:
|
||||||
|
return err
|
||||||
|
case docs, ok = <-ch:
|
||||||
|
if !ok {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if succeed != len(lines) {
|
if len(docs) == 0 {
|
||||||
return fmt.Errorf("cmd.run: got lines=%d, only succeed=%d", len(lines), succeed)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Infof("Dump: %d docs succeed!!!", succeed)
|
if succeed, err = output.WriteData(ctx, docs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if succeed != len(docs) {
|
||||||
|
return fmt.Errorf("cmd.run: got lines=%d, only succeed=%d", len(docs), succeed)
|
||||||
|
}
|
||||||
|
|
||||||
|
total += succeed
|
||||||
|
|
||||||
|
logrus.Infof("Dump: succeed=%d total=%d docs succeed!!!", succeed, total)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,6 +111,7 @@ func (c *client) WriteData(ctx context.Context, docs []*interfaces.ESSource) (in
|
|||||||
err error
|
err error
|
||||||
indexer esutil.BulkIndexer
|
indexer esutil.BulkIndexer
|
||||||
count int
|
count int
|
||||||
|
be error
|
||||||
)
|
)
|
||||||
if indexer, err = esutil.NewBulkIndexer(esutil.BulkIndexerConfig{
|
if indexer, err = esutil.NewBulkIndexer(esutil.BulkIndexerConfig{
|
||||||
Client: c.c,
|
Client: c.c,
|
||||||
@ -134,6 +135,9 @@ func (c *client) WriteData(ctx context.Context, docs []*interfaces.ESSource) (in
|
|||||||
Index: c.index,
|
Index: c.index,
|
||||||
DocumentID: doc.DocId,
|
DocumentID: doc.DocId,
|
||||||
Body: bytes.NewReader(bs),
|
Body: bytes.NewReader(bs),
|
||||||
|
OnFailure: func(ctx context.Context, item esutil.BulkIndexerItem, item2 esutil.BulkIndexerResponseItem, bulkErr error) {
|
||||||
|
be = bulkErr
|
||||||
|
},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@ -144,9 +148,13 @@ func (c *client) WriteData(ctx context.Context, docs []*interfaces.ESSource) (in
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if be != nil {
|
||||||
|
return 0, be
|
||||||
|
}
|
||||||
|
|
||||||
stats := indexer.Stats()
|
stats := indexer.Stats()
|
||||||
if stats.NumFailed > 0 {
|
if stats.NumFailed > 0 {
|
||||||
return count, fmt.Errorf("write to xes failed=%d", stats.NumFailed)
|
return count, fmt.Errorf("write to xes failed_count=%d bulk_count=%d", stats.NumFailed, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
return count, nil
|
return count, nil
|
||||||
|
Reference in New Issue
Block a user