Compare commits

...

No commits in common. "d3524d0f059ae4fad27ce492a368ecf3c48808fb" and "91ddffe752e89d0cc042e59c194633fa759d126f" have entirely different histories.

11 changed files with 293 additions and 84 deletions

55
.github/workflows/build.yml vendored Normal file
View File

@ -0,0 +1,55 @@
name: Auto Build
on:
push:
tags:
- 'v*'
jobs:
build-job:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
pull-requests: write
repository-projects: write
steps:
- name: checkout repository
uses: actions/checkout@v4
- name: install golang
uses: actions/setup-go@v4
with:
go-version: '1.18'
- name: build linux amd64
run: CGO_ENABLE=0 GOOS=linux GOARCH=amd64 go build -ldflags='-s -w' -o dist/esgo2dump_${{ github.ref_name }}_linux_amd64 .
- name: build linux arm64
run: CGO_ENABLE=0 GOOS=linux GOARCH=arm64 go build -ldflags='-s -w' -o dist/esgo2dump_${{ github.ref_name }}_linux_arm64 .
- name: build windows amd64
run: CGO_ENABLE=0 GOOS=windows GOARCH=amd64 go build -ldflags='-s -w' -o dist/esgo2dump_${{ github.ref_name }}_windows_amd64.exe .
- name: build windows arm64
run: CGO_ENABLE=0 GOOS=windows GOARCH=arm64 go build -ldflags='-s -w' -o dist/esgo2dump_${{ github.ref_name }}_windows_arm64.exe .
- name: build darwin amd64
run: CGO_ENABLE=0 GOOS=darwin GOARCH=amd64 go build -ldflags='-s -w' -o dist/esgo2dump_${{ github.ref_name }}_darwin_amd64 .
- name: build darwin arm64
run: CGO_ENABLE=0 GOOS=darwin GOARCH=arm64 go build -ldflags='-s -w' -o dist/esgo2dump_${{ github.ref_name }}_darwin_arm64 .
- name: create releases
id: create_releases
uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
title: "Release_${{ github.ref_name }}"
files: |
dist/esgo2dump_${{ github.ref_name }}_linux_amd64
dist/esgo2dump_${{ github.ref_name }}_linux_arm64
dist/esgo2dump_${{ github.ref_name }}_windows_amd64.exe
dist/esgo2dump_${{ github.ref_name }}_windows_arm64.exe
dist/esgo2dump_${{ github.ref_name }}_darwin_amd64
dist/esgo2dump_${{ github.ref_name }}_darwin_amd64
dist/esgo2dump_${{ github.ref_name }}_darwin_arm64

3
.gitignore vendored
View File

@ -5,4 +5,5 @@ data.json
mapping.json mapping.json
setting.json setting.json
output.json output.json
*.txt *.txt
dist

2
go.mod
View File

@ -1,6 +1,6 @@
module esgo2dump module esgo2dump
go 1.20 go 1.18
require ( require (
github.com/elastic/go-elasticsearch/v7 v7.17.10 github.com/elastic/go-elasticsearch/v7 v7.17.10

View File

@ -16,6 +16,8 @@ var (
Example: ` Example: `
esgo2dump --input=http://127.0.0.1:9200/some_index --output=./data.json esgo2dump --input=http://127.0.0.1:9200/some_index --output=./data.json
esgo2dump --input=http://127.0.0.1:9200/some_index --output=http://192.168.1.1:9200/some_index --limit=5000
esgo2dump --input=https://username:password@127.0.0.1:9200/some_index --output=./data.json esgo2dump --input=https://username:password@127.0.0.1:9200/some_index --output=./data.json
esgo2dump --input=http://127.0.0.1:9200/some_index --output=./data.json --query='{"match": {"name": "some_name"}}'`, esgo2dump --input=http://127.0.0.1:9200/some_index --output=./data.json --query='{"match": {"name": "some_name"}}'`,

View File

@ -51,18 +51,30 @@ func run(cmd *cobra.Command, args []string) error {
return executeData(cmd.Context(), ioi, ioo) return executeData(cmd.Context(), ioi, ioo)
case "mapping": case "mapping":
var mapping map[string]any var mapping map[string]any
if mapping, err = ioi.ReadMapping(); err != nil { if mapping, err = ioi.ReadMapping(cmd.Context()); err != nil {
return err return err
} }
return ioo.WriteMapping(mapping) if err = ioo.WriteMapping(cmd.Context(), mapping); err != nil {
return err
}
logrus.Info("Dump: write mapping succeed!!!")
return nil
case "setting": case "setting":
var setting map[string]any var setting map[string]any
if setting, err = ioi.ReadSetting(); err != nil { if setting, err = ioi.ReadSetting(cmd.Context()); err != nil {
return err return err
} }
return ioo.WriteSetting(setting) if err = ioo.WriteSetting(cmd.Context(), setting); err != nil {
return err
}
logrus.Info("Dump: write setting succeed!!!")
return nil
default: default:
return fmt.Errorf("unknown type=%s", f_type) return fmt.Errorf("unknown type=%s", f_type)
} }
@ -76,33 +88,28 @@ func executeData(ctx context.Context, input, output interfaces.DumpIO) error {
) )
for { for {
select {
case <-ctx.Done():
default:
if lines, err = input.ReadData(f_limit); err != nil { if lines, err = input.ReadData(ctx, f_limit); err != nil {
if errors.Is(err, io.EOF) { if errors.Is(err, io.EOF) {
return nil
}
return err
}
if len(lines) == 0 {
return nil return nil
} }
if succeed, err = output.WriteData(lines); err != nil { return err
return err
}
if succeed != len(lines) {
return fmt.Errorf("cmd.run: got lines=%d, only succeed=%d", len(lines), succeed)
}
logrus.Infof("Dump: %d docs succeed!!!", succeed)
} }
if len(lines) == 0 {
return nil
}
if succeed, err = output.WriteData(ctx, lines); err != nil {
return err
}
if succeed != len(lines) {
return fmt.Errorf("cmd.run: got lines=%d, only succeed=%d", len(lines), succeed)
}
logrus.Infof("Dump: %d docs succeed!!!", succeed)
} }
} }
@ -140,7 +147,7 @@ func newIO(source string, ioType interfaces.IO) (interfaces.DumpIO, error) {
logrus.Debugf("newIO.%s: source as url=%+v", ioType.Code(), *iurl) logrus.Debugf("newIO.%s: source as url=%+v", ioType.Code(), *iurl)
return xes.NewClient(iurl, qm) return xes.NewClient(iurl, ioType, qm)
ClientByFile: ClientByFile:
if ioType == interfaces.IOOutput { if ioType == interfaces.IOOutput {

View File

@ -1,14 +1,19 @@
package interfaces package interfaces
import "context"
type DumpIO interface { type DumpIO interface {
ReadData(int) ([]*ESSource, error) ReadData(context.Context, int) ([]*ESSource, error)
ReadMapping() (map[string]any, error) WriteData(ctx context.Context, docs []*ESSource) (int, error)
ReadSetting() (map[string]any, error)
WriteData(docs []*ESSource) (int, error) ReadMapping(context.Context) (map[string]any, error)
WriteMapping(map[string]any) error WriteMapping(context.Context, map[string]any) error
WriteSetting(map[string]any) error
ReadSetting(ctx context.Context) (map[string]any, error)
WriteSetting(context.Context, map[string]any) error
Close() error Close() error
IsInput() bool IOType() IO
IsFile() bool IsFile() bool
} }

View File

@ -25,3 +25,9 @@ type ESResponse struct {
Hits []*ESSource `json:"hits"` Hits []*ESSource `json:"hits"`
} `json:"hits"` } `json:"hits"`
} }
type ESMapping map[string]struct {
Mappings struct {
Properties map[string]any `json:"properties"`
} `json:"mappings"`
}

View File

@ -15,3 +15,14 @@ func Timeout(seconds ...int) context.Context {
return ctx return ctx
} }
func TimeoutCtx(ctx context.Context, seconds ...int) context.Context {
second := 30
if len(seconds) > 0 && seconds[0] > 0 {
second = seconds[0]
}
timeout, _ := context.WithTimeout(ctx, time.Duration(second)*time.Second)
return timeout
}

View File

@ -13,13 +13,14 @@ import (
"github.com/elastic/go-elasticsearch/v7/esapi" "github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/elastic/go-elasticsearch/v7/esutil" "github.com/elastic/go-elasticsearch/v7/esutil"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"net"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"time" "time"
) )
func NewClient(url *url.URL, qm map[string]any) (interfaces.DumpIO, error) { func NewClient(url *url.URL, iot interfaces.IO, qm map[string]any) (interfaces.DumpIO, error) {
var ( var (
err error err error
@ -55,6 +56,7 @@ func NewClient(url *url.URL, qm map[string]any) (interfaces.DumpIO, error) {
RetryBackoff: nil, RetryBackoff: nil,
Transport: &http.Transport{ Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DialContext: (&net.Dialer{Timeout: 5 * time.Second}).DialContext,
}, },
}, },
); err != nil { ); err != nil {
@ -71,46 +73,28 @@ func NewClient(url *url.URL, qm map[string]any) (interfaces.DumpIO, error) {
return nil, fmt.Errorf("info xes status=%d", infoResp.StatusCode) return nil, fmt.Errorf("info xes status=%d", infoResp.StatusCode)
} }
return &client{c: c, index: index, queryMap: qm}, nil return &client{c: c, index: index, queryMap: qm, iot: iot}, nil
} }
type client struct { type client struct {
c *elastic.Client c *elastic.Client
iot interfaces.IO
index string index string
from int from int
scrollId string scrollId string
queryMap map[string]any queryMap map[string]any
} }
func (c *client) ReadSetting() (map[string]any, error) { func (c *client) checkResponse(r *esapi.Response) error {
r, err := c.c.Indices.GetSettings( if r.StatusCode == 200 {
c.c.Indices.GetSettings.WithIndex(c.index), return nil
)
if err != nil {
return nil, err
} }
if r.StatusCode != 200 { return fmt.Errorf("status=%d msg=%s", r.StatusCode, r.String())
return nil, fmt.Errorf("status=%d, msg=%s", r.StatusCode, r.String())
}
m := make(map[string]any)
decoder := json.NewDecoder(r.Body)
if err = decoder.Decode(&m); err != nil {
return nil, err
}
return m, nil
} }
func (c *client) WriteSetting(m map[string]any) error { func (c *client) IOType() interfaces.IO {
//TODO implement me return c.iot
panic("implement me")
}
func (c *client) IsInput() bool {
//TODO implement me
panic("implement me")
} }
func (c *client) IsFile() bool { func (c *client) IsFile() bool {
@ -121,7 +105,7 @@ func (c *client) Close() error {
return nil return nil
} }
func (c *client) WriteData(docs []*interfaces.ESSource) (int, error) { func (c *client) WriteData(ctx context.Context, docs []*interfaces.ESSource) (int, error) {
var ( var (
err error err error
indexer esutil.BulkIndexer indexer esutil.BulkIndexer
@ -155,7 +139,7 @@ func (c *client) WriteData(docs []*interfaces.ESSource) (int, error) {
count++ count++
} }
if err = indexer.Close(util.Timeout(opt.Timeout)); err != nil { if err = indexer.Close(util.TimeoutCtx(ctx, opt.Timeout)); err != nil {
return 0, err return 0, err
} }
@ -167,7 +151,7 @@ func (c *client) WriteData(docs []*interfaces.ESSource) (int, error) {
return count, nil return count, nil
} }
func (c *client) ReadData(i int) ([]*interfaces.ESSource, error) { func (c *client) ReadData(ctx context.Context, i int) ([]*interfaces.ESSource, error) {
var ( var (
err error err error
resp *esapi.Response resp *esapi.Response
@ -176,7 +160,7 @@ func (c *client) ReadData(i int) ([]*interfaces.ESSource, error) {
if c.scrollId == "" { if c.scrollId == "" {
qs := []func(*esapi.SearchRequest){ qs := []func(*esapi.SearchRequest){
c.c.Search.WithContext(util.Timeout(opt.Timeout)), c.c.Search.WithContext(util.TimeoutCtx(ctx, opt.Timeout)),
c.c.Search.WithIndex(c.index), c.c.Search.WithIndex(c.index),
c.c.Search.WithSize(i), c.c.Search.WithSize(i),
c.c.Search.WithFrom(0), c.c.Search.WithFrom(0),
@ -221,7 +205,7 @@ func (c *client) ReadData(i int) ([]*interfaces.ESSource, error) {
return result.Hits.Hits, nil return result.Hits.Hits, nil
} }
func (c *client) ReadMapping() (map[string]any, error) { func (c *client) ReadMapping(ctx context.Context) (map[string]any, error) {
r, err := c.c.Indices.GetMapping( r, err := c.c.Indices.GetMapping(
c.c.Indices.GetMapping.WithIndex(c.index), c.c.Indices.GetMapping.WithIndex(c.index),
) )
@ -241,7 +225,73 @@ func (c *client) ReadMapping() (map[string]any, error) {
return m, nil return m, nil
} }
func (c *client) WriteMapping(m map[string]any) error { func (c *client) WriteMapping(ctx context.Context, m map[string]any) error {
//TODO implement me var (
panic("implement me") err error
bs []byte
result *esapi.Response
)
for idxKey := range m {
if bs, err = json.Marshal(m[idxKey]); err != nil {
return err
}
if result, err = c.c.Indices.Create(
c.index,
c.c.Indices.Create.WithContext(util.TimeoutCtx(ctx, opt.Timeout)),
c.c.Indices.Create.WithBody(bytes.NewReader(bs)),
); err != nil {
return err
}
if err = c.checkResponse(result); err != nil {
return err
}
}
return nil
}
func (c *client) ReadSetting(ctx context.Context) (map[string]any, error) {
r, err := c.c.Indices.GetSettings(
c.c.Indices.GetSettings.WithContext(util.TimeoutCtx(ctx, opt.Timeout)),
c.c.Indices.GetSettings.WithIndex(c.index),
)
if err != nil {
return nil, err
}
if r.StatusCode != 200 {
return nil, fmt.Errorf("status=%d, msg=%s", r.StatusCode, r.String())
}
m := make(map[string]any)
decoder := json.NewDecoder(r.Body)
if err = decoder.Decode(&m); err != nil {
return nil, err
}
return m, nil
}
func (c *client) WriteSetting(ctx context.Context, m map[string]any) error {
var (
err error
bs []byte
result *esapi.Response
)
if bs, err = json.Marshal(m); err != nil {
return err
}
if result, err = c.c.Indices.PutSettings(
bytes.NewReader(bs),
c.c.Indices.PutSettings.WithContext(util.TimeoutCtx(ctx, opt.Timeout)),
); err != nil {
return err
}
return c.checkResponse(result)
} }

View File

@ -2,28 +2,59 @@ package xfile
import ( import (
"bufio" "bufio"
"context"
"encoding/json" "encoding/json"
"esgo2dump/internal/interfaces" "esgo2dump/internal/interfaces"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"io"
"os" "os"
) )
type client struct { type client struct {
f *os.File f *os.File
iot interfaces.IO
scanner *bufio.Scanner scanner *bufio.Scanner
} }
func (c *client) ReadMapping() (map[string]any, error) { func (c *client) ReadMapping(ctx context.Context) (map[string]any, error) {
//TODO implement me var (
panic("implement me") err error
bs []byte
)
if bs, err = io.ReadAll(c.f); err != nil {
return nil, err
}
m := make(map[string]any)
if err = json.Unmarshal(bs, &m); err != nil {
return nil, err
}
return m, nil
} }
func (c *client) ReadSetting() (map[string]any, error) { func (c *client) ReadSetting(ctx context.Context) (map[string]any, error) {
//TODO implement me var (
panic("implement me") err error
bs []byte
)
if bs, err = io.ReadAll(c.f); err != nil {
return nil, err
}
m := make(map[string]any)
if err = json.Unmarshal(bs, &m); err != nil {
return nil, err
}
return m, nil
} }
func (c *client) WriteMapping(m map[string]any) error { func (c *client) WriteMapping(ctx context.Context, m map[string]any) error {
bs, err := json.Marshal(m) bs, err := json.Marshal(m)
if err != nil { if err != nil {
return err return err
@ -34,7 +65,7 @@ func (c *client) WriteMapping(m map[string]any) error {
return err return err
} }
func (c *client) WriteSetting(m map[string]any) error { func (c *client) WriteSetting(ctx context.Context, m map[string]any) error {
bs, err := json.Marshal(m) bs, err := json.Marshal(m)
if err != nil { if err != nil {
return err return err
@ -45,16 +76,15 @@ func (c *client) WriteSetting(m map[string]any) error {
return err return err
} }
func (c *client) IsInput() bool { func (c *client) IOType() interfaces.IO {
//TODO implement me return c.iot
panic("implement me")
} }
func (c *client) IsFile() bool { func (c *client) IsFile() bool {
return true return true
} }
func (c *client) WriteData(docs []*interfaces.ESSource) (int, error) { func (c *client) WriteData(ctx context.Context, docs []*interfaces.ESSource) (int, error) {
var ( var (
err error err error
bs []byte bs []byte
@ -78,7 +108,7 @@ func (c *client) WriteData(docs []*interfaces.ESSource) (int, error) {
return count, nil return count, nil
} }
func (c *client) ReadData(i int) ([]*interfaces.ESSource, error) { func (c *client) ReadData(ctx context.Context, i int) ([]*interfaces.ESSource, error) {
var ( var (
err error err error
count = 0 count = 0
@ -115,7 +145,7 @@ func (c *client) Close() error {
} }
func NewClient(file *os.File, ioType interfaces.IO) (interfaces.DumpIO, error) { func NewClient(file *os.File, ioType interfaces.IO) (interfaces.DumpIO, error) {
c := &client{f: file} c := &client{f: file, iot: ioType}
if ioType == interfaces.IOInput { if ioType == interfaces.IOInput {
c.scanner = bufio.NewScanner(c.f) c.scanner = bufio.NewScanner(c.f)

42
readme.md Normal file
View File

@ -0,0 +1,42 @@
# esgo2dump
# dump elasticsearch with golang
---
- 当前仅支持 elasticsearch 7
---
### install
- with golang >= 1.18
`go install github.com/loveuer/esgo2dump@latest`
- download pre-build release:
[releases](https://github.com/loveuer/esgo2dump/releases)
### usage
`esgo2dump -h`
```bash
esgo2dump --input=http://127.0.0.1:9200/some_index --output=./data.json
esgo2dump --input=http://127.0.0.1:9200/some_index --output=http://192.168.1.1:9200/some_index --limit=5000
esgo2dump --input=https://username:password@127.0.0.1:9200/some_index --output=./data.json
esgo2dump --input=http://127.0.0.1:9200/some_index --output=./data.json --query='{"match": {"name": "some_name"}}'`,
```
### roadmap
- [x] data dump
- [x] mapping dump
- [x] es to file
- [x] es to es
- [x] auto create index with mapping
- [ ] auto create index with mapping,setting
- [ ] support es8