-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
83 lines (74 loc) · 1.91 KB
/
loader.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
71
72
73
74
75
76
77
78
79
80
81
82
83
// loader parses food data central csv and ingests it into couchbase documents
package main
import (
"flag"
"io"
"log"
"os"
"github.com/littlebunch/fdc-api/ds"
"github.com/littlebunch/fdc-api/ds/cb"
fdc "github.com/littlebunch/fdc-api/model"
"github.com/littlebunch/fdc-ingest/ingest"
"github.com/littlebunch/fdc-ingest/ingest/bfpd"
"github.com/littlebunch/fdc-ingest/ingest/dictionaries"
"github.com/littlebunch/fdc-ingest/ingest/fndds"
"github.com/littlebunch/fdc-ingest/ingest/sr"
)
var (
c = flag.String("c", "config.yml", "YAML Config file")
l = flag.String("l", "/tmp/ingest.out", "send log output to this file -- defaults to /tmp/ingest.out")
i = flag.String("i", "", "Input csv file")
t = flag.String("t", "", "Input file type")
cnt = 0
cs fdc.Config
)
func init() {
var (
err error
lfile *os.File
)
lfile, err = os.OpenFile(*l, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open log file", *l, ":", err)
}
m := io.MultiWriter(lfile, os.Stdout)
log.SetOutput(m)
}
func main() {
log.Print("Starting ingest")
flag.Parse()
var dt fdc.DocType
dtype := dt.ToDocType(*t)
if dtype == 999 {
log.Fatalln("Valid t option is required")
}
var (
cs fdc.Config
in ingest.Ingest
cb cb.Cb
ds ds.DataSource
)
cs.GetConfig(c)
// create a datastore and connect to it
ds = &cb
if err := ds.ConnectDs(cs); err != nil {
log.Fatalln("Cannot connect to datastore ", err)
}
// implement the Ingest interface
if dtype == fdc.BFPD {
in = bfpd.Bfpd{Doctype: dt.ToString(fdc.BFPD)}
} else if dtype == fdc.FNDDS {
in = fndds.Fndds{Doctype: dt.ToString(fdc.FNDDS)}
} else if dtype == fdc.SR {
in = sr.Sr{Doctype: dt.ToString(fdc.SR)}
} else {
in = dictionaries.Dictionary{Dt: dtype}
}
// ingest the csv files
if err := in.ProcessFiles(*i, ds, cs.CouchDb.Bucket); err != nil {
log.Fatal(err)
}
log.Println("Finished.")
ds.CloseDs()
os.Exit(0)
}