-
-
Save devn/1e4e397ffb267b737ef9 to your computer and use it in GitHub Desktop.
refactor for kenneth
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 importer.core | |
(:require [clojure.data.json :as json] | |
[monger.collection :as monger-coll] | |
[monger.core :as monger] | |
[clojure.java.io :as io]) | |
(:import [com.mongodb Mongo DB DBObject])) | |
(defn update-unique-devices [uuid app] | |
(monger-coll/update | |
"unique_devices", | |
{:_id uuid}, | |
{"$addToSet" {:install app}}, | |
:upsert true)) | |
(defn each-line | |
"Executed for each line in the big file" | |
[line] | |
(let [decoded (json/read-json line) | |
uuid (:uuid decoded) | |
app (:app decoded)] | |
(update-unique-devices uuid app) | |
(str "added uuid: " uuid "app: " app "!\n"))) | |
(defn make-connection [host port] | |
{:host host, :port port}) | |
(def connection-info | |
(make-connection "ec2-23-21-72-55.compute-1.amazonaws.com" 27017) | |
(defn read-parallel [f] | |
(let [lazy-lines (line-seq (io/reader file))] | |
(->> (pmap each-line lazy-lines) | |
(map println)))) | |
(defn import-file-to-db | |
"Import file to database" | |
[file db] | |
(monger/connect! connection-info) ; connect to mongo | |
(monger/set-db! (monger.core/get-db db)) | |
(read-parallel file)) ; read file in parallel | |
(import-file-to-db "head.json" "chartboost") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment