Flume Spring Boot User Guide

Spring Boot Setup

Apache Flume provides the flume-spring-boot module to provide support for packaging and configuring an application using Spring Boot. Version 2.0.0 or greater of flume-spring-boot should be used.

Flume "normally" follows a paradigm where each component implements the Configurable interface and must implement the configure method to configure itself by retrieving the configuration attributes from its Context’s properties. In contrast, Spring and Spring Boot normally rely on dependency injection. where the configuration values are set into the object being configured either as constructor parameters or via setter methods.

Flume’s integration with Spring Boot provides support to configure Flume components in the application’s properties via the normal application.yml. Unlike Flume’s default method of configuring however, only component attributes are specified in the application.yml. Wiring components together, and thus defining the flows, is handled via Spring’s Java configuration.

Creating the application

Flume’s Spring Boot support provides the main class to be configured with spring as org.apache.flume.spring.boot.FlumeApplication. Flume applications that use Spring Boot should configure the Spring Boot Maven plugin with that as the main class as in:

<execution>
  <id>repackage</id>
  <goals>
    <goal>repackage</goal>
  </goals>
  <configuration>
    <executable>true</executable>
    <mainClass>org.apache.flume.spring.boot.FlumeApplication</mainClass>
  </configuration>
</execution>

Component Scanning

Spring Boot will automatically locate all the Spring components provided by Flume. However, in order for the Flume application to be configured Spring needs the configuration and package names used by the application in order for Spring to locate these components.This is accomplished in the application by providing a META-INF/spring.factories file that enables auto configuration for one class that will then provide the component scanning information for the rest of the application. For example:

META-INF/spring.factories:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sample.myapp.config.AppConfig

com.sample.config.AppConfig.java:

package com.sample.myapp.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages="com.sample.myapp")
public class MyConfiguration {

}

This will result in all classes in the com.sample.myapp package and it`s sub-packages being scanned by Spring for components to be included. Note that classes found there may also use Spring’s @Import annotation to include classes in other packages.

Component Wiring

Flume’s Spring Boot support will automatically gather all the defined Channels, SourceRunners, and SinkRunners and start them. To do that they all must first be created as Spring Singletons using the Spring @Bean annotation in a class containing the @Configuration annotation on the class declaration and then initialize them just as the "normal" FlumeApplication class does. To define these components an application should provide a Configuration class that creates these Flume components. An example configuration that generates sequence numbers, writes them to a MemoryChannel and then consumes these events without publishing them anywhere would look like:

@Configuration
@ComponentScan(basePackages="com.sample.myapp")
public class AppConfig extends AbstractFlumeConfiguration {

  @Bean
  @ConfigurationProperties(prefix = "flume.sources.source1")
  public Map<String, String> source1Properties() {
    return new HashMap<>();
  }

  @Bean
  @ConfigurationProperties(prefix = "flume.channels.channel1")
  public Map<String, String> channel1Properties() {
    return new HashMap<>();
  }

  @Bean
  public Channel memoryChannel(Map<String, String> channel1Properties) {
    return configureChannel("channel1", MemoryChannel.class, channel1Properties);
  }

  @Bean
  public SourceRunner seqSource(Channel memoryChannel, Map<String, String> source1Properties) {
    ChannelSelector selector = new ReplicatingChannelSelector();
    selector.setChannels(listOf(memoryChannel));
    return configureSource("source1", SequenceGeneratorSource.class, selector,
        source1Properties);
  }

  @Bean
  public SinkRunner nullSink(Channel memoryChannel) {
    Sink sink = configureSink("null", NullSink.class, memoryChannel,null);
    return createSinkRunner(configureSinkProcessor(null, DefaultSinkProcessor.class,
        listOf(sink)));
  }
}

The configuration for this set of components might look like:

spring:
  application:
    name: flume-test

server:
  port: 41414

flume:
  metrics: http
  sources:
    source1:
      totalEvents: 10000
  channels:
    channel1:
      capacity: 10000

This would result in an application named "flume-test" that listens on port 41414 for the /metrics endpoint. 10,000 events would be written to the channel. These events would be consumed by the NullSink. The Configuration class should extend AbstractFlumeConfiguration, as shown here, to be able to use the helper classes that construct the appropriate Flume components.

Note that all Spring Boot facilities are available to Flume applications configured this way.

SinkGroups and Sinks may also be configured in a similar fashion as in:

flume:
  sinkGroups:
    rrobin:
       backoff: true
       selector: round_robin
       "selector.maxTimeOut": 30000

  sinks:
    avroSinks:
      avroSink1:
        hostname: 192.168.10.10
        port: 4141
        batch-size: 100
        compression-type: deflate
      avroSink2:
        hostname: 192.168.10.11
        port: 4141
        batch-size: 100
        compression-type: deflate

These would be configured in the Java config with:

@Bean
@ConfigurationProperties(prefix = "flume.sink-groups.rrobin")
public Map<String, String> rrobinProperties() {
    return new HashMap<>();
}

@Bean
@ConfigurationProperties(prefix = "flume.sinks.avro-sinks")
public Map<String, AvroSinkConfiguration> avroSinksProperties() {
    return new HashMap<>();
}

@Bean
public List<Sink> avroSinks(final Channel avroFileChannel,
    final Map<String, AvroSinkConfiguration> avroSinksProperties) {
    List<Sink> sinks = new ArrayList<>();
    for (Map.Entry<String, AvroSinkConfiguration> entry : avroSinksProperties.entrySet()) {
        sinks.add(configureSink(entry.getKey(), AvroSink.class, avroFileChannel,
            entry.getValue().getProperties()));
    }
    return sinks;
}

@Bean
public SinkRunner avroSinkRunner(final Map<String, String> rrobinProperties, final List<Sink> avroSinks) {
    return createSinkRunner(
        configureSinkProcessor(rrobinProperties, LoadBalancingSinkProcessor.class, avroSinks));
}

Note that the attribute names specified for the sources, channels, and sink groups must match the attribute names specified for the components in other portions of this documentation.

It is important to note that a concrete class was used to capture the data for the Avro Sinks. When a simple Map is used Spring seems to get confused with the nested Maps. The AvroSinkConfiguration class would look like:

public class AvroSinkConfiguration {

   private String hostName;
   private int port;
   private int batchSize;
   private String compressionType;

   public String getHostName() {
      return hostName;
   }

   public void setHostName(String hostName) {
      this.hostName = hostName;
   }

   public int getPort() {
      return port;
   }

   public void setPort(int port) {
      this.port = port;
   }

   public int getBatchSize() {
      return batchSize;
   }

   public void setBatchSize(int batchSize) {
      this.batchSize = batchSize;
   }

   public String getCompressionType() {
      return compressionType;
   }

   public void setCompressionType(String compressionType) {
      this.compressionType = compressionType;
   }

   public Map<String, String> getProperties() {
      Map<String, String> map = new HashMap<>();
      map.put("hostname", hostName);
      map.put("port", Integer.toString(port));
      map.put("batchSize", Integer.toString(batchSize));
      map.put(compressionType, compressionType);
      return map;
   }
}