newbie
newbie

Reputation: 321

How to get all tables of hbase in linux shell script and operate on each table?

Now I can do it with this code, but is there a better way to do this?

output=`echo "list" | hbase shell`
output=`echo ${output} | cut -d'[' -f 2 | cut -d']' -f 1`
IFS=',' read -ra  tables <<< "$output"
for tb in "${tables[@]}"; do
    echo "${tb}\n"
done

Upvotes: 1

Views: 1177

Answers (3)

rvp
rvp

Reputation: 580

This is what I did for getting all the tables of a namespace into an array in a BASH shell and perform operation on each table.

table_list_str=$(echo "list_namespace_tables '$current_ns'"  | hbase shell | 
                 sed -e '1,/TABLE/d' -e '/seconds/,$d' -e '/row/,$d')
table_list=(${table_list_str// / })
for j in "${!table_list[@]}"
do
  current_table=${table_list[j]}
  echo "$current_table" 
done

Upvotes: 0

pyXelr
pyXelr

Reputation: 9

I found that using the script suggested by Subash, did not let me execute hbase snapshot create inside the while loop for some reason. Probably because of the missing -n (non-interactive) parameter; however, adding it also did not solve this problem.

After several experiments, I ended up with this script, which let me take a snapshot of each HBase table:

set -e

# set the current time
time=$(date +'%Y%m%d-%H%M%S')

# loop through all the tables and back them up
echo "list" | sudo hbase shell -n | sed -e '1,/seconds/ d' |
while IFS='' read -r line || [[ -n "$line" ]]; do
    [ -n "$line" ] && sudo hbase snapshot create -n "${time}_${line}" -t "${line}" && echo "Backup created: ${time}_${line}"
done

In your case, you can replace the part after [ -n "$line" ] with the operation you would like to perform.

Upvotes: 0

Subash Kunjupillai
Subash Kunjupillai

Reputation: 400

You could simplify that a bit more as shown here. This involves no intermediate variable declaration, hope it helps you.

echo 'list' | hbase shell | sed -e '1,/TABLE/d' -e '/seconds/,$d' |
while IFS='' read -r line || [[ -n "$line" ]]; do
    echo "$line"
done

Upvotes: 4

Related Questions