forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
to_sql.go
65 lines (61 loc) · 1.7 KB
/
to_sql.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
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"strings"
)
func SplitJson(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(strings.TrimSpace(string(data))) == 0 {
return 0, nil, nil // No more records; finish scanning
}
if i := bytes.IndexByte(data, '}'); i > 0 {
return i + 1, data[:i+1], nil // Encountered finished json record
} else if atEOF {
return 0, nil, fmt.Errorf("incomplete JSON record: file did not end in '}'")
}
return 0, nil, nil // request more data
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Error: to_sql takes one argument (name of the "+
"table receiving writes), but received none\n")
os.Exit(1)
}
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(SplitJson)
for scanner.Scan() {
// Parse json as plain interface{}. See https://blog.golang.org/json-and-go
var jsinter interface{}
json.Unmarshal(scanner.Bytes(), &jsinter)
js, found := jsinter.(map[string]interface{})
if !found {
log.Fatalf("Could not parse json (may be nested):\n\"\"\"\n%s\n\"\"\"",
scanner.Text())
}
// Convert json into SQL INSERT
keys := make([]string, len(js))
values := make([]string, len(js))
i := 0
for k, v := range js {
keys[i] = k
values[i] = fmt.Sprintf("%v", v)
_, ismap := v.(map[string]interface{})
_, isinterface := v.([]interface{})
if ismap || isinterface {
log.Fatalf("Could not convert nested json value: \"%v\"", values[i])
}
i++
}
fmt.Printf("INSERT INTO %s (%s) VALUES (%s);\n",
os.Args[1],
strings.Join(keys, ", "),
strings.Join(values, ", "))
}
if scanner.Err() != nil {
log.Fatalf("Could not scan json record: %s", scanner.Err().Error())
}
}