Last active
November 2, 2021 14:56
-
-
Save alehatsman/4a602dddd448a5b5ef36cf9c09a4ca94 to your computer and use it in GitHub Desktop.
snowflake clojure access
This file contains hidden or 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
;{:paths ["src"] | |
; :deps | |
; {org.clojure/clojure {:mvn/version "1.10.3"} | |
; com.github.seancorfield/next.jdbc {:mvn/version "1.2.674"} | |
; net.snowflake/snowflake-jdbc {:mvn/version "3.13.9"}}} | |
(ns main | |
(:require [next.jdbc :as jdbc] | |
[clojure.string :as string])) | |
(import 'net.snowflake.client.jdbc.SnowflakeDriver) | |
(Class/forName "net.snowflake.client.jdbc.SnowflakeDriver") | |
(def creds | |
{:user "" | |
:db "" | |
:account "" | |
:password "" | |
;:region "eu-west-1" | |
}) | |
(defn get-connection-string [{:keys [account | |
;region | |
]}] | |
(format | |
"jdbc:snowflake://%s.snowflakecomputing.com/" | |
account | |
; region - if it aws | |
)) | |
(defn get-connection-props [{:keys [user password db account]}] | |
(doto (java.util.Properties.) | |
(.put "account" account) | |
(.put "user" user) | |
(.put "password" password) | |
(.put "db" db) | |
(.put "loginTimeout" (int 5)))) | |
(defn get-connection [{:keys [user db account password region]}] | |
(let [connection-string (get-connection-string | |
{:account account | |
:region region}) | |
connection-props (get-connection-props | |
{:account account | |
:user user | |
:password password | |
:db db})] | |
(java.sql.DriverManager/getConnection connection-string | |
connection-props))) | |
(defn test-connection [conn] | |
(let [rs (jdbc/execute! conn ["select 1"])] | |
(= (->> rs first :1) 1))) | |
(defn use-role [conn role] | |
(jdbc/execute! | |
conn | |
[(format "USE ROLE %s" role)])) | |
(def conn (get-connection creds)) | |
(test-connection conn) | |
(use-role conn "TRAINING_ROLE") | |
(jdbc/execute! | |
conn | |
["select * from SNOWFLAKE_SAMPLE_DATA.TPCH_SF1.NATION limit 10;"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment