Created
September 15, 2011 09:53
-
-
Save karmi/1218928 to your computer and use it in GitHub Desktop.
Experiments with the HBase REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# =================================== | |
# Experiments with the HBase REST API | |
# =================================== | |
# | |
# <http://hbase.apache.org/docs/r0.20.4/api/org/apache/hadoop/hbase/rest/package-summary.html> | |
# | |
# Usage: | |
# | |
# $ brew install hbase | |
# $ hbase master start | |
# $ hbase rest start | |
# $ ./hbase-rest-examples.sh | |
# | |
# | |
function step { | |
/usr/bin/env ruby -e "puts '', %Q|\e[1m$1\e[0m|, %Q|\e[37m\e[42m|+('-'*80)+%Q|\e[0m|;" | |
} | |
function encode { | |
echo $1 | tr -d "\n" | base64 | |
} | |
function decode { | |
echo $1 | base64 -D | |
} | |
TABLE='test' | |
FAMILY='data' | |
KEY=$(encode 'row1') | |
COLUMN=$(encode 'data:test') | |
DATA=$(encode 'Some data...') | |
# echo $KEY | |
# echo $COLUMN | |
# echo $DATA | |
step "Delete table '$TABLE'" | |
# ----------------------------------------------------------------------------- | |
curl -v -X DELETE \ | |
'http://localhost:8080/test/schema' \ | |
-H "Accept: application/json" | |
step "Create table '$TABLE' with column family '$FAMILY'" | |
# ----------------------------------------------------------------------------- | |
curl -v -X PUT \ | |
'http://localhost:8080/test/schema' \ | |
-H "Accept: application/json" \ | |
-H "Content-Type: application/json" \ | |
-d '{"@name":"test","ColumnSchema":[{"name":"data"}]}' | |
step "Store value '$(decode $DATA)' in column '$(decode $COLUMN)' as row '$(decode $KEY)'" | |
# ----------------------------------------------------------------------------- | |
curl -v -X PUT \ | |
'http://localhost:8080/test/row1/data:test' \ | |
-H "Accept: application/json" \ | |
-H "Content-Type: application/json" \ | |
-d "{\"Row\":{\"@key\":\"$KEY\",\"Cell\":{\"@column\":\"$COLUMN\", \"$\":\"$DATA\"}}}" | |
step "Get row '$(decode $KEY)' from table '$TABLE'" | |
# ----------------------------------------------------------------------------- | |
curl -v -X GET \ | |
'http://localhost:8080/test/row1' \ | |
-H "Accept: application/json" | |
step "Get value from returned JSON" | |
# ----------------------------------------------------------------------------- | |
VALUE=$( curl -s -X GET -H "Accept: application/json" 'http://localhost:8080/test/row1' | ruby -ne 'puts $_[/"\$"\:"(.+)"/, 1]' | decode ) | |
echo "The returned value is: $VALUE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Karmi,
I have following query and need your help ...
I am trying to insert a row in HBase using curl and getting an error. I am unable to locate what am I doing wrong?
Scan of 'testable' table output:
hbase(main):003:0> scan 'testtable'
ROW COLUMN+CELL
firstrow column=columnfamily:message, timestamp=1403601833682, value=hello world
firstrow column=columnfamily:name, timestamp=1403843584253, value=Agrawal
firstrow column=columnfamily:number, timestamp=1403765553459, value=43
secondrow column=columnfamily:number, timestamp=1403601842745, value=42
test-key column=columnfamily:q, timestamp=1403785401271, value=value
3 row(s) in 0.1690 seconds
Curl command:
curl -X POST -H "Content-Type: application/json" -d '[{"key" : "4throw", "columnfamily":[{"name" : "Cool" , "number" : 2500 }] }]' http://localhost:9000/testtable/
Error when run with curl:
<title>Error 405 Method Not Allowed</title>HTTP ERROR 405
Problem accessing /testtable/. Reason:
Powered by Jetty://
Thanks
-Agrawal