Skip to content

Instantly share code, notes, and snippets.

@kballenegger
Created April 13, 2012 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kballenegger/1253f5c5800672324e38 to your computer and use it in GitHub Desktop.
Save kballenegger/1253f5c5800672324e38 to your computer and use it in GitHub Desktop.
(ns importer.core
(:require [clojure.data.json :as json]
[monger.collection :as monger-coll]
[monger.core :as monger]
[clojure.java.io :as io]
[clojure.tools.cli :as cli])
(: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 process-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)
(println "added uuid: " uuid "app: " app "!") nil)) ; log and return nil
(def connection-info
{:host "ec2-23-21-72-55.compute-1.amazonaws.com", :port 27017})
(defn read-parallel [f]
(->> (line-seq (io/reader f))
(partition 10)
(pmap (fn [l] (map process-line l)))
dorun))
(defn import-file
"Import file"
[file]
(monger/connect! connection-info) ; connect to mongo
(monger/set-db! (monger.core/get-db "chartboost"))
(read-parallel file)) ; read file in parallel
(defn -main [& args]
(let [[options args banner]
(cli/cli args
["--file" "which file to process"])]
(import-file "head.json")))
;(import-file "head.json")
@drlivingston
Copy link

(with-open [r (io/reader file)]
  (pmap each-line (line-seq r)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment