Skip to content

Instantly share code, notes, and snippets.

@hivefans
Forked from karmi/hbase-rest-examples.sh
Last active March 17, 2020 02:03
Show Gist options
  • Save hivefans/230bcf20cca25e47b3aaebf947fc89c4 to your computer and use it in GitHub Desktop.
Save hivefans/230bcf20cca25e47b3aaebf947fc89c4 to your computer and use it in GitHub Desktop.
Experiments with the HBase REST API|-|{"files":{"hbase-rest-examples.sh":{"env":"plain"}},"tag":"bigdata"}
#!/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 "Store multiple value '$(decode $DATA)' in column '$(decode $COLUMN)' as row '$(decode $KEY)'"
# -----------------------------------------------------------------------------
curl -v -X PUT \
'http://localhost:8080/test/false-row-key' \
-H "Content-Type: application/json" \
--data "{\"Row\":[{\"@key\":\"$KEY1\",\"Cell\":{\"@column\":\"$COLUMN\", \"$\":\"$DATA1\"}},{\"@key\":\"$KEY2\",\"Cell\":{\"@column\":\"$COLUMN\", \"$\":\"$DATA2\"}}]}"
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