Skip to content

Instantly share code, notes, and snippets.

@tariqmislam
Created April 2, 2012 14:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tariqmislam/2284007 to your computer and use it in GitHub Desktop.
Save tariqmislam/2284007 to your computer and use it in GitHub Desktop.
HBase | REST | JSON
Taken from Karmi via Gist @ gist: 1218928
#!/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
}
export TABLE='test'
export FAMILY='cf'
export KEY='row1'
export COLUMN='data:test'
export DATA='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"}]}'
echo "Store value '$DATA' in column '$COLUMN' as row '$KEY'"
# -----------------------------------------------------------------------------
curl -v -X PUT \
'http://localhost:8080/test/row1/data:test' \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d "{\"Row\":{\"@key\":\"row1\",\"Cell\":{\"@column\":\"data:test\", \"$\":\"awesomedataaaa\"}}}"
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