Skip to content
Browse files

Add put test with and without specified version timestamp

  • Loading branch information...
1 parent 0e8d640 commit a81e8d6f3ac9b004115f27c1698f3f8b7a917e39 @mudphone committed Jan 30, 2010
View
15 src/hbase_runner/hbase/put.clj
@@ -1,8 +1,13 @@
(ns hbase-runner.hbase.put
(:import [org.apache.hadoop.hbase.client Put]))
-(defn put-for-row [row-id columns-and-values]
- (let [put (Put. (.getBytes row-id))]
- (doseq [[family qualifier value] columns-and-values]
- (.add put family qualifier value))
- put))
+(defn put-for-row
+ ([row-id columns-and-values]
+ (put-for-row row-id columns-and-values nil))
+ ([row-id columns-and-values timestamp]
+ (let [put (Put. (.getBytes row-id))]
+ (if timestamp
+ (.setTimeStamp put timestamp))
+ (doseq [[family qualifier value] columns-and-values]
+ (.add put family qualifier value))
+ put)))
View
19 src/hbase_runner/hbase_repl.clj
@@ -4,9 +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.region)
- (:use hbase-runner.hbase.scan)
- (:use hbase-runner.hbase.table)
+ (:use [hbase-runner.hbase put region scan table])
(:use hbase-runner.utils.clojure)
(:use hbase-runner.utils.config)
(:use hbase-runner.utils.file)
@@ -309,14 +307,25 @@
(defn disable-region [region-name]
(online region-name true))
-(defn scan!
+(defn scan-results
"Return results of scan (which may use a lot of memory).
See 'scan' for print-only version of this function."
([table-name]
- (scan! table-name {}))
+ (scan-results table-name {}))
([table-name options]
(scan-table table-name (merge options {:print-only false}))))
(defn scan [ & args ]
"Print results of scan, but do not return them (to avoid using memory)."
(apply scan-table args))
+
+(defn put
+ ([table-name row-id column value]
+ (put table-name row-id column value nil))
+ ([table-name row-id column value timestamp]
+ (let [htable (hbase-table table-name)
+ [family qualifier] (str-utils/re-split #":" column 2)
+ column-and-value [(.getBytes family)
+ (.getBytes (or qualifier ""))
+ (.getBytes value)]]
+ (.put htable (put-for-row row-id [column-and-value] timestamp)))))
View
22 test/hbase_runner/hbase_repl_spec.clj
@@ -1,6 +1,6 @@
(ns hbase-runner.hbase-repl-spec
(:require [clojure.contrib.java-utils :as java-utils])
- (:use [clojure.test :only [run-tests deftest is]])
+ (:use [clojure.test :only [deftest is run-tests testing]])
(:use hbase-runner.spec-helper)
(:use hbase-runner.hbase-repl)
(:use hbase-runner.utils.config)
@@ -51,4 +51,22 @@
(is (= "hbr_spec_t1" (:name
(first
(read-clojure-lines-from test-file-path))))))
-)))
+ )))
+
+(deftest put-test
+ (let [table-name (ns-table-name :t1)]
+ (with-test-tables [table-name]
+ (truncate-table table-name)
+ (testing "put, with default timestamp version"
+ (put table-name, "test-row-id1", "f1", "123")
+ (let [results (scan-results table-name)
+ result (first results)]
+ (is (= 1 (count results)))
+ (is (= "123" (ffirst (vals (get-in result ["test-row-id1" "f1" ""])))))))
+
+ (testing "put, with custom timestamp version"
+ (put table-name, "test-row-id2", "f1", "456" (long 111222333))
+ (let [results (scan-results table-name)
+ result (second results)]
+ (is (= 2 (count results)))
+ (is (= "456" (first (get-in result ["test-row-id2" "f1" "" (long 111222333)])))))))))
View
6 test/hbase_runner/spec_helper.clj
@@ -1,12 +1,14 @@
(ns hbase-runner.spec-helper
(:require [clojure.contrib.java-utils :as java-utils])
- (:import [org.apache.hadoop.hbase HTableDescriptor])
+ (:import [org.apache.hadoop.hbase HColumnDescriptor HTableDescriptor])
(:use hbase-runner.hbase-repl)
(:use hbase-runner.utils.config))
(defn create-table-if-does-not-exist [table-name]
(if-not (table-exists? table-name)
- (let [table-descriptor (HTableDescriptor. table-name)]
+ (let [table-descriptor (HTableDescriptor. table-name)
+ col-descriptor (HColumnDescriptor. (.getBytes "f1"))]
+ (.addFamily table-descriptor col-descriptor)
(.createTable *HBaseAdmin* table-descriptor)
(println "Created table" table-name))))

0 comments on commit a81e8d6

Please sign in to comment.
Something went wrong with that request. Please try again.