load_users.go 905 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package main
  2. import (
  3. "bufio"
  4. "encoding/csv"
  5. "fmt"
  6. "io"
  7. "log"
  8. "os"
  9. "github.com/gomodule/redigo/redis"
  10. rg "github.com/maguec/redisgraph-go"
  11. )
  12. func main() {
  13. csvFile, _ := os.Open("random_reddit_ids")
  14. reader := csv.NewReader(bufio.NewReader(csvFile))
  15. reader.Comma = '\t'
  16. reader.LazyQuotes = true
  17. conn, _ := redis.Dial("tcp", "localhost:6379")
  18. graph := rg.Graph{}.New("Reddit", conn)
  19. batchCounter := 0
  20. for {
  21. line, error := reader.Read()
  22. if error == io.EOF {
  23. break
  24. } else if error != nil {
  25. log.Print(error)
  26. } else {
  27. p := &rg.Node{
  28. Label: "User",
  29. Properties: map[string]interface{}{
  30. "RedditId": fmt.Sprintf("u%d", batchCounter),
  31. "Name": line[0],
  32. },
  33. }
  34. graph.AddNode(p)
  35. if batchCounter%100 == 0 {
  36. graph.Flush()
  37. fmt.Println("Records Added:", batchCounter)
  38. }
  39. }
  40. batchCounter = batchCounter + 1
  41. }
  42. graph.Flush()
  43. conn.Close()
  44. }