· 9 years ago · Jan 19, 2017, 06:42 AM
1declare -a table_names=("Table_1;['t1']" "Table_2;['t1','t2']")
2
3for i in "${table_names[@]}"
4do
5
6done
7
8set -- "$i"
9IFS=";"; declare -a table_name_info=($*)
10
11echo "exists '${table_name_info[0]}'" | hbase shell > log 2>&1
12
13cat log | grep -q 'Table '${table_name_info[0]}' does exist'
14
15if [ $? = 0 ]; then
16 ## log message
17else
18 ## write script to create a table
19fi
20
21#!/bin/sh
22
23echo "Running the <$0> file"
24
25## table names
26declare -a table_names=("Table_1;['t1']" "Table_2;['t1','t2']")
27
28function tableExists {
29 echo "Table <$1> already exists."
30}
31
32function createTable {
33 echo "Table <$1> does not exist."
34 echo "Creating the Table <$1>"
35
36 echo "create '$1', $2" | hbase shell > log 2>&1
37 cat log | grep -q '0 row(s) in'
38
39 if [ $? = 0 ]; then
40 echo "Table <$1> created successfully."
41 else
42 echo "Table <$1> is not created."
43 fi
44}
45
46
47## iterating table names and checking table already exists. if does not exist creating table.
48for i in "${table_names[@]}"
49do
50 ## splitting the table based on ';'. First argument will be 'table_name' and second argument will be 'column_families'
51 set -- "$i"
52 IFS=";"; declare -a table_name_info=($*)
53
54 echo ""
55 echo "Checking the table <${table_name_info[0]}> exists or not"
56 echo ""
57
58 echo "exists '${table_name_info[0]}'" | hbase shell > log 2>&1
59 cat log | grep -q 'Table '${table_name_info[0]}' does exist'
60
61 if [ $? = 0 ]; then
62 tableExists ${table_name_info[0]}
63 else
64 createTable ${table_name_info[0]} ${table_name_info[1]}
65 fi
66done
67
68## deleting the log file.
69rm -rf log
70echo ""
71echo "done."
72
73exit