/
geonames_import.go
104 lines (78 loc) · 1.58 KB
/
geonames_import.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"reflect"
"strconv"
"time"
elastigo "github.com/mattbaird/elastigo/lib"
)
type GeoElement struct {
Id string
}
var (
host *string = flag.String("host", "localhost", "Elasticsearch Host")
)
func main() {
pathFilePtr := flag.String("pathFile", "", "path of the geonames' file.")
flag.Parse()
if *pathFilePtr == "" {
flag.PrintDefaults()
os.Exit(2)
}
channel := make(chan []string)
for i := 0; i < 10; i++ {
go printCsvLine(channel)
}
go readCsv(pathFilePtr, channel)
time.Sleep(time.Second * 10000)
}
func readCsv(pathFile *string, channel chan []string) {
csvFile, err := os.Open(*pathFile)
defer csvFile.Close()
if err != nil {
panic(err)
}
reader := csv.NewReader(csvFile)
reader.Comma = '\t'
counter := 0
for {
counter = counter + 1
record, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
reflect.TypeOf(record)
channel <- record
if counter%1000 == 0 {
if counter%10000 == 0 {
fmt.Print(". " + strconv.Itoa(counter/1000) + "K")
fmt.Println("")
} else {
fmt.Print(".")
}
}
}
fmt.Println("")
fmt.Println("==================================")
fmt.Println("Total processed lines: ", counter-1)
os.Exit(1)
}
func printCsvLine(channel chan []string) {
c := elastigo.NewConn()
for {
record := <-channel
element := GeoElement{record[1]}
value, _ := json.Marshal(element)
_, err := c.Index("geolocations", "geoname", record[0], nil, string(value))
if err != nil {
panic(err)
}
}
}