天天看點

批量導入千萬資料到Redis叢集處理方案,shell腳本處理(使用pipe處理)

從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
           

改進版如下:

#!/bin/bash

cd /home/jthx
param_date=`date -d '-2 days' +%Y%m%d`
/apprun/redis.20200206/bin/redis-cli -h 172.26.xxx.xx -a j+VXNOuMA6E= 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`
cd res
crc_slot=`hive -e "select crc16('recommend:hot:list');" | grep -v "WARN"`
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]}
    if (($crc_slot>=$slot1)) && (($crc_slot<=$slot2));then
      hive -e "
      set hive.execution.engine=mr;
      select concat('*3\n', '$', '3\n', 'set\n', '$', length(key), '\n', key, '\n', '$', length(value), '\n', value) from
      (select concat('recommend:hot:list') as key, concat('\"', concat_ws(',', collect_set(prod_id)), '\"') as value from
      (select * from jthx_db.t_dwa_hjq_prodhot_score_ad where dayid=${param_date} order by hot_score desc limit 300)t1)t;"  | grep -v "WARN" > hot.data
      unix2dos hot.data
      cat "related_"$slot1"_"$slot2.data | /usr/local/bin/redis-cli -h $ip -p $port -a xxxxxxx --pipe
    fi
  done
           

繼續閱讀