Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
mydumper: add select filter
Browse files Browse the repository at this point in the history
  • Loading branch information
BohuTANG committed Aug 16, 2019
1 parent 3d01f87 commit 09e78c2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
8 changes: 8 additions & 0 deletions conf/mydumper.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ vars= "xx=xx;xx=xx;"
[where]
sample_table1 = created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
sample_table2 = created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)

# Use this to override value returned from tables. These are optional
[select]
user.salt = 'reset salt of all system users'
user.password = 'reset password of all system users'

customer.first_name = CONCAT('Bohu', id)
customer.last_name = 'Last'
22 changes: 22 additions & 0 deletions src/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package main
import (
"common"
"fmt"
"strings"

ini "github.com/dlintw/goconf"
)
Expand Down Expand Up @@ -65,6 +66,27 @@ func parseDumperConfig(file string) (*common.Args, error) {
return nil, err
}

var selects []string
if selects, err = cfg.GetOptions("select"); err != nil {
return nil, err
}
for _, tblcol := range selects {
var table, column string
split := strings.Split(tblcol, ".")
table = split[0]
column = split[1]

if args.Selects == nil {
args.Selects = make(map[string]map[string]string)
}
if args.Selects[table] == nil {
args.Selects[table] = make(map[string]string, 0)
}
if args.Selects[table][column], err = cfg.GetString("select", tblcol); err != nil {
return nil, err
}
}

args.Address = fmt.Sprintf("%s:%d", host, port)
args.User = user
args.Password = password
Expand Down
1 change: 1 addition & 0 deletions src/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Args struct {
Allrows uint64
OverwriteTables bool
Wheres map[string]string
Selects map[string]map[string]string

// Interval in millisecond.
IntervalMs int
Expand Down
28 changes: 21 additions & 7 deletions src/common/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,34 @@ func dumpTable(log *xlog.Log, conn *Connection, args *Args, table string) {
var allBytes uint64
var allRows uint64
var where string
var selfields []string

fields := make([]string, 0, 16)
{
cursor, err := conn.StreamFetch(fmt.Sprintf("SELECT * FROM `%s`.`%s` LIMIT 1", args.Database, table))
AssertNil(err)

flds := cursor.Fields()
for _, fld := range flds {
fields = append(fields, fmt.Sprintf("`%s`", fld.Name))
replacement, ok := args.Selects[table][fld.Name]
if ok {
selfields = append(selfields, fmt.Sprintf("%s AS `%s`", replacement, fld.Name))
} else {
selfields = append(selfields, fmt.Sprintf("`%s`", fld.Name))
}
}
err = cursor.Close()
AssertNil(err)
}

if v, ok := args.Wheres[table]; ok {
where = fmt.Sprintf(" WHERE %v", v)
}

cursor, err := conn.StreamFetch(fmt.Sprintf("SELECT * FROM `%s`.`%s` %s", args.Database, table, where))
cursor, err := conn.StreamFetch(fmt.Sprintf("SELECT %s FROM `%s`.`%s` %s", strings.Join(selfields, ", "), args.Database, table, where))
AssertNil(err)

fields := make([]string, 0, 16)
flds := cursor.Fields()
for _, fld := range flds {
fields = append(fields, fmt.Sprintf("`%s`", fld.Name))
}

fileNo := 1
stmtsize := 0
chunkbytes := 0
Expand Down

4 comments on commit 09e78c2

@geshan
Copy link

@geshan geshan commented on 09e78c2 Aug 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@geshan
Copy link

@geshan geshan commented on 09e78c2 Aug 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to do the where and this select in the streamer too?

@BohuTANG
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@geshan
Yes, only some backports, want to try?

@geshan
Copy link

@geshan geshan commented on 09e78c2 Aug 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might give it a shot.

Please sign in to comment.