Compare commits
No commits in common. "d3524d0f059ae4fad27ce492a368ecf3c48808fb" and "91ddffe752e89d0cc042e59c194633fa759d126f" have entirely different histories.
d3524d0f05
...
91ddffe752
55
.github/workflows/build.yml
vendored
Normal file
55
.github/workflows/build.yml
vendored
Normal 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
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,3 +6,4 @@ mapping.json
|
||||
setting.json
|
||||
output.json
|
||||
*.txt
|
||||
dist
|
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
||||
module esgo2dump
|
||||
|
||||
go 1.20
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/elastic/go-elasticsearch/v7 v7.17.10
|
||||
|
@ -16,6 +16,8 @@ var (
|
||||
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=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"}}'`,
|
||||
|
@ -51,18 +51,30 @@ func run(cmd *cobra.Command, args []string) error {
|
||||
return executeData(cmd.Context(), ioi, ioo)
|
||||
case "mapping":
|
||||
var mapping map[string]any
|
||||
if mapping, err = ioi.ReadMapping(); err != nil {
|
||||
if mapping, err = ioi.ReadMapping(cmd.Context()); err != nil {
|
||||
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":
|
||||
var setting map[string]any
|
||||
if setting, err = ioi.ReadSetting(); err != nil {
|
||||
if setting, err = ioi.ReadSetting(cmd.Context()); err != nil {
|
||||
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:
|
||||
return fmt.Errorf("unknown type=%s", f_type)
|
||||
}
|
||||
@ -76,11 +88,8 @@ func executeData(ctx context.Context, input, output interfaces.DumpIO) error {
|
||||
)
|
||||
|
||||
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) {
|
||||
return nil
|
||||
}
|
||||
@ -92,7 +101,7 @@ func executeData(ctx context.Context, input, output interfaces.DumpIO) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if succeed, err = output.WriteData(lines); err != nil {
|
||||
if succeed, err = output.WriteData(ctx, lines); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -102,8 +111,6 @@ func executeData(ctx context.Context, input, output interfaces.DumpIO) error {
|
||||
|
||||
logrus.Infof("Dump: %d docs succeed!!!", succeed)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func newIO(source string, ioType interfaces.IO) (interfaces.DumpIO, error) {
|
||||
@ -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)
|
||||
|
||||
return xes.NewClient(iurl, qm)
|
||||
return xes.NewClient(iurl, ioType, qm)
|
||||
|
||||
ClientByFile:
|
||||
if ioType == interfaces.IOOutput {
|
||||
|
@ -1,14 +1,19 @@
|
||||
package interfaces
|
||||
|
||||
import "context"
|
||||
|
||||
type DumpIO interface {
|
||||
ReadData(int) ([]*ESSource, error)
|
||||
ReadMapping() (map[string]any, error)
|
||||
ReadSetting() (map[string]any, error)
|
||||
WriteData(docs []*ESSource) (int, error)
|
||||
WriteMapping(map[string]any) error
|
||||
WriteSetting(map[string]any) error
|
||||
ReadData(context.Context, int) ([]*ESSource, error)
|
||||
WriteData(ctx context.Context, docs []*ESSource) (int, error)
|
||||
|
||||
ReadMapping(context.Context) (map[string]any, error)
|
||||
WriteMapping(context.Context, map[string]any) error
|
||||
|
||||
ReadSetting(ctx context.Context) (map[string]any, error)
|
||||
WriteSetting(context.Context, map[string]any) error
|
||||
|
||||
Close() error
|
||||
|
||||
IsInput() bool
|
||||
IOType() IO
|
||||
IsFile() bool
|
||||
}
|
||||
|
@ -25,3 +25,9 @@ type ESResponse struct {
|
||||
Hits []*ESSource `json:"hits"`
|
||||
} `json:"hits"`
|
||||
}
|
||||
|
||||
type ESMapping map[string]struct {
|
||||
Mappings struct {
|
||||
Properties map[string]any `json:"properties"`
|
||||
} `json:"mappings"`
|
||||
}
|
||||
|
@ -15,3 +15,14 @@ func Timeout(seconds ...int) context.Context {
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -13,13 +13,14 @@ import (
|
||||
"github.com/elastic/go-elasticsearch/v7/esapi"
|
||||
"github.com/elastic/go-elasticsearch/v7/esutil"
|
||||
"github.com/sirupsen/logrus"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"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 (
|
||||
err error
|
||||
@ -55,6 +56,7 @@ func NewClient(url *url.URL, qm map[string]any) (interfaces.DumpIO, error) {
|
||||
RetryBackoff: nil,
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
DialContext: (&net.Dialer{Timeout: 5 * time.Second}).DialContext,
|
||||
},
|
||||
},
|
||||
); 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 &client{c: c, index: index, queryMap: qm}, nil
|
||||
return &client{c: c, index: index, queryMap: qm, iot: iot}, nil
|
||||
}
|
||||
|
||||
type client struct {
|
||||
c *elastic.Client
|
||||
iot interfaces.IO
|
||||
index string
|
||||
from int
|
||||
scrollId string
|
||||
queryMap map[string]any
|
||||
}
|
||||
|
||||
func (c *client) ReadSetting() (map[string]any, error) {
|
||||
r, err := c.c.Indices.GetSettings(
|
||||
c.c.Indices.GetSettings.WithIndex(c.index),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func (c *client) checkResponse(r *esapi.Response) error {
|
||||
if r.StatusCode == 200 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if r.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("status=%d, msg=%s", r.StatusCode, r.String())
|
||||
return 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 {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c *client) IsInput() bool {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (c *client) IOType() interfaces.IO {
|
||||
return c.iot
|
||||
}
|
||||
|
||||
func (c *client) IsFile() bool {
|
||||
@ -121,7 +105,7 @@ func (c *client) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *client) WriteData(docs []*interfaces.ESSource) (int, error) {
|
||||
func (c *client) WriteData(ctx context.Context, docs []*interfaces.ESSource) (int, error) {
|
||||
var (
|
||||
err error
|
||||
indexer esutil.BulkIndexer
|
||||
@ -155,7 +139,7 @@ func (c *client) WriteData(docs []*interfaces.ESSource) (int, error) {
|
||||
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
|
||||
}
|
||||
|
||||
@ -167,7 +151,7 @@ func (c *client) WriteData(docs []*interfaces.ESSource) (int, error) {
|
||||
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 (
|
||||
err error
|
||||
resp *esapi.Response
|
||||
@ -176,7 +160,7 @@ func (c *client) ReadData(i int) ([]*interfaces.ESSource, error) {
|
||||
|
||||
if c.scrollId == "" {
|
||||
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.WithSize(i),
|
||||
c.c.Search.WithFrom(0),
|
||||
@ -221,7 +205,7 @@ func (c *client) ReadData(i int) ([]*interfaces.ESSource, error) {
|
||||
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(
|
||||
c.c.Indices.GetMapping.WithIndex(c.index),
|
||||
)
|
||||
@ -241,7 +225,73 @@ func (c *client) ReadMapping() (map[string]any, error) {
|
||||
|
||||
return m, nil
|
||||
}
|
||||
func (c *client) WriteMapping(m map[string]any) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (c *client) WriteMapping(ctx context.Context, m map[string]any) error {
|
||||
var (
|
||||
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)
|
||||
}
|
||||
|
@ -2,28 +2,59 @@ package xfile
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"esgo2dump/internal/interfaces"
|
||||
"github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type client struct {
|
||||
f *os.File
|
||||
iot interfaces.IO
|
||||
scanner *bufio.Scanner
|
||||
}
|
||||
|
||||
func (c *client) ReadMapping() (map[string]any, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (c *client) ReadMapping(ctx context.Context) (map[string]any, error) {
|
||||
var (
|
||||
err error
|
||||
bs []byte
|
||||
)
|
||||
|
||||
if bs, err = io.ReadAll(c.f); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (c *client) ReadSetting() (map[string]any, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
m := make(map[string]any)
|
||||
|
||||
if err = json.Unmarshal(bs, &m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (c *client) WriteMapping(m map[string]any) error {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (c *client) ReadSetting(ctx context.Context) (map[string]any, error) {
|
||||
var (
|
||||
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(ctx context.Context, m map[string]any) error {
|
||||
bs, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -34,7 +65,7 @@ func (c *client) WriteMapping(m map[string]any) error {
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -45,16 +76,15 @@ func (c *client) WriteSetting(m map[string]any) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *client) IsInput() bool {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (c *client) IOType() interfaces.IO {
|
||||
return c.iot
|
||||
}
|
||||
|
||||
func (c *client) IsFile() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *client) WriteData(docs []*interfaces.ESSource) (int, error) {
|
||||
func (c *client) WriteData(ctx context.Context, docs []*interfaces.ESSource) (int, error) {
|
||||
var (
|
||||
err error
|
||||
bs []byte
|
||||
@ -78,7 +108,7 @@ func (c *client) WriteData(docs []*interfaces.ESSource) (int, error) {
|
||||
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 (
|
||||
err error
|
||||
count = 0
|
||||
@ -115,7 +145,7 @@ func (c *client) Close() 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 {
|
||||
c.scanner = bufio.NewScanner(c.f)
|
||||
|
42
readme.md
Normal file
42
readme.md
Normal 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
|
Loading…
x
Reference in New Issue
Block a user