Spark Streaming编程初级实践

Spark Streaming编程初级实践


文章目录


写在前面

  • Linux:CentOS7.5
  • Spark: spark-3.0.0-bin-hadoop3.2
  • Flume:Flume-1.9.0
  • IDE:IntelliJ IDEA2020.2.3

1. 安装Flume

Flume是Cloudera提供的一个分布式、可靠、可用的系统,它能够将不同数据源的海量日志数据进行高效收集、聚合、移动,最后存储到一个中心化数据存储系统中。Flume 的核心是把数据从数据源收集过来,再送到目的地。请到Flume官网下载Flume1.7.0安装文件,下载地址如下:

http://www.apache.org/dyn/closer.lua/flume/1.7.0/apache-flume-1.7.0-bin.tar.gz

或者也可以直接到本教程官网的"下载专区"中的"软件"目录中下载apache-flume-1.7.0-bin.tar.gz。

下载后,把Flume1.7.0安装到Linux系统的"/usr/local/flume"目录下,具体安装和使用方法可以参考教程官网的"实验指南"栏目中的"日志采集工具Flume的安装与使用方法。

安装命令

shell 复制代码
tar -zxvf apache-flume-1.9.0-bin.tar.gz -C /export/server/
mv apache-flume-1.9.0-bin/ flume-1.9.0
sudo vi /etc/profile

export FLUME_HOME=/usr/local/flume
export PATH=$PATH:$FLUME_HOME/bin

source /etc/profile
mv flume-env.sh.template flume-env.sh
  • 查看版本号
shelll 复制代码
bin/flume-ng version

2.使用Avro数据源测试Flume

题目描述

Avro可以发送一个给定的文件给Flume,Avro 源使用AVRO RPC机制。请对Flume的相关配置文件进行设置,从而可以实现如下功能:在一个终端中新建一个文件helloworld.txt(里面包含一行文本"Hello World"),在另外一个终端中启动Flume以后,可以把helloworld.txt中的文本内容显示出来。

Flume配置文件

properties 复制代码
al.sources = r1
a1.sinks = k1
a1.channels = c1
a1.sources.r1.type = avro
a1.sources.r1.channels= c1
a1.sources.r1.bind = 0.0.0.0
al.sources.r1.port = 4141
a1.sinks.k1.type = logger
a1.channels.c1.type = memory
al.channels.c1.capacity = 1000
a1.channels.c1.transaction = 100
al.sources.r1.channels = c1
a1.sinks.k1.channel=c1

执行命令

  • 先进入到Flume安装目录,执行以下第一行命令;

  • 开始新的一个会话窗口,执行第二行命令写入数据到指定的文件中

  • 查看上一步骤中指定的文件内容

scala 复制代码
./bin/flume-ng agent -c . -f ./conf/avro.conf -n a1 -Dflume.root.logger=INFO,console
echo 'hello,world' >> ./log.00
bin/flume-ng avro-client --conf conf -H localhost -p 4141 -F ./log.00

执行结果如下

3. 使用netcat数据源测试Flume

题目描述

请对Flume的相关配置文件进行设置,从而可以实现如下功能:在一个Linux终端(这里称为"Flume终端")中,启动Flume,在另一个终端(这里称为"Telnet终端")中,输入命令"telnet localhost 44444",然后,在Telnet终端中输入任何字符,让这些字符可以顺利地在Flume终端中显示出来。

编写Flume配置文件

properties 复制代码
al.sources = r1
a1.sinks = k1
a1.channels = c1
al.sources.r1.type = netcat
al.sources.r1.channels = c1
a1.sources.r1.bind = localhost
al.sources.r1.port = 44444
a1.sinks.k1.type = logger
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
al.channels.c1.transaction = 100
al.sources.r1.channels = c1
a1.sinks.k1.channel = c1
  • 执行以下命令
shell 复制代码
./bin/flume-ng agent -c . -f ./netcatExample.conf -n a1 -Dflume.root.logger=INFO,console
telnet localhost 44444
  • 会话窗口成功得到数据

4. 使用Flume作为Spark Streaming数据源

题目描述

Flume是非常流行的日志采集系统,可以作为Spark Streaming的高级数据源。请把Flume Source设置为netcat类型,从终端上不断给Flume Source发送各种消息,Flume把消息汇集到Sink,这里把Sink类型设置为avro,由Sink把消息推送给Spark Streaming,由自己编写的Spark Streaming应用程序对消息进行处理。

编写Flume配置文件

properties 复制代码
al.sources = r1
a1.sinks = k1
a1.channels =  c1
al.sources.r1.type = netcat
al.sources.r1.bind = localhost
a1.sources.r1.port = 33333
a1.sinks.k1.type = avro
al.sinks.k1.hostname = localhost
a1.sinks.k1.port = 44444
a1.channels.c1.type = memory
al.channels.c1.capacity = 1000000
a1.channels.c1.transactionCapacity = 1000000
al.sources.r1.channels = c1
a1.sinks.k1.channel = c1

主程序代码

scala 复制代码
import org.apache.spark.SparkConf
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming._
import org.apache.spark.streaming.Milliseconds
import org.apache.spark.streaming.flume._
import org.apache.spark.util.IntParam

object FlumeEventCount {

    def main(args: Array[String]): Unit = {
        if (args.length < 2) {
            System.err.println( "Usage: FlumeEventCount <host> <port>")
            System.exit(1)
        }

        StreamingExamples.setStreamingLogLevels()
        val Array(host, IntParam(port)) = args
        val batchInterval = Milliseconds(2000)

        val sc = new SparkConf()
          .setAppName("FlumeEventCount")
//          .setMaster("local[2]")
        val ssc = new StreamingContext(sc, batchInterval)

        val stream = FlumeUtils.createStream(ssc, host, port, StorageLevel.MEMORY_ONLY_SER_2)

        stream.count().map(cnt => "Received " + cnt + " flume events." ).print()

        ssc.start()
        ssc.awaitTermination()


    }
}

执行结果1

scala 复制代码
import org.apache.log4j.{Level, Logger}
import org.apache.spark.internal.Logging

object StreamingExamples extends Logging {

    def setStreamingLogLevels(): Unit = {

        val log4jInitialized = Logger.getRootLogger.getAllAppenders.hasMoreElements

        if (!log4jInitialized) {
            logInfo("Setting log level to [WARN] for streaming example." + " To override add a custom log4j.properties to the classpath.")
            Logger.getRootLogger.setLevel(Level.WARN)
        }
    }

}

执行结果2

全文结束!!!

相关推荐
一只栖枝1 小时前
华为 HCIE 大数据认证中 Linux 命令行的运用及价值
大数据·linux·运维·华为·华为认证·hcie·it
喂完待续5 小时前
Apache Hudi:数据湖的实时革命
大数据·数据仓库·分布式·架构·apache·数据库架构
青云交5 小时前
Java 大视界 -- 基于 Java 的大数据可视化在城市交通拥堵治理与出行效率提升中的应用(398)
java·大数据·flink·大数据可视化·拥堵预测·城市交通治理·实时热力图
还是大剑师兰特11 小时前
Flink面试题及详细答案100道(1-20)- 基础概念与架构
大数据·flink·大剑师·flink面试题
1892280486115 小时前
NY243NY253美光固态闪存NY257NY260
大数据·网络·人工智能·缓存
武子康15 小时前
大数据-70 Kafka 日志清理:删除、压缩及混合模式最佳实践
大数据·后端·kafka
CCF_NOI.17 小时前
解锁聚变密码:从微观世界到能源新未来
大数据·人工智能·计算机·聚变
杨荧17 小时前
基于Python的电影评论数据分析系统 Python+Django+Vue.js
大数据·前端·vue.js·python
数据智研18 小时前
【数据分享】上市公司创新韧性数据(2007-2023)
大数据·人工智能
辞--忧1 天前
双十一美妆数据分析:洞察消费趋势与行业秘密
大数据