Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @GrabConfig(systemClassLoader=true)
- @Grab(group='org.postgresql', module='postgresql', version='42.1.4')
- import groovy.sql.Sql
- if(args[0])
- {
- def db = [url:'jdbc:postgresql://localhost/postgres', user:'postgres', password:'', driver:'org.postgresql.Driver']
- def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
- def count=0
- new File(args[0]).splitEachLine(",") {fields ->
- Long globalId = Long.parseLong(fields[1]);
- String date = fields[0]
- Integer quantity = Integer.parseInt(fields[2])
- sql.execute("""INSERT INTO sft_ds.daily_sales (product_id, sale_date, quantity_sold)
- VALUES (?, to_date(?, 'YYYYMMDD'),?)
- ON CONFLICT (product_id, sale_date)
- DO NOTHING""", [globalId, date, quantity])
- count++
- }
- println "Evaluated $count many lines and inserted if did not exist in db"
- }
- else
- {
- println "Provide a csv file"
- }
Add Comment
Please, Sign In to add comment