Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "sync"
- "strconv"
- _ "github.com/lib/pq"
- "database/sql"
- "time"
- "math/rand"
- "sort"
- )
- //Create a struct that will be written to and then passed into a channel of that struct type
- //query results with result.Value and place them into db.QueryRow function to update database
- //then implement a simple method for creating purposeful errors with the database
- //test simple KNN impute algorithm against the database, as a seperate application
- //create the presentation around this
- func printLn(query string, count int, results chan<- string) {
- result := query + "_" + strconv.Itoa(count)
- // fmt.Println(result)
- results <- result
- }
- func main () {
- results := make(chan string)
- var waitGroup sync.WaitGroup
- query := "friend_ID_"
- for i := 1; i < 101; i++ {
- waitGroup.Add(1)
- go func (query string, i int) {
- printLn(query, i, results)
- waitGroup.Done()
- }(query, i)
- }
- go func() {
- // Wait for everything to be processed.
- waitGroup.Wait()
- // Close the channel to signal to the Display
- // function that we can exit the program.
- close(results)
- }()
- // Display our final results
- writeDB(results)
- }
- func writeDB (results chan string) {
- count := 0
- const (
- DB_USER = "postgres"
- DB_PASSWORD = "postgres"
- DB_NAME = "orangemvp"
- )
- dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
- DB_USER, DB_PASSWORD, DB_NAME)
- db, err := sql.Open("postgres", dbinfo)
- if err != nil {
- fmt.Println(fmt.Sprint(err))
- }
- defer db.Close()
- for result := range results {
- count++
- var friendSlice []float64
- if (count % 1000 == 0) {
- fmt.Println(result + ": " + strconv.Itoa(count))
- }
- for i := 0; i < 7; i++ {
- friendSlice = append(friendSlice, randNum())
- }
- sort.Float64s(friendSlice)
- //This can be used to simulate randomly missing values within our database
- if (count % 25000 == 0) {
- friendSlice[random(0, 6)] = 0.0
- }
- // fmt.Println(friendSlice)
- // fmt.Println(result)
- db.QueryRow("INSERT INTO friendlistrelation(friend_id, friend_1, friend_2, friend_3, friend_4, friend_5, friend_6, friend_7, created) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)", result, friendSlice[6], friendSlice[5], friendSlice[4], friendSlice[3], friendSlice[2], friendSlice[1], friendSlice[0], time.Now().Local()).Scan()
- }
- return
- }
- func randNum() (randFloat float64) {
- return rand.Float64()
- }
- func random(min, max int) int {
- rand.Seed(time.Now().Unix())
- return rand.Intn(max - min) + min
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement