【定时将hbase的索引同步到solr的core,当同步失败时,回滚core】好记性不如烂笔头,我将工作中写的自动化脚本记录在此,供大家参考

前言
  此脚本不包含core的创建,创建core请移步他处。
  本贴侧重core快照的创建,快照状态查询,core的删除,从快照恢复core。
  hbase到solr的同步不做为本贴的重点。

同步脚本syn_solr.sh内容:

#!/binbash

#定义core名称list
core_list=("core1-name_shard1_replica1" "core2-name_shard1_replica1")


#定义solr服务地址
solr_point="130.10.7.109:8983"
echo "【`date "+%Y-%m-%d %H:%M:%S"`】syn solr start"


#制作core快照
#
#/solr-backups目录是已经创建的hdfs存储目录
#目录创建命令是:
#   sudo -u hdfs hdfs dfs -mkdir /solr-backups
#   sudo -u hdfs hdfs dfs -chown sorl:solr /solr-backups
#   sudo -u hdfs hdfs dfs -chmod 777 /solr-backups
#

for core_name in "${core_list[@]}"
do
  curl http://${solr_point}/solr/${core_name}/replication?command=backup\&repository=hdfs\&location=/solr-backups/${core_name} > /dev/null 2>&1
done


#快照制作状态检查
#查询快照状态执行: curl http://${solr_point}/solr/${core_name}/replication?command=details
#每10秒查询一次,快照创建成功就结束查询,最大查询次数30次(我们的数据较大,创建快照
#要一两分钟,请根据你的实际情况修改查询等待时间和次数),如仍未完成就结束整个脚本。

num=0

for core in "${core_list[@]}"
do

  while [ true  ]
  do 
    sleep 10s 
    http_result_xml=$(curl http://${solr_point}/solr/${core}/replication?command=details)
    #echo "${http_result_xml}"
  
    detail_rightString=${http_result_xml#*\<str\ name=\"status\"\>}
    detail_delStatus=${detail_rightString%%<*}

    echo ${detail_delStatus}
    if [ "success" = "${detail_delStatus}" ]
    then
      echo "【`date "+%Y-%m-%d %H:%M:%S"`】快照 ${core} 制作完成。"
      break
    fi
   
    ((num+=1))
    if [ ${num} -eq 30 ]
      then
      echo "【`date "+%Y-%m-%d %H:%M:%S"`】快照 ${core} 制作失败"
      exit
    fi

  done
done

#删除core索引数据
#
echo "【`date "+%Y-%m-%d %H:%M:%S"`】开始删除cores"
#遍历coren,并删除core的数据
for core_name in "${core_list[@]}"
do
  #当删除请求的协议状态和删除结果状态都为0时,跳出删除循环。最多循环3次。
  num=0
  while [ ${num} -lt 3 ]
  do
    #发送删除请求,并将response赋值给resultString
    resultString=`curl -X post  http://${solr_point}/solr/${core_name}/update?wt=json \
                       -H Content-Type:text/xml --data '<add><delete><query>*:*</query></delete><commit/></add>'`
    #获取curl命令返回值
    curlStatus=$?
    #echo ${curlStatus}
    #截取response中json字符串的status值
    rightString=${resultString#*\"status\":}
    delStatus=${rightString%,\"QTime\":*}
    echo ${delStatus}
    #打印完整response的json
    echo ${resultString}
    if [[ ${curlStatus} == 0 && ${delStatus} == 0 ]]
    then
      echo "【`date "+%Y-%m-%d %H:%M:%S"`${core_name}删除成功"
      break
    fi
    let num+=1
    
    if [ ${num} -eq 3  ]
    then
      echo "【`date "+%Y-%m-%d %H:%M:%S"`】删除cores失败。"
      exit
    fi

  done
done


#执行同步命令
#将hbase的数据同步到solr的core(hbase的数据每天都增量更新,所以solr的core每天也要更新)
#
indexs=("core1-name" "core2-name")
morphlines_path="/root/hbase-indexer" 
for index in "${indexs[@]}"
do
  echo "【`date "+%Y-%m-%d %H:%M:%S"`】开始同步【${index}】"
  `nohup hadoop --config /etc/hadoop/conf jar /opt/cloudera/parcels/CDH/lib/hbase-solr/tools/hbase-indexer-mr-1.5-cdh5.16.1-job.jar \
          --conf /etc/hbase/conf/hbase-site.xml --hbase-indexer-file ${morphlines_path}/hbase-indexer/${index}/conf/morphline-hbase-mapper.xml \
          --morphline-file ${morphlines_path}/morphlines.conf --zk-host master:2181,slave1:2181,slave2:2181/solr \
          --collection ${index} --go-live --reducers 0 > /dev/null 2>&1 &`

done

#如果从hbase同步失败,从solr快照恢复core数据
#
 
num=0

while [ true ]
do
  #每分钟查询一次同步状态,如果同步完成就结束while循环。
  sleep 60s
  syn_result=`yarn application -list -appStates All | grep "HBaseMapReduceIndexerTool\/HBaseIndexerMapper" | sort -n -k 1.27r | head -2 | grep SUCCEEDED | wc -l`
  echo "已同步 [${syn_result}]"

  if [ ${syn_result} == 2 ]
  then
    echo "同步成功。"
    break
  fi

  ((num+=1))
  #如果15分钟后仍没有同步成功,就从快照恢复solr的core
  #
  if [ ${num} -eq 15 ]
    then
    echo "同步失败,开始回滚..."

    for i in "${indexs[@]}"
    do
      curl http://${solr_point}/solr/${i}_shard1_replica1/replication?command=restore\&location=/solr-backups/${i}_shard1_replica1\&repository=hdfs
    done
    echo "回滚结束。"
    break
  fi

done

echo "【`date "+%Y-%m-%d %H:%M:%S"`】syn solr end"

定时任务:
每天8点50开始同步,并在/var/log/syn_solr.log中记录同步日志。

[root@slave2 ~]# crontab -l
50 8 * * * bash /root/syn_solr.sh >> /var/log/syn_solr.log 2>&1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是MapReduce导入数据到HBase的代码: import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.mapreduce.TableOutputFormat; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; public class HBaseImport { static class HBaseImportMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> { private final byte[] FAMILY = "cf1".getBytes(); private ImmutableBytesWritable outkey = new ImmutableBytesWritable(); private Put outvalue = new Put(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] words = value.toString().split(","); outkey.set(words[0].getBytes()); outvalue.addColumn(FAMILY, "name".getBytes(), words[1].getBytes()); outvalue.addColumn(FAMILY, "email".getBytes(), words[2].getBytes()); context.write(outkey, outvalue); } } public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set(TableOutputFormat.OUTPUT_TABLE, "mytable"); Job job = Job.getInstance(conf, "HBase Import"); job.setJarByClass(HBaseImport.class); job.setMapperClass(HBaseImportMapper.class); job.setOutputFormatClass(TableOutputFormat.class); job.getConfiguration() .set(TableOutputFormat.OUTPUT_TABLE, "mytable"); FileInputFormat.setInputPaths(job, new Path(args[0])); job.setOutputKeyClass(ImmutableBytesWritable.class); job.setOutputValueClass(Put.class); System.exit(job.waitForCompletion(true) ? 0 : 1); } } 希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

散人KK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值