欢迎光临
我们一直在努力

Flume 与 Spark Streaming 实时集成:Direct 方式与 Receiver 方式的架构差异

Flume 与 Spark Streaming 实时集成:Direct 方式与 Receiver 方式的架构差异

1. 集成方式概述

Flume 作为 Apache 生态中常用的日志收集工具,与 Spark Streaming 的结合可以实现强大的实时数据处理能力。在实际应用中,主要有两种集成方式:Receiver 方式和 Direct 方式,它们在架构设计、数据流处理和性能表现上存在显著差异。

Receiver 方式采用传统的事件驱动模型,而 Direct 方式则采用更加高效的 Pull 模型,这两种方式各有优劣,适用于不同的业务场景。

2. Receiver 方式架构解析

2.1 工作原理

Receiver 方式下,Spark Streaming 应用启动一个长期运行的 Receiver,该 Receiver 作为 Flume 的 Sink 接收数据。Flume 将数据推送到 Receiver,Spark Streaming 通过 Receiver 接收到数据后,将其存入 Spark 内存中,并由 Spark Streaming 的微批处理机制进行处理。

2.2 实现方式

首先需要配置 Flume,使其指向 Spark Streaming 的 Avro Sink:

# Flume 配置示例
a1.sources = r1
a1.sinks = k1
a1.channels = c1

a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /var/log/mysqld.log

a1.sinks.k1.type = avro
a1.sinks.k1.hostname = localhost
a1.sinks.k1.port = 9999

a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

在 Spark Streaming 应用中配置 Receiver:

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.streaming.flume.FlumeUtils

object FlumeReceiverStream {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("FlumeReceiverStream").setMaster("local[2]")
val ssc = new StreamingContext(conf, Seconds(10))

// 创建 Flume Receiver
val flumeStream = FlumeUtils.createStream(ssc, "localhost", 9999)

// 处理数据
flumeStream.flatMap(e => new String(e.event.getBody.array()).split(" "))
.map(word => (word, 1))
.reduceByKey(_ + _)
.print()

ssc.start()
ssc.awaitTermination()
}
}

2.3 优缺点分析

优点:

  • 实现简单,易于理解和部署
  • 与 Flume 生态无缝集成
  • 支持多种数据源类型

缺点:

  • 存在数据丢失风险,因为数据先存入 Receiver 内存,尚未持久化
  • 容错能力有限,Receiver 故障可能导致数据丢失
  • 资源消耗较高,需要为 Receiver 单独分配资源

2.4 架构图

#publish-mermaid-1788106699330-0{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1788106699330-0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1788106699330-0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1788106699330-0 .error-icon{fill:#552222;}#publish-mermaid-1788106699330-0 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1788106699330-0 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1788106699330-0 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1788106699330-0 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1788106699330-0 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1788106699330-0 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1788106699330-0 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1788106699330-0 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1788106699330-0 .marker.cross{stroke:#333333;}#publish-mermaid-1788106699330-0 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1788106699330-0 p{margin:0;}#publish-mermaid-1788106699330-0 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1788106699330-0 .cluster-label text{fill:#333;}#publish-mermaid-1788106699330-0 .cluster-label span{color:#333;}#publish-mermaid-1788106699330-0 .cluster-label span p{background-color:transparent;}#publish-mermaid-1788106699330-0 .label text,#publish-mermaid-1788106699330-0 span{fill:#333;color:#333;}#publish-mermaid-1788106699330-0 .node rect,#publish-mermaid-1788106699330-0 .node circle,#publish-mermaid-1788106699330-0 .node ellipse,#publish-mermaid-1788106699330-0 .node polygon,#publish-mermaid-1788106699330-0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788106699330-0 .rough-node .label text,#publish-mermaid-1788106699330-0 .node .label text,#publish-mermaid-1788106699330-0 .image-shape .label,#publish-mermaid-1788106699330-0 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1788106699330-0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1788106699330-0 .rough-node .label,#publish-mermaid-1788106699330-0 .node .label,#publish-mermaid-1788106699330-0 .image-shape .label,#publish-mermaid-1788106699330-0 .icon-shape .label{text-align:center;}#publish-mermaid-1788106699330-0 .node.clickable{cursor:pointer;}#publish-mermaid-1788106699330-0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1788106699330-0 .arrowheadPath{fill:#333333;}#publish-mermaid-1788106699330-0 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1788106699330-0 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1788106699330-0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788106699330-0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1788106699330-0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788106699330-0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1788106699330-0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1788106699330-0 .cluster text{fill:#333;}#publish-mermaid-1788106699330-0 .cluster span{color:#333;}#publish-mermaid-1788106699330-0 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-1788106699330-0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1788106699330-0 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1788106699330-0 .icon-shape,#publish-mermaid-1788106699330-0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1788106699330-0 .icon-shape p,#publish-mermaid-1788106699330-0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1788106699330-0 .icon-shape .label rect,#publish-mermaid-1788106699330-0 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1788106699330-0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1788106699330-0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1788106699330-0 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1788106699330-0 [data-look=\”neo\”].node rect,#publish-mermaid-1788106699330-0 [data-look=\”neo\”].cluster rect,#publish-mermaid-1788106699330-0 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788106699330-0 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1788106699330-0 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1788106699330-0 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788106699330-0 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1788106699330-0 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788106699330-0 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1788106699330-0 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788106699330-0 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1788106699330-0 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}数据推送数据推送数据传输数据存储微批处理结果输出

Flume Source

Flume Channel

Flume Avro Sink

Spark Receiver

Spark Memory

Spark Streaming Processing

Spark Sink

3. Direct 方式架构解析

3.1 工作原理

Direct 方式采用 Pull 模型,Spark Streaming 应用直接作为 Flume 的 Source,主动从 Flume 拉取数据。这种方式下,数据直接从 Flume 推送到 Kafka,然后 Spark Streaming 从 Kafka 拉取数据进行处理,避免了中间 Receiver 节点。

3.2 实现方式

首先配置 Flume,使其数据发送到 Kafka:

# Flume 配置示例
a1.sources = r1
a1.sinks = k1
a1.channels = c1

a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /var/log/mysqld.log

a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.kafka.bootstrap.servers = localhost:9092
a1.sinks.k1.kafka.topic = flume-log
a1.sinks.k1.kafka.producer.acks = 1

a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

在 Spark Streaming 应用中配置 Direct 消费 Kafka:

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.streaming.kafka.KafkaUtils

object FlumeDirectStream {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("FlumeDirectStream").setMaster("local[2]")
val ssc = new StreamingContext(conf, Seconds(10))

val topics = Map("flume-log" -> 1)
val kafkaParams = Map("metadata.broker.list" -> "localhost:9092")

// 创建 Direct Kafka Stream
val kafkaStream = KafkaUtils.createDirectStream[String, String, StringDecoder, StringDecoder](
ssc, kafkaParams, topics)

// 处理数据
kafkaStream.flatMap(_._2.split(" "))
.map(word => (word, 1))
.reduceByKey(_ + _)
.print()

ssc.start()
ssc.awaitTermination()
}
}

3.3 优缺点分析

优点:

  • 数据可靠性高,数据在 Kafka 中持久化存储
  • 容错能力强,Kafka 的副本机制保证数据不丢失
  • 资源消耗较低,不需要 Receiver 节点
  • 支持 Exactly-Once 语义

缺点:

  • 实现相对复杂,需要额外部署 Kafka
  • 引入 Kafka 增加了系统复杂度
  • 延迟可能略高于 Receiver 方式

4. 两种方式的对比分析

| 特性 | Receiver 方式 | Direct 方式 |

|——|————–|————-|

| 数据可靠性 | 一般,可能丢失数据 | 高,数据持久化存储 |

| 容错能力 | 较弱,Receiver 故障可能导致数据丢失 | 强,Kafka 副本机制保证数据安全 |

| 资源消耗 | 高,需要专门资源运行 Receiver | 低,无需额外 Receiver 资源 |

| 实现复杂度 | 简单,直接集成 Flume | 复杂,需要引入 Kafka |

| 延迟 | 较低 | 略高,取决于 Kafka 处理能力 |

| 适用场景 | 实时性要求高,可容忍少量数据丢失 | 数据完整性要求高,容错能力强 |

5. 实践应用与注意事项

最小运行示例

以下是使用 Direct 方式的完整最小示例,包含 Flume 和 Spark Streaming 配置:

  • 首先启动 Kafka:
  • ```bash

    bin/kafka-server-start.sh config/server.properties

    bin/kafka-topics.sh –create –topic flume-log –partitions 1 –replication-factor 1 –zookeeper localhost:2181

    ```

  • 启动 Flume(配置如上述):
  • ```bash

    bin/flume-ng agent –conf conf –conf-file flume-direct.conf –name a1

    ```

  • 运行 Spark Streaming 应用:
  • ```bash

    spark-submit –class FlumeDirectStream spark-flume-streaming.jar

    ```

    注意事项

  • Receiver 方式注意事项:
    • 配置适当的批处理间隔以平衡实时性和资源消耗
    • 启用 Spark Streaming 的 WAL(Write Ahead Log)功能提高可靠性
    • 合理设置 Receiver 的资源分配,避免资源竞争
  • Direct 方式注意事项:
    • 确保 Kafka 集群配置合理,分区和副本数量适中
    • 考虑使用 Kafka 0.10+ 版本以获得更好的 Spark Streaming 集成支持
    • 监控 Kafka 消费者延迟,确保数据处理及时
  • 通用建议:
    • 在高可靠性要求场景下,优先选择 Direct 方式
    • 对于简单场景和快速原型开发,Receiver 方式更为便捷
    • 无论选择哪种方式,都要考虑系统监控和告警机制
    赞(0)
    未经允许不得转载:171主机测评 » Flume 与 Spark Streaming 实时集成:Direct 方式与 Receiver 方式的架构差异
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址