Skip to content

Instantly share code, notes, and snippets.

@shanemhansen
Created June 30, 2014 23:43
Show Gist options
  • Save shanemhansen/83849c8ff287167ca990 to your computer and use it in GitHub Desktop.
Save shanemhansen/83849c8ff287167ca990 to your computer and use it in GitHub Desktop.
Read "csv"
package main
import (
"bufio"
"fmt"
"github.com/davecheney/profile"
"os"
"strings"
)
type AggregationKey struct {
Country string
Idcategory string
Iddevice string
Idos string
Date string
}
type Aggregation struct {
AggregationKey
Hits int
Value float64
}
func main() {
defer profile.Start(profile.CPUProfile).Stop()
aggregation := make(map[AggregationKey]*Aggregation)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := string(scanner.Bytes())
record := strings.Split(strings.TrimSpace(line), "|")
date := record[0][:10]
aggregationKey := AggregationKey{
record[2], record[9], record[12], record[8], date,
}
if aggregate, ok := aggregation[aggregationKey]; ok {
aggregate.Hits++
} else {
aggregation[aggregationKey] = &Aggregation{
aggregationKey,
0,
0.0,
}
}
}
fmt.Printf("Aggregated a total of >>%d<< rows from >><<\n", len(aggregation))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment