ClojureでPostgreSQLのデータをMongoDBに放り込む

細かいことはさておき、とりあえずPostgreSQLのデータをMongoDBにぶち込みたい場合に使うためのClojureスクリプトを書いた。

pg2mongo.clj

(ns pg2mongo
  (:import
    (java.sql Connection DriverManager)
    (java.util ArrayList)
    (com.mongodb Mongo DBCollection BasicDBObject WriteConcern)
  )
)

; PostgreSQL config
(def _dbmsUser "joe")
(def _dbmsPass "joe")
(def _dbmsUrl "jdbc:postgresql:foo")

; MongoDB config
(def _mongoDBName "foo")
(def _mongoHost "127.0.0.1")

(.newInstance (Class/forName "org.postgresql.Driver" ))

(defn _copyData [ _conn _tableName _coll ]
  (let
    [
    _rs (.executeQuery (.prepareStatement _conn (str "select * from " _tableName) ) )
    _md (.getMetaData _rs)
    _columnCount (.getColumnCount _md)
    ]
    (while (true? (.next _rs))
      (let [ _dbObject (new BasicDBObject) ]
        (dotimes [ _i _columnCount ]
          (.put _dbObject (.getColumnName _md (inc _i)) (.getObject _rs (inc _i)))
        )
        (.insert _coll _dbObject (WriteConcern/NORMAL))
      )
    )
  )
)

(with-open
  [
  _mongo (new Mongo _mongoHost)
  _conn (DriverManager/getConnection _dbmsUrl _dbmsUser _dbmsPass)
  _rs (.executeQuery (.prepareStatement _conn "select tablename from pg_tables where schemaname = 'public'"))
  ]
  (let [ _mongoDB (.getDB _mongo _mongoDBName) ]
    (while (true? (.next _rs))
      (let
        [
        _tableName (.getString _rs 1 )
        _coll (.getCollection _mongoDB _tableName)
        ]
        (_copyData _conn _tableName _coll)
      )
    )
  )
)

(System/exit 0)

実行には以下のjarファイルが必要

  • Clojure
  • JDBC Driver
  • MongoDB Driver
  • Advertisements


    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s