-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathsimdjson_example.go
50 lines (43 loc) · 1015 Bytes
/
simdjson_example.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
//go:build ignore
// +build ignore
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"github.com/minio/simdjson-go"
)
func printKeyHistogram(pj *simdjson.ParsedJson, key string) (err error) {
var elem *simdjson.Element
count := make(map[string]int)
err = pj.ForEach(func(i simdjson.Iter) error {
if elem, err = i.FindElement(elem, key); err != nil {
return nil
}
if elem.Type == simdjson.TypeString {
s, _ := elem.Iter.String()
count[s]++
}
return nil
})
res, _ := json.Marshal(count)
fmt.Println(key, ":", string(res)+"\n")
return err
}
func main() {
if !simdjson.SupportedCPU() {
log.Fatal("Unsupported CPU")
}
msg, err := ioutil.ReadFile("parking-citations.json")
if err != nil {
log.Fatalf("Failed to load file: %v", err)
}
parsed, err := simdjson.ParseND(msg, nil)
if err != nil {
log.Fatalf("Failed to parse JSON: %v", err)
}
printKeyHistogram(parsed, "Make")
printKeyHistogram(parsed, "MeterId")
printKeyHistogram(parsed, "ViolationCode")
}