TOP

flume 自定义Sink之kafkaSink
2018-11-28 17:45:47 】 浏览:512
Tags:flume 定义 Sink kafkaSink

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wuxintdrh/article/details/79478747

在flume1.5.2中没有kafkasink,需要自定义KafkaSink

在fluem-1.6.0中提供了kafkasink


kafkaSink就是将Channel中的输出通过sink写到kafka,所有kafka相当与一个生产者的功能

1.1、官网的开发者文档Developer Guide

这里写图片描述

问题 1、Cannot Append to Appender! Appender either closed or not setup correctly!

这里写图片描述

1.2、自定义KafkaSink

这里写图片描述

完整代码

package com.chb.test.flume.sink;

import java.util.Properties;

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
import kafka.serializer.StringEncoder;

import org.apache.flume.Channel;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.EventDeliveryException;
import org.apache.flume.Transaction;
import org.apache.flume.conf.Configurable;
import org.apache.flume.sink.AbstractSink;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

/**
 * 自定义KafkaSink: flume整合kafka
 * @author 12285
 */
public class MyKafkaSink extends AbstractSink implements Configurable {
      private KafkaProducer<String, String> producer;

      @Override
      public void configure(Context context) {
        Properties originalProps = new Properties();
        originalProps.put("bootstrap.servers", "idc007123:9092,idc007124:9092,idc007128:9092");
        originalProps.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        originalProps.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        producer = new KafkaProducer<String,String>(originalProps);
      }


      @Override
      public Status process() throws EventDeliveryException {
        Status status = null;
        Channel ch = getChannel();
        Transaction txn = ch.getTransaction();
        txn.begin();
        try {

          Event event = ch.take();
          if(event == null) {
              status = Status.BACKOFF;
          }
          byte[] byte_message = event.getBody();
          //生产者
          ProducerRecord<String, String> record =new ProducerRecord<>("topic2", new String(byte_message)); 
          producer.send(record);

          txn.commit();
          status = Status.READY;
        } catch (Throwable t) {
          txn.rollback();
          status = Status.BACKOFF;
          if (t instanceof Error) {
            throw (Error)t;
          }
        }finally {
              txn.close();
        }
        return status;
      }
    }

依赖配置文件pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.chb.shbaobiao</groupId>
    <artifactId>TestDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>TestDemo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.flume</groupId>
            <artifactId>flume-ng-core</artifactId>
            <version>1.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka_2.10</artifactId>
            <version>0.10.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.flume</groupId>
            <artifactId>flume-ng-configuration</artifactId>
            <version>1.5.2</version>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass></mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- compiler插件, 设定JDK版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.7</source>
                    <target>1.7</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

flume 自定义Sink之kafkaSink https://www.cppentry.com/bencandy.php?fid=119&id=187199

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Flume学习12_Flume Spooling dire.. 下一篇Flume NG 基本架构及原理