如何用shell脚本加载hbase表

aor9mmx1  于 2021-06-08  发布在  Hbase
关注(0)|答案(1)|浏览(320)

我是一个大数据的学习者,我正在尝试将一个文件加载到hbase表中。文件内容看起来像-

U100,A300&A301&A302    
U101,A301&A302    
U102,A302    
U103,A303&A301&A302

此文件存在于本地文件系统中。我想把这些数据加载到hbase表中-

我尝试下面的脚本,但无法得到这个确切的输出-

echo "create 'uid-map', 'users'" | hbase shell
file="/home/abc/lookupfiles/uid.txt"
touch /home/abc/lookupfiles/uid1.txt
chmod 775 /home/abc/lookupfiles/uid1.txt
file1="/home/abc/lookupfiles/uid1.txt"
awk '$1=$1' FS="&" OFS=" " $file > $file1
num=1
while IFS= read -r line
do
 uid=`echo $line | cut -d',' -f1`
 users=`echo $line | cut -d'&' -f2`
 for row in $users
 do
 #artist= 'echo $row | cut -d',' -f$num
 echo "put 'uid-map', '$uid', 'users:artist$num', '$row'" | hbase shell
 let "num=num+1"
done
num=1
done <"$file"

我的输出是-

请让我知道我做错了什么。

ha5z0ras

ha5z0ras1#

单变量优化解 Awk 课程:

echo "create 'uid-map', 'users'" | hbase shell
awk -F'[,&]' -v cmd="hbase shell" '{
        fmt="put \047uid-map\047, \047%s\047, \047users:artist%d\047, \047%s\047\n";
        for (i=2; i<=NF; i++) 
            printf(fmt, $1, ++c, $i ) | cmd; 
        c=0 
    }' "$file"

将传递给的输出 hbase shell (每次通话一行) ... | cmd ):

put 'uid-map', 'U100', 'users:artist1', 'A300'
put 'uid-map', 'U100', 'users:artist2', 'A301'
put 'uid-map', 'U100', 'users:artist3', 'A302'
put 'uid-map', 'U101', 'users:artist1', 'A301'
put 'uid-map', 'U101', 'users:artist2', 'A302'
put 'uid-map', 'U102', 'users:artist1', 'A302'
put 'uid-map', 'U103', 'users:artist1', 'A303'
put 'uid-map', 'U103', 'users:artist2', 'A301'
put 'uid-map', 'U103', 'users:artist3', 'A302'

相关问题