-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
113 lines (90 loc) · 2.27 KB
/
main.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
105
106
107
108
109
110
111
112
113
package main
import (
"os"
"flag"
"io"
"bufio"
"fmt"
"time"
"encoding/csv"
)
var rdfPath = flag.String("path", "", "Specify rdf data path")
const CAP = 100000
func main() {
flag.Parse()
if err := Read(*rdfPath); err != nil {
panic(err)
}
}
func Read(rdfPath string) error {
srcFile, err := os.Open(rdfPath)
if err != nil {
return err
}
defer srcFile.Close()
var vFilePath = "./vertex.csv"
vFile, err := os.Create(vFilePath)
if err != nil {
return err
}
defer vFile.Close()
var eFilePath = "./edge.csv"
eFile, err := os.Create(eFilePath)
if err != nil {
return err
}
defer eFile.Close()
reader := csv.NewReader(bufio.NewReader(srcFile))
vWriter := csv.NewWriter(bufio.NewWriter(vFile))
eWriter := csv.NewWriter(bufio.NewWriter(eFile))
defer vWriter.Flush()
defer eWriter.Flush()
now := time.Now()
defer func() {
fmt.Printf("Finish convert rdf data, consume time: %.2fs\n", time.Since(now).Seconds())
}()
lineNum, numErrorLines := 0, 0
lVRecord := make([]string, 1)
rVRecord := make([]string, 1)
eRecord := make([]string, 3)
exists := make(map[string]bool)
for {
if lineNum % 100000 == 0 {
fmt.Printf("hava read lines: %d\n", lineNum)
}
line, err := reader.Read()
if err == io.EOF {
fmt.Printf("totalLines: %d, errorLines: %d\n", lineNum, numErrorLines)
break
}
lineNum++
if err != nil {
numErrorLines++
continue
}
if len(line) != 3 {
numErrorLines++
continue
}
if len(exists) >= CAP {
exists = make(map[string]bool)
}
vid := line[0]
lVRecord[0] = vid
if _, ok := exists[vid]; !ok {
exists[vid] = true
vWriter.Write(lVRecord)
}
vid = line[2]
rVRecord[0] = vid
if _, ok := exists[vid]; !ok {
exists[vid] = true
vWriter.Write(rVRecord)
}
eRecord[0] = lVRecord[0]
eRecord[1] = rVRecord[0]
eRecord[2] = line[1]
eWriter.Write(eRecord)
}
return nil
}