Guest User

Untitled

a guest
Jan 22nd, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. @GrabConfig(systemClassLoader=true)
  2. @Grab(group='org.postgresql', module='postgresql', version='42.1.4')
  3.  
  4. import groovy.sql.Sql
  5.  
  6. if(args[0])
  7. {
  8. def db = [url:'jdbc:postgresql://localhost/postgres', user:'postgres', password:'', driver:'org.postgresql.Driver']
  9. def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
  10.  
  11. def count=0
  12. new File(args[0]).splitEachLine(",") {fields ->
  13. Long globalId = Long.parseLong(fields[1]);
  14. String date = fields[0]
  15. Integer quantity = Integer.parseInt(fields[2])
  16. sql.execute("""INSERT INTO sft_ds.daily_sales (product_id, sale_date, quantity_sold)
  17. VALUES (?, to_date(?, 'YYYYMMDD'),?)
  18. ON CONFLICT (product_id, sale_date)
  19. DO NOTHING""", [globalId, date, quantity])
  20. count++
  21. }
  22. println "Evaluated $count many lines and inserted if did not exist in db"
  23.  
  24.  
  25. }
  26. else
  27. {
  28. println "Provide a csv file"
  29. }
Add Comment
Please, Sign In to add comment