Guest User

Untitled

a guest
Nov 8th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. # Import libraries. You don't really need tidyverse, I just like working with tibbles.
  2. library(RPostgreSQL)
  3. library(tidyverse)
  4.  
  5. record_count = 1000
  6. min_date = "2015-01-01"
  7. max_date = "2018-12-31"
  8. actions = c("Button click", "Fetch data", "Page load", "Refresh")
  9. users = c("Allen", "Brian", "Charlie", "Dave", "Evan")
  10.  
  11. # Create the SQL statement.
  12. sql_statement = paste("INSERT INTO \"actions\"",
  13. "(\"action\", \"user\", \"timestamp\")",
  14. "VALUES",
  15. "($1, $2, $3)")
  16. print(paste("SQL Statement:", sql_statement))
  17.  
  18. # Connect to the database.
  19. driver = dbDriver("PostgreSQL")
  20. connection = dbConnect(driver,
  21. host = "XXX.XXX.XXX.XXX",
  22. port = 5432,
  23. user = "XXXXXXXX",
  24. password = "XXXXXXXXXXXXXXXXXXXXXX"
  25. dbname = "XXXXXXXXXXXXX")
  26.  
  27. dates = seq(from = as.POSIXct(as.Date(min_date)),
  28. to = as.POSIXct(as.Date(max_date)),
  29. by = "min")
  30.  
  31. data = tibble(action = sample(actions, record_count, repalce = TRUE),
  32. user = sample(users, record_count, replace = TRUE),
  33. timestamp = sample(dates, record_count, replace = TRUE))
  34.  
  35. for (index in seq_len(record_count)) {
  36. row = data[index,]
  37. dbExecute(connection, sql_statement, row)
  38. }
  39. print(paste("Inserted", record_count, "rows."))
  40.  
  41. # Clean up after yourself and disconnect from the database.
  42. dbDisconnect(connection)
Add Comment
Please, Sign In to add comment