Skip to content

Instantly share code, notes, and snippets.

View geoffeg's full-sized avatar
🏠
Working from home

Geoffrey Gallaway geoffeg

🏠
Working from home
View GitHub Profile
@geoffeg
geoffeg / ExampleClient.java
Created January 25, 2011 02:07
HBase Hadoop ExampleClient
import java.io.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.*;
public class ExampleClient {
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
// Create table
var util = require('util');
var fs = require('fs');
var systemConfig = (function() {
var configFilePath = __dirname + '/config.js';
var configSource = fs.readFileSync(configFilePath);
var configData = new Array();
configData = JSON.parse(configSource);
@geoffeg
geoffeg / gist:1280203
Created October 12, 2011 03:39
Futures and I do not understand each other
import java.util.Random
import akka.dispatch.Future
import akka.actor._
import Commands._
import collection.mutable.ListBuffer
object Commands {
trait Command
case class GetRandom() extends Command
@geoffeg
geoffeg / gist:1785380
Created February 10, 2012 01:44
initial mongodb fixed-array example
> db.searches.save(
{_id: 1,
"searches" : [
{"site" : "google", "q": "restaurants"},
{"site" : "yahoo", "q" : "shoes"},
{"site" : "bing", "q" : "cheap flights"}
]
}
)
> db.searches.update(
@geoffeg
geoffeg / gist:1786043
Created February 10, 2012 03:13
Separate Mongodb fixed-size initial example.
db.searches.update(
{ _id : 1 },
{ $push : { "searches" : { "site" : "blekko.com", "q" : "mongodb" } } }
);
db.searches.update(
{ _id : 1 },
{ $pop : { "searches" : -1 } }
)
@geoffeg
geoffeg / gist:1786051
Created February 10, 2012 03:15
Final MongoDB fixed-sized array example.
db.searches.remove()
db.searches.save(
{_id: 1,
"searches-size" : 3,
"searches" : [
{"site" : "google", "q": "restaurants"},
{"site" : "yahoo", "q" : "shoes"},
{"site" : "bing", "q" : "cheap flights"}
]
}
@geoffeg
geoffeg / map-cast.clj
Last active December 20, 2015 23:39
Convert a map's values into their proper type.
; Given a map where the values are numeric types contained in a string
; Convert the values to the type defined in a map of field types.
(mapv #(case ({"one" :int, "point-two" :float} (key %))
:int {(key %) (Integer/parseInt (val %))}
:float {(key %) (Float/parseFloat (val %))}
{(key %) (val %)}) ; For strings, just return the original values if there was no type defined
{"one" "1", "point-two" ".2", "three" "three"})
; Thanks to http://stackoverflow.com/questions/18202661/casting-values-of-a-map-to-their-proper-types-in-clojure
; for the basis for this, I needed to also support cases where a value in `input` was empty when the cast occured.
(def input {:a "1" :b "2.5" :c "more" :d "string" :e "keys" :f ""})
(def typetrans {:a #(Long/parseLong %) :b #(Double/parseDouble %) :f #(Integer/parseInt %)})
(defn cast-fields [csvmap]
(reduce-kv
(fn [acc k v]
@geoffeg
geoffeg / badmaps.clj
Last active December 22, 2015 21:09
Find the matching key in qparams and parammap and execute the matching parammap value with the qparam value as the argument.
(let [qparams {"cat" "meow"}
parammap {"cat" #(println "feline says" %), "dog" #(println "canine says" %)}
foundparam (into {} (filter #(contains? parammap (key %)) qparams))]
((get parammap (key (first foundparam))) (val (first foundparam))))
;> feline says meow
; Cleaner?
(let [queryparams {:cat "meow", :cheese "nonsense"}
functionmap {:cat #(println "feline says" %), :dog #(println "canine says" %)}
selectedkey (first (select-keys functionmap (keys queryparams)))]
(defn search-weather [qparams remote-addr]
; three search methods are supported:
; zipcode=XXXXX return airports within proximity to zipcode
; geo=lat,lon return airports within proximity of coords
; ip=XXX.XXX.XXX.XXX airports within geolocation of IP address
; ip=@detect airports within geolocation of client IP
(when (= (get qparams :ip) "@detect") (assoc qparams :ip remote-addr))
(let? [geoloc (get-location qparams) :is-not nil? :else (error-response "could not find location")
stations (db/find-stations (qparams :type) geoloc) :is-not nil? :else (error-response "no stations found for location")
reports (if