9.14

6. 使用 Spark Streaming 实时处理数据

概述

Spark Streaming 是 Spark 生态系统中的流式数据处理组件。本文将介绍如何使用 Spark Streaming 实现实时数据处理。

内容

  • Spark Streaming 的基本概念
  • 从 Socket 数据源读取数据
  • 实时单词统计示例

代码示例

val conf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount") val ssc = new StreamingContext(conf, Seconds(1)) // 从 Socket 读取数据 val lines = ssc.socketTextStream("localhost", 9999) val words = lines.flatMap(_.split(" ")) // 单词计数 val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _) wordCounts.print() ssc.start() ssc.awaitTermination()
posted @ 2024-12-26 13:23  kxzzow  阅读(10)  评论(0)    收藏  举报