Hbase mulitple operations with bash script

Hbase mulitple operations with bash script

Hbase mulitple operations with bash script or run the bulk of command from bash script
HBase latest version shell commands that provide a jruby-style object-oriented references for tables and reference variable can be used to perform the hbase operation directly in ruby shell, but there are no way to run the same into bash script.
Using bash pipe a table operation(put,get, scan…) can be spawn to hbase shell, single operation will be performed at a time and variable table referencing can’t be implemented with bash scripting.
Here we will run the table operations such as puts, scans, and admin functionality such as disabling, dropping, describing tables using bash script.
A single hbase operation can be launched from bash like this-

e.g. "create 'SAMPLE', {NAME ='UP' , VERSIONS =5}'" | hbase shell

here is example to run the bunch of commands from shell script-

  • write all hbase commands into a file-
  • 1
    2
    3
    4
    
    CREATE 'TEST', {NAME => 'SE'}
    CREATE 'SAMPLE', {NAME => 'SA'} , {NAME => 'AD' , VERSIONS => 5}
    CREATE 'SAMPLE1', {NAME => 'FM'}
    disable 'TEST'

    save all commands into a file ‘hbase.script’

  • create a bash script to read the each line of hbase script and pipe the commands to hbase shell-
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    #!/bin/bash
    #########################################
    # Name of the Script        : ddl.sh
    # No. of Input Parameters   : 2
    # Input Parameter accepted  : <command filepath> <table location in hbase> | e.g. createtables.sh /home/hadoop/ /home/hadoop/hbasem7/path/
    # Comment                   : This shell script run all the commands present in command file param $1.
    ###########################################
    usage() {
            echo "`date +%Y%m%d%H%M%S`: Usage: $0 <hbase command file> <table location>" 1>&2;
            exit 1;
    }
    if [ $# -ne 2 ]
    then
            usage;
    fi;
    STATUS=0
    file_path=$1
    ip=$(echo ${2}| sed 's/\//\\\//g')
    while read line;do
            command=$(echo ${line} | sed -e "s/#table_path#/${ip}/g")
            echo "executing command on hbase; ${command}"
            echo ${command}| hbase shell
            if [ "$?" != "0" ]; then
            echo "Error while creating tables" 1>&2
            STATUS=1;
    fi
    done <"${file_path}"
    exit ${STATUS};

find complete source at github under hbase root.

2 thoughts on “Hbase mulitple operations with bash script

  1. Vrushank Doshi

    This was pretty helpful. I need a small help.
    I am trying to delete all the tables from hbase through a script. Everytime i execute
    drop_all ‘.*’, it asks for a confirmation y/n ?

    Is there any way we can ignore this confirmation ? or ca it be handled through a shell script ?
    Your help will be highly appreciated.
    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.