core.clj 1.23 KB
Newer Older
Edward J. Stembler's avatar
Edward J. Stembler committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
(ns emr-hive-jdbc-example.core
  (:require [clojure.tools.logging :as log])
  (:require [clojure.java.jdbc :as jdbc])
  (:gen-class))

;; Define the user/password/subname from ENV variables
(def user (System/getenv "EMR_HIVE_JDBC_USER"))
(def password (System/getenv "EMR_HIVE_JDBC_PASSWORD"))
(def subname (System/getenv "EMR_HIVE_JDBC_SUBNAME"))

;; Preconditions
(when (nil? user)
  (throw (RuntimeException. "user is nil")))
(when (nil? password)
  (throw (RuntimeException. "password is nil")))
(when (nil? subname)
  (throw (RuntimeException. "subname is nil")))

;; Define the connection specification
Edward J. Stembler's avatar
Edward J. Stembler committed
20
(def hive-spec {:classname "com.amazon.hive.jdbc41.HS2Driver"}
Edward J. Stembler's avatar
Edward J. Stembler committed
21 22 23
               :subprotocol "hive2"
               :subname subname
               :user user
Edward J. Stembler's avatar
Edward J. Stembler committed
24
               :password password)
Edward J. Stembler's avatar
Edward J. Stembler committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38

;; Define the SQL statement to execute
(def sql ["SELECT
               COUNT(*) AS row_count
           FROM
               my_table"]) ; TODO: Change to your desired SQL statement

;; The main entry-point
;; 1. Logs the SQL to execute
;; 2. Executes the SQL
;; 3. Returns the row_count value of the first row
(defn -main
  [& args]
  (log/info "Executing SQL:\n" (first sql))
Edward J. Stembler's avatar
Edward J. Stembler committed
39
  (println (jdbc/query hive-spec sql {:row-fn :row_count :result-set-fn first})))