Created
November 14, 2011 18:49
-
-
Save pims/1364737 to your computer and use it in GitHub Desktop.
Basic Log parser written in Go.
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 ( | |
"json"; | |
"fmt"; | |
"strconv"; | |
"bufio"; | |
"os"; | |
"flag"; | |
) | |
func EachLine(filename string) chan string { | |
output := make(chan string) | |
go func() { | |
file, err := os.Open(filename, os.O_RDONLY, 0) | |
if err != nil { | |
return | |
} | |
defer file.Close() | |
reader := bufio.NewReader(file) | |
for { | |
line, err := reader.ReadString('\n') | |
output <- line | |
if err == os.EOF { | |
break | |
} | |
} | |
close(output) | |
}() | |
return output | |
} | |
type StreamEvent struct { | |
//Account_id string | |
// Ts int | |
Items []string | |
} | |
var filename = flag.String("f", "log.txt", "parse given file") | |
// {"account_id":"1","items":["1","2","3"],"ts":1321068839} | |
// {"account_id":"2","items":["1","2","3"],"ts":1321068839} | |
// {"account_id":"3","items":["1","2","3"],"ts":1321068839} | |
// {"account_id":"4","items":["1","2","3"],"ts":1321068839} | |
// {"account_id":"5","items":["1","2","3"],"ts":1321068839} | |
func main() { | |
flag.Parse() | |
lex := make(map[string] int) | |
for line := range EachLine(*filename) { | |
var se StreamEvent | |
if len(line) == 0 { | |
continue | |
} | |
err := json.Unmarshal([]byte(line), &se) | |
if err != nil { | |
fmt.Println("error converting json:", err.String()) | |
fmt.Println("line:", line) | |
os.Exit(1) | |
} | |
for _, item := range se.Items { | |
lex[item] += 1 | |
} | |
} | |
out := bufio.NewWriter(os.Stdout) | |
for k, v := range lex { | |
out.WriteString(strconv.Itoa(v) + "," + k + "\n") | |
} | |
out.Flush() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment