Last active
March 7, 2021 09:06
-
-
Save jjylik/11c3c3da19a18439e182e0dc2bed9ae5 to your computer and use it in GitHub Desktop.
WAL2JSON output naive go parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"change": [ | |
{ | |
"kind": "update", | |
"schema": "public", | |
"table": "users", | |
"columnnames": ["id", "name"], | |
"columntypes": ["bigint", "text"], | |
"columnvalues": [14, "emily"], | |
"oldkeys": { | |
"keynames": ["id"], | |
"keytypes": ["bigint"], | |
"keyvalues": [14] | |
} | |
} | |
] | |
} | |
{ | |
"change": [ | |
{ | |
"kind": "insert", | |
"schema": "public", | |
"table": "users", | |
"columnnames": ["id", "name"], | |
"columntypes": ["bigint", "text"], | |
"columnvalues": [16, "emma"] | |
} | |
] | |
} | |
{ | |
"change": [ | |
{ | |
"kind": "update", | |
"schema": "public", | |
"table": "users", | |
"columnnames": ["id", "name"], | |
"columntypes": ["bigint", "text"], | |
"columnvalues": [16, "emily"], | |
"oldkeys": { | |
"keynames": ["id"], | |
"keytypes": ["bigint"], | |
"keyvalues": [16] | |
} | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"flag" | |
"fmt" | |
"strings" | |
"github.com/hpcloud/tail" | |
) | |
var filename = flag.String("f", "./events.log", "event log file") | |
var table = flag.String("t", "users", "table to listen events from") | |
type changeEvent struct { | |
Kind string `json:"kind"` | |
Schema string `json:"schema"` | |
Table string `json:"table"` | |
ColumnNames []string `json:"columnnames"` | |
ColumnTypes []string `json:"columntypes"` | |
ColumnValues []interface{} `json:"columnvalues"` | |
} | |
type event struct { | |
Change []changeEvent `json:"change"` | |
} | |
func init() { | |
flag.Parse() | |
} | |
func main() { | |
t, err := tail.TailFile(*filename, tail.Config{Follow: true}) | |
if err != nil { | |
panic("could not open file") | |
} | |
openBrackets := 0 | |
var readBuffer bytes.Buffer | |
for line := range t.Lines { | |
readBuffer.WriteString(line.Text) | |
if strings.Contains(line.Text, "{") { | |
openBrackets = openBrackets + 1 | |
} | |
if strings.Contains(line.Text, "}") { | |
if openBrackets == 1 { | |
parseEvent(readBuffer.Bytes()) | |
readBuffer.Reset() | |
} | |
openBrackets = openBrackets - 1 | |
} | |
} | |
} | |
func parseEvent(rawEvent []byte) { | |
var newEvent event | |
err := json.Unmarshal(rawEvent, &newEvent) | |
if err != nil { | |
panic(err) | |
} | |
for _, change := range newEvent.Change { | |
if change.Table == *table { | |
fmt.Printf("New event to %s table, kind: %s values: %v\n", *table, change.Kind, change.ColumnValues) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment