Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add better get-row syntax, with option hash, instead of stupid func n…

…ames
  • Loading branch information...
commit 52e7b32ceb306b280b19e644ad0e95f2cb953f62 1 parent 6de1267
@mudphone authored
View
17 src/hbase_runner/hbase/column.clj
@@ -1,5 +1,22 @@
(ns hbase-runner.hbase.column
+ (:require [clojure.contrib [str-utils :as str-utils]])
(:use [clojure.contrib.str-utils :only [re-split]]))
(defn col-qual-from-col-str [col-str]
(map #(.getBytes %1) (re-split #":" col-str 2)))
+
+(defn columns-from-coll-or-str [columns]
+ (cond
+ (nil? columns) nil
+ (coll? columns) columns
+ (string? columns) [columns]
+ :else (throw (Exception.
+ (str ":columns must be specified as a single string"
+ " column, or a collection of columns.")))))
+
+(defn col-val-entry-to-vec [col-val-entry]
+ (let [column (first col-val-entry)
+ value (second col-val-entry)
+ [family qualifier] (str-utils/re-split #":" column 2)]
+ [(.getBytes family) (.getBytes (or qualifier "")) (.getBytes value)]
+ ))
View
32 src/hbase_runner/hbase/get.clj
@@ -12,18 +12,34 @@
(.addFamily get family))
get)
+(defn add-get-cols [get cols]
+ (let [columns (columns-from-coll-or-str cols)]
+ (doseq [family-qual-str columns]
+ (add-get-col get (col-qual-from-col-str family-qual-str))))
+ get)
+
(defn get-row-with-byte-cols
"row-id: as string
columns: as collection of [family qualifier] collections"
[row-id cols-as-bytes]
(let [get (hbase-get row-id)]
- (doseq [[family qualifier] cols-as-bytes]
- (.addColumn get family qualifier))
+ (doseq [family-qual-as-bytes cols-as-bytes]
+ (add-get-col get family-qual-as-bytes))
get))
-(defn get-for [row-id cols]
- (let [get (hbase-get row-id)]
- (dorun
- (map (comp #(add-get-col get %) col-qual-from-col-str) cols))
- get
- ))
+(defn ts-vec
+ "Forces timestamp val or values into a collection.
+ This makes desctructuring easier."
+ [timestamps]
+ (if (coll? timestamps)
+ timestamps
+ [timestamps]))
+
+(defn add-get-ts
+ [get timestamps]
+ (let [[ts1 ts2 :as both-ts] (ts-vec timestamps)]
+ (if ts1
+ (if ts2
+ (.setTimeRange get (long ts1) (long ts2))
+ (.setTimeStamp get (long ts1)))))
+ get)
View
20 src/hbase_runner/hbase/scan.clj
@@ -2,15 +2,6 @@
(:import [org.apache.hadoop.hbase.client Scan])
(:use (hbase-runner.hbase column result table)))
-(defn- columns-from-coll-or-str [columns]
- (cond
- (nil? columns) nil
- (coll? columns) columns
- (string? columns) [columns]
- :else (throw (Exception.
- (str ":columns must be specified as a single string"
- " column, or a collection of columns.")))))
-
(defn- add-family-qualifier-to [scan col-str]
(let [col-qual (col-qual-from-col-str col-str)]
(if (= (count col-qual) 2)
@@ -21,11 +12,16 @@
(reduce add-family-qualifier-to scan cols))
(defn- empty-or-nil? [coll-or-symbol]
- (or (and (coll? coll-or-symbol) (empty? coll-or-symbol)) (nil? coll-or-symbol)))
+ (or
+ (and (coll? coll-or-symbol) (empty? coll-or-symbol))
+ (nil? coll-or-symbol)))
(defn- update-if-input [scan update-fn & input]
- (or (and (not-empty (remove empty-or-nil? input)) (apply (partial update-fn scan) input))
- scan))
+ (or
+ (and
+ (not-empty (remove empty-or-nil? input))
+ (apply (partial update-fn scan) input))
+ scan))
(defn- set-start-row [scan start-row]
(.setStartRow scan (.getBytes start-row)))
View
47 src/hbase_runner/hbase_repl.clj
@@ -4,7 +4,7 @@
(:import [org.apache.hadoop.hbase.client HTable])
(:require [clojure.contrib [str-utils :as str-utils]])
(:use [clojure.contrib.pprint :only [pp pprint]])
- (:use [hbase-runner.hbase get put region result scan table])
+ (:use [hbase-runner.hbase column get put region result scan table])
(:use hbase-runner.utils.clojure)
(:use hbase-runner.utils.config)
(:use hbase-runner.utils.file)
@@ -327,17 +327,11 @@
([table-name options]
(scan-table table-name (merge options {:print-only false}))))
-(defn scan [ & args ]
+(defn scan
"Print results of scan, but do not return them (to avoid using memory)."
+ [ & args ]
(apply scan-table args))
-(defn- col-val-entry-to-vec [col-val-entry]
- (let [column (first col-val-entry)
- value (second col-val-entry)
- [family qualifier] (str-utils/re-split #":" column 2)]
- [(.getBytes family) (.getBytes (or qualifier "")) (.getBytes value)]
- ))
-
(defn put-cols
([table-name row-id col-val-map]
(put-cols table-name row-id col-val-map nil))
@@ -353,6 +347,35 @@
(put-cols table-name row-id {column value} timestamp)))
(defn get-row
- [table-name row-id columns]
- (let [htable (hbase-table table-name)]
- (result-column-values-to-map (.get htable (get-for row-id columns)))))
+ "Returns row as Clojure map, by table-name, row-id, and timestamp.
+ An option hash may be provided as the optional 3rd argument.
+ Options: {
+ :column \"f1\"
+ :columns [ \"f1\" \"f2:q1\" ]
+ :timestamp 1268279340489
+ :timestamps [ 1268279340488 1268279340489 ]
+ }
+ column - family:qualifier for single column
+ columns - a vector of family:qualifier strings
+ timestamp - a single timestamp long
+ gets only values with this timestamp
+ timestamps - a vector of min and max timestamp longs
+ gets all values for selected columns in this range"
+ ([table-name row-id]
+ )
+ ([table-name row-id {:keys [column columns timestamp timestamps]
+ :or {column nil
+ columns nil
+ timestamp nil
+ timestamps []}}]
+ (let [htable (hbase-table table-name)
+ the-get (-> (hbase-get row-id)
+ (add-get-cols (columns-from-coll-or-str column))
+ (add-get-cols columns)
+ (add-get-ts timestamp)
+ (add-get-ts timestamps)
+ )]
+ (result-column-values-to-map (.get htable the-get))
+ )
+ )
+ )
Please sign in to comment.
Something went wrong with that request. Please try again.