forked from azwarnrst/redis_csv_toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
70 lines (61 loc) · 1.62 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"flag"
"fmt"
"github.com/azwarnrst/redis_csv_toolkit/util"
"github.com/garyburd/redigo/redis"
"log"
"os"
"time"
)
var (
cfg *util.Config
redisPool *redis.Pool
err error
redisImportLimit = 1000
isTemporaryKey bool
)
func main() {
// initialize app configuration
cfg = util.NewConfig()
if err := cfg.ReadConfig(); err != nil {
log.Fatalf("[Init][Configuration] %v", err)
} else {
log.Printf("[Init][Configuration] file loaded successfully")
}
redisHost := flag.String("host", "", "redis host")
importLimit := flag.Int("limit", 30000, "Max concurrent import to redis")
redisPort := flag.Int("port", 0, "redis port")
csvInFile := flag.String("in", "", "input filename to import")
ttl := flag.Int("ttl", 0, "redis key expiration time (seconds)")
flag.Parse()
if *redisHost != "" && *redisPort > 0 {
cfg.RedisConfig.Host = fmt.Sprintf("%s:%d", *redisHost, *redisPort)
}
if *csvInFile != "" {
cfg.AppConfig.FileName = *csvInFile
} else {
log.Printf("no file input specified ...")
os.Exit(0)
}
if *importLimit > 0 {
redisImportLimit = *importLimit
}
if *ttl > 0 {
isTemporaryKey = true
}
start := time.Now()
log.Printf("Target redis host : %s", cfg.RedisConfig.Host)
redisConn, err := redis.Dial("tcp", cfg.RedisConfig.Host)
if err != nil {
log.Fatal(err)
}
csvUtil := util.NewCsv(cfg, redisConn, redisImportLimit)
err = csvUtil.ParseCsv(isTemporaryKey, *ttl)
if err != nil {
log.Printf("parse error : %v\n", err)
}
t := time.Since(start)
log.Printf("import completed in %f seconds", float64(t)/float64(time.Second))
fmt.Println("")
}