-
-
Save fivejjs/e6a5ffaa0d257bd41607d71018f7b237 to your computer and use it in GitHub Desktop.
clojure jdbc copy into temporary table with upsert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def t (java.io.File/createTempFile "filename" ".txt")) | |
(println (.getPath t)) | |
(defn row [n] | |
[(+ n 500000) (rand-int 10000) (rand-int 10000) (rand-int 10000) (rand-int 10000) 5550555 (rand) "hello world"]) | |
(with-open [w (io/writer t)] | |
(csv/write-csv w (map row (range 1000000)))) | |
(j/with-db-transaction [tx db-spec] | |
(j/execute! tx "create temporary table x (like t)") | |
(def cm (org.postgresql.copy.CopyManager. (j/get-connection tx))) | |
(.copyIn cm "copy x (id, i, i2, i3, i4, i5, i6, i7) from stdin csv" (io/reader t)) | |
(j/execute! tx "insert into t select * from x on conflict (id) do update set i=excluded.i, i2=excluded.i2, i3=excluded.i3, i4=excluded.i4, i5=excluded.i5, i6=excluded.i6, i7=excluded.i7")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment