flume高并发优化——(10)消灭elasticsearch sink多次插入

本文介绍了在使用Flume处理JSON数据时遇到的问题,即Elasticsearch Sink在出现错误后会不断尝试并重复插入数据,导致数据不一致。解决方案包括在Elasticsearch的批量操作中不进行回滚,并且当插入出错时仅做报警,而非强事务处理。通过修改Flume源码,实现了这一优化策略。
摘要由CSDN通过智能技术生成

在flume作为通道接收json数据时,最近遇到一个问题,当flume-es-sink遭遇一个错误的时候,会不断尝试插入数据,而以前的数据又没有进行回滚,导致数据重复插入,脏数据累积,为了解决这个问题,现解决如下:


原因如下:

        1,事务控制在channel端

        2,事务回滚,未处理已插入es中数据


解决方案:

        1,es批量操作不做回滚

        2,es插入出错,只做报警(日志业务,不要求强事务)


总结:

        在解决问题的过程中,解决问题的关键在于两点:
1,分析错误数据
2,关键点日志分析
找到这两个点,解决问题基本就可以定位,在以后的工作生涯中,我们要沉住气,不要怕,为了以后不犯错,保留犯错现场。



修改flume源码如下

关键代码:

 } catch (Throwable ex) {

      logger.error("=ElasticSearchSink=>has error",ex);
      try {
        txn.commit();
        sinkCounter.addToEventDrainSuccessCount(0);
        counterGroup.incrementAndGet("transaction.success");
      } catch (Exception ex2) {
        logger.error(
            "=ElasticSearchSink.counterGroup.incrementAndGet=> has exception.",
            ex2);
      }
    }



elasticsearch-sink:

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.flume.sink.elasticsearch;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import org.apache.commons.lang.StringUtils;
import org.apache.
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值