Author: zsxwing <zsxwing@gmail.com> Closes #6830 from zsxwing/flume-python and squashes the following commits: 78dfdac [zsxwing] Fix the compile error in the test code f1bf3c0 [zsxwing] Address TD's comments 0449723 [zsxwing] Add sbt goal streaming-flume-assembly/assembly e93736b [zsxwing] Fix the test case for determine_modules_to_test 9d5821e [zsxwing] Fix pyspark_core dependencies f9ee681 [zsxwing] Merge branch 'master' into flume-python 7a55837 [zsxwing] Add streaming_flume_assembly to run-tests.py b96b0de [zsxwing] Merge branch 'master' into flume-python ce85e83 [zsxwing] Fix incompatible issues for Python 3 01cbb3d [zsxwing] Add import sys 152364c [zsxwing] Fix the issue that StringIO doesn't work in Python 3 14ba0ff [zsxwing] Add flume-assembly for sbt building b8d5551 [zsxwing] Merge branch 'master' into flume-python 4762c34 [zsxwing] Fix the doc 0336579 [zsxwing] Refactor Flume unit tests and also add tests for Python API 9f33873 [zsxwing] Add the Python API for Flume
9.5 KiB
layout | title |
---|---|
global | Spark Streaming + Flume Integration Guide |
Apache Flume is a distributed, reliable, and available service for efficiently collecting, aggregating, and moving large amounts of log data. Here we explain how to configure Flume and Spark Streaming to receive data from Flume. There are two approaches to this.
Python API Flume is not yet available in the Python API.
Approach 1: Flume-style Push-based Approach
Flume is designed to push data between Flume agents. In this approach, Spark Streaming essentially sets up a receiver that acts an Avro agent for Flume, to which Flume can push the data. Here are the configuration steps.
General Requirements
Choose a machine in your cluster such that
-
When your Flume + Spark Streaming application is launched, one of the Spark workers must run on that machine.
-
Flume can be configured to push data to a port on that machine.
Due to the push model, the streaming application needs to be up, with the receiver scheduled and listening on the chosen port, for Flume to be able push data.
Configuring Flume
Configure Flume agent to send data to an Avro sink by having the following in the configuration file.
agent.sinks = avroSink
agent.sinks.avroSink.type = avro
agent.sinks.avroSink.channel = memoryChannel
agent.sinks.avroSink.hostname = <chosen machine's hostname>
agent.sinks.avroSink.port = <chosen port on the machine>
See the Flume's documentation for more information about configuring Flume agents.
Configuring Spark Streaming Application
-
Linking: In your SBT/Maven projrect definition, link your streaming application against the following artifact (see Linking section in the main programming guide for further information).
groupId = org.apache.spark artifactId = spark-streaming-flume_{{site.SCALA_BINARY_VERSION}} version = {{site.SPARK_VERSION_SHORT}}
-
Programming: In the streaming application code, import
FlumeUtils
and create input DStream as follows.import org.apache.spark.streaming.flume._val flumeStream = FlumeUtils.createStream(streamingContext, [chosen machine's hostname], [chosen port])
import org.apache.spark.streaming.flume.*;JavaReceiverInputDStream<SparkFlumeEvent> flumeStream = FlumeUtils.createStream(streamingContext, [chosen machine's hostname], [chosen port]);
from pyspark.streaming.flume import FlumeUtilsflumeStream = FlumeUtils.createStream(streamingContext, [chosen machine's hostname], [chosen port])
By default, the Python API will decode Flume event body as UTF8 encoded strings. You can specify your custom decoding function to decode the body byte arrays in Flume events to any arbitrary data type. See the API docs and the example.
Note that the hostname should be the same as the one used by the resource manager in the cluster (Mesos, YARN or Spark Standalone), so that resource allocation can match the names and launch the receiver in the right machine.
-
Deploying: Package
spark-streaming-flume_{{site.SCALA_BINARY_VERSION}}
and its dependencies (exceptspark-core_{{site.SCALA_BINARY_VERSION}}
andspark-streaming_{{site.SCALA_BINARY_VERSION}}
which are provided byspark-submit
) into the application JAR. Then usespark-submit
to launch your application (see Deploying section in the main programming guide).
Approach 2: Pull-based Approach using a Custom Sink
Instead of Flume pushing data directly to Spark Streaming, this approach runs a custom Flume sink that allows the following.
- Flume pushes data into the sink, and the data stays buffered.
- Spark Streaming uses a reliable Flume receiver and transactions to pull data from the sink. Transactions succeed only after data is received and replicated by Spark Streaming.
This ensures stronger reliability and fault-tolerance guarantees than the previous approach. However, this requires configuring Flume to run a custom sink. Here are the configuration steps.
General Requirements
Choose a machine that will run the custom sink in a Flume agent. The rest of the Flume pipeline is configured to send data to that agent. Machines in the Spark cluster should have access to the chosen machine running the custom sink.
Configuring Flume
Configuring Flume on the chosen machine requires the following two steps.
-
Sink JARs: Add the following JARs to Flume's classpath (see Flume's documentation to see how) in the machine designated to run the custom sink .
(i) Custom sink JAR: Download the JAR corresponding to the following artifact (or direct link).
groupId = org.apache.spark artifactId = spark-streaming-flume-sink_{{site.SCALA_BINARY_VERSION}} version = {{site.SPARK_VERSION_SHORT}}
(ii) Scala library JAR: Download the Scala library JAR for Scala {{site.SCALA_VERSION}}. It can be found with the following artifact detail (or, direct link).
groupId = org.scala-lang artifactId = scala-library version = {{site.SCALA_VERSION}}
(iii) Commons Lang 3 JAR: Download the Commons Lang 3 JAR. It can be found with the following artifact detail (or, direct link).
groupId = org.apache.commons artifactId = commons-lang3 version = 3.3.2
-
Configuration file: On that machine, configure Flume agent to send data to an Avro sink by having the following in the configuration file.
agent.sinks = spark agent.sinks.spark.type = org.apache.spark.streaming.flume.sink.SparkSink agent.sinks.spark.hostname = <hostname of the local machine> agent.sinks.spark.port = <port to listen on for connection from Spark> agent.sinks.spark.channel = memoryChannel
Also make sure that the upstream Flume pipeline is configured to send the data to the Flume agent running this sink.
See the Flume's documentation for more information about configuring Flume agents.
Configuring Spark Streaming Application
-
Linking: In your SBT/Maven project definition, link your streaming application against the
spark-streaming-flume_{{site.SCALA_BINARY_VERSION}}
(see Linking section in the main programming guide). -
Programming: In the streaming application code, import
FlumeUtils
and create input DStream as follows.import org.apache.spark.streaming.flume._val flumeStream = FlumeUtils.createPollingStream(streamingContext, [sink machine hostname], [sink port])
import org.apache.spark.streaming.flume.*;JavaReceiverInputDStream<SparkFlumeEvent>flumeStream = FlumeUtils.createPollingStream(streamingContext, [sink machine hostname], [sink port]);
from pyspark.streaming.flume import FlumeUtilsaddresses = [([sink machine hostname 1], [sink port 1]), ([sink machine hostname 2], [sink port 2])] flumeStream = FlumeUtils.createPollingStream(streamingContext, addresses)
By default, the Python API will decode Flume event body as UTF8 encoded strings. You can specify your custom decoding function to decode the body byte arrays in Flume events to any arbitrary data type. See the API docs.
See the Scala example FlumePollingEventCount.
Note that each input DStream can be configured to receive data from multiple sinks.
-
Deploying: Package
spark-streaming-flume_{{site.SCALA_BINARY_VERSION}}
and its dependencies (exceptspark-core_{{site.SCALA_BINARY_VERSION}}
andspark-streaming_{{site.SCALA_BINARY_VERSION}}
which are provided byspark-submit
) into the application JAR. Then usespark-submit
to launch your application (see Deploying section in the main programming guide).