Sunday, 12 August 2012

Using Java github repo's from Clojure

This week after watching Mike's lightning talk on bloom & count-min sketch algorithms I found myself hacking away at a Clojure implementation of a bloom filter and needing to pull in a Java project from github (a murmur hash implementation).



Here's how it's done:
  1. Add the following line to ~/.lein/profiles: [lein-git-deps "0.0.1-SNAPSHOT"] under :user :plugins, like so:
    {:user {:plugins [[lein-difftest "1.3.7"]
    [lein-marginalia "0.7.1"]
    [lein-pprint "1.1.1"]
    [lein-swank "1.4.3"]
    [lein-git-deps "0.0.1-SNAPSHOT"]]}}
    view raw profiles.clj hosted with ❤ by GitHub
  2. Form the command line run: lein deps

  3. In your projects project.clj file add the following:
    :git-dependencies [["git://github.com/tnm/murmurhash-java.git"]]
    :java-source-paths [".lein-git-deps/murmurhash-java/src/main/java/ie/ucd/murmur/"]
    view raw project.clj hosted with ❤ by GitHub
    where :git-dependencies is the github url of the repo you wish to use and :java-source-paths is the path where Leiningen will find the source code to build (note: it will be downloaded from github into a directory .lein-git-deps/ by default
  4. From the command like run: lein git-deps

  5. Add an import statement to your clojure files:
    (ns github-java-import-demo.core
    (:import (ie.ucd.murmur MurmurHash))
    (:gen-class))
    (defn -main
    "I don't do a whole lot."
    [& args]
    (println (str "Hello " (MurmurHash/hash32 "World"))))


No comments:

Post a Comment