http sink自定义

 

问题:httpsink到终端接口依然用flume 的httpsource收集数据发现拿不到数据。发现源码的httpsink之后只保留了body的value其他信息都没了这里自定义一下,也可将header封装回去。

package test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import okhttp3.*;
import org.apache.flume.*;
import org.apache.flume.conf.Configurable;
import org.apache.flume.sink.AbstractSink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpSink extends AbstractSink implements Configurable {
   private static Logger LOG = LoggerFactory.getLogger(HttpSink.class);
   private String hostname;
   private String port;
   private String url;
   private int batchSize;
   private String postUrl;
   public HttpSink(){
      LOG.info("HttpSink start....");
   }

   @Override
   public void configure(Context context) {
      hostname = context.getString("hostname");
      Preconditions.checkNotNull("hostname","hostname must not be null!");
      port = context.getString("port");
      Preconditions.checkNotNull("port","port must not be null!");
      batchSize = context.getInteger("batchSize",100);
      Preconditions.checkArgument(batchSize>0,"batchSize must be a positive number!");
      url = context.getString("url");
      postUrl = "http://"+hostname+":"+port+url;
   }

   @Override
   public Status process() throws EventDeliveryException {
      Status result = Status.READY;
      Channel channel = getChannel();
      Transaction transaction = null;
      try {
         transaction = channel.getTransaction();
         transaction.begin();
         Event event = null;
         String content = null;

         List<Map<String,String>> contents = new ArrayList<>();
         for(int i = 0 ;i < batchSize;i++){
            Map<String,String> content1= new HashMap<>();
            event = channel.take();
            if(null != event){
               //对事件进行处理,源码对事件只取了body,如果,http sink的终端是flume采集的,会收集不到信息,这里再加上body,也可再加上其他的封装,header都可以通过event拿到。
               content = new String(event.getBody());
               if(null != content && !"".equals(content)){
                  content1.put("body",content);
                  contents.add(content1);
//                contents.add(JSONObject.toJSON(content1).toString());
               }
            }else {
               result = Status.BACKOFF;
               break;
            }
         }
         if(contents.size() > 0){
            Response response = postJson(postUrl, JSONArray.toJSON(contents).toString());
            if(null != response && response.isSuccessful()){
               //commit 机制确保数据并不会丢失
               transaction.commit();
            }
         }else {
            transaction.commit();
         }
      }catch (Exception e){
         try {
            if(null != transaction){
               transaction.rollback();
            }
         }catch (Exception e2){
            LOG.error("Exception in rollback. Rollback might not be successful." + e2);
         }
         LOG.error("Failed to commit transaction." + e);
         Throwables.propagate(e);

      }finally {
         if(null != transaction){
            transaction.close();
            LOG.debug("close Transaction");
         }
      }
      return result;
   }

   /**
    * post 请求,body为json数据
    * @param url
    * @param json
    * @return
    */
   private Response postJson(String url, String json){
      OkHttpClient client = new OkHttpClient();
      RequestBody body = RequestBody.create(MediaType.parse("application/json"),json);
      Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
      Response response = null;
      try {
         response = client.newCall(request).execute();
         if(!response.isSuccessful()){
            LOG.info("request was error!");
         }
      }catch (IOException e){
         LOG.error("IOException in postJson." + e);
         e.printStackTrace();
      }

      return response;
   }

   @Override
   public synchronized void stop() {
      super.stop();
   }

   @Override
   public synchronized void start() {
      super.start();
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值