從hive查詢數據,使用shell腳本導入到Redis。
樣例如下:
#!/bin/bash
/usr/local/bin/redis-cli -h 172.28.xx.xx -a xxxxx cluster nodes | grep master > info.txt
sed -i 's/@/:/g' info.txt
sed -i 's/-/:/g' info.txt
info=`cat info.txt | awk '{print $2":"$9}' | awk -F ':' '{print $1" "$2" "$4" "$5}'`
array=(${info// / })
len=${#array}
cnt=`expr $len / 4`
for((i=0;i<$cnt;i++));
do
a=`expr $i \* 4`
b=`expr $a + 1`
c=`expr $a + 2`
d=`expr $a + 3`
ip=${array[$a]}
port=${array[$b]}
slot1=${array[$c]}
slot2=${array[$d]}
hive -e "select concat('*3\n','$','3\n','set\n','$',length(key),'\n',key,'\n','$',length(value),'\n',value) from (select concat('key1:phone:', user_id) as key, concat('\"', pro_id,'\"') as value from databaseName.table_name where crc16(concat('key1:phone:', user_id)) >= $slot1 and crc16(concat('key1:phone:', user_id)) <= $slot2)t;" | grep -v "^WARN" > $slot1"_"$slot2".data"
unix2dos $slot1"_"$slot2".data"
cat $slot1"_"$slot2".data" | /usr/local/bin/redis-cli -h $ip -p $port -a xxxxx--pipe
done