-
-
Save dpick/15dcd98167c099a356c6 to your computer and use it in GitHub Desktop.
Clojure postgres copy table
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
(ns backfill-spike.core | |
(:require [byte-streams :as bs] | |
[clojure.java.jdbc :as jdbc] | |
[clojure.string :as string] | |
[clojure.java.io :as io] | |
[environ.core :refer [env]]) | |
(import org.postgresql.copy.CopyManager) | |
(import org.postgresql.copy.PGCopyInputStream) | |
(import org.postgresql.util.PSQLException) | |
(import java.io.IOException) | |
(import [backfill_spike PGCopy]) | |
(:gen-class)) | |
(def db-spec | |
{:subprotocol "postgresql" | |
:subname (str "//" (env :db-host) ":" (env :db-port) "/" (env :db-name)) | |
:user (env :db-user) | |
:password (env :db-password)}) | |
(def conn (jdbc/get-connection db-spec)) | |
(defn copy-table [table-name] | |
(let [is (PGCopyInputStream. conn (str "COPY " table-name " TO STDOUT (DELIMITER '|')")) | |
br (java.io.BufferedReader. (java.io.InputStreamReader. is))] | |
(doall | |
(doseq [row (line-seq br)] | |
(prn row))))) | |
(defn -main [& args] | |
(copy-table "foo")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment