实验3 Spark Streaming编程初级实践
文章目录
前言
Flume是Cloudera提供的一个分布式、可靠、可用的系统,它能够将不同数据源的海量日志数据进行高效收集、聚合、移动,最后存储到一个中心化数据存储系统中。Flume 的核心是把数据从数据源收集过来,再送到目的地。本次实验内容主要是安装flume,并对其进行测试,最终编写Spark Streaming程序对接受的flume source数据进行处理。
一、实验目的
1.1、通过实验学习日志采集工具Flume的安装和使用方法;
1.2、掌握采用Flume作为Spark Streaming数据源的编程方法。
二、实验环境
操作系统: Centos 8
Spark版本:1.11
Flume版本:1.7.1
三、实验内容
3.1 安装Flume
下载地址如下:
http://www.apache.org/dyn/closer.lua/flume/1.7.0/apache-flume-1.7.0-bin.tar.gz
我将其下载到桌面上,再传进docker里,命令如下
docker cp F:/apache-flume-1.7.0-bin.tar.gz masterip:/opt
(1)解压安装包并改名为flume
tar -zxvf apache-flume-1.7.0-bin.tar.gz
cd /opt
mv ./apache-flume-1.7.0-bin ./flume
(2)配置环境变量
vim ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64;
export FLUME_HOME=/usr/local/flume
export FLUME_CONF_DIR=$FLUME_HOME/conf
export PATH=$PATH:$FLUME_HOME/bin
这里的java根据本机安装的版本来写
修改 flume-env.sh 配置文件:
cd /opt/flume/conf
cp ./flume-env.sh.template ./flume-env.sh
vim ./flume-env.sh
添加Java版本
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64;
(3)查看flume版本信息
cd /opt/flume
./bin/flume-ng version
显示如下:

3.2 使用netcat数据源测试Flume
请对Flume的相关配置文件进行设置,从而可以实现如下功能:在一个Linux终端(这里称为“Flume终端”)中,启动Flume,在另一个终端(这里称为“Telnet终端”)中,输入命令“telnet localhost 44444”,然后,在Telnet终端中输入任何字符,让这些字符可以顺利地在Flume终端中显示出来。
(1)创建agent文件
cd /opt/flume
vim ./conf/example.conf
写入以下内容
1.#example.conf: A single-node Flume configuration
2.# Name the components on this agent
3.a1.sources = r1
4.a1.sinks = k1
5.a1.channels = c1
6.# Describe/configure the source
7.a1.sources.r1.type = netcat
8.a1.sources.r1.bind = localhost
9.a1.sources.r1.port = 44444
10.#同上,记住该端口名
11.# Describe the sink
12.a1.sinks.k1.type = logger
13.# Use a channel which buffers events in memory
14.a1.channels.c1.type = memory
15.a1.channels.c1.capacity = 1000
16.a1.channels.c1.transactionCapacity = 100
17.# Bind the source and sink to the channel
18.a1.sources.r1.channels = c1
19.a1.sinks.k1.channel = c1
(2)启动 flume agent (即打开日志控制台):
/usr/local/flume/bin/flume-ng agent --conf ./conf --conf-file ./conf/example.conf --name a1 -Dflume.root.logger=INFO,console
如下:

再打开一个终端,进入主机,并输入
telnet localhost 44444
发现出错,原因没有telnet命令
于是下载telnet
yum -y install telnet-server.x86_64
yum -y install telnet.x86_64
yum -y install xinetd.x86_64
设置开机自启
chkconfig telnet on
修改配置文件
vim /etc/xinetd.d/telnet
注意,在centos7以前的版本有这个配置文件,以后的版本没有,可以新建并修改如下:
# default: yes
# description: The telnet server servestelnet sessions; it uses \
# unencrypted username/password pairs for authentication.
service telnet
{
flags = REUSE
socket_type = stream
wait = no
user = root
server =/usr/sbin/in.telnetd
log_on_failure += USERID
disable = no
}
如有则修改,第一次修改,此文件若不存在,可自己vim创建修改,修改 disable = yes 为 disable = no
开启service,发现没有service,于是下载
yum install initscripts -y
下载完成,执行命令
service xinetd restart
再次输入命令
telnet localhost 44444
这时候可以在第二个终端输入数据,
第一、二个终端显示如下


netcatsource 运行成功。
3.3 使用Flume作为Spark Streaming数据源
Flume是非常流行的日志采集系统,可以作为Spark Streaming的高级数据源。请把Flume Source设置为netcat类型,从终端上不断给Flume Source发送各种消息,Flume把消息汇集到Sink,这里把Sink类型设置为avro,由Sink把消息推送给Spark Streaming,由自己编写的Spark Streaming应用程序对消息进行处理。
(1)下载spark-streaming的jar包
我下载的是spark-streaming-flume_2.11-2.4.7.jar,注意要下载对应的版本。下载地址如下:
https://mvnrepository.com/artifact/org.apache.spark/spark-streaming-flume_2.11/2.1.0
(2)将下载的jar包导入spark的jars目录下
将下载的jar包传入docker容器里,
进入jars目录,新建flume文件夹并将
cd spark-2.4.7-bin-hadoop2.7/jars/
mkdir flume
cp /opt/spark-streaming-flume_2.11-2.4.7.jar /opt/spark-2.4.7-bin-hadoop2.7/jars/flume/
将flume的lib目录下的jar包,复制到spark的jars下:
cp /usr/local/flume/lib/* /opt/spark-2.4.7-bin-hadoop2.7/jars
注意,在复制过来以后,确认是否覆盖原有的包,我选择的是替换,在复制过后可能会出现多个版本的jar包冲突,需要将冲突的包删去。
进入spark-shell,输入
import org.apache.spark.streaming.flume._
结果如下:

说明环境配置成功。
(3) 添加conf文件
输入以下命令
cd flume/conf
新建 flume-to-spark.conf 文件,添加以下内容
'''创建了agent(a1)的source、sink和channel'''
a1.sources = r1
a1.sinks = k1
a1.channels = c1
'''Flume Source类别设置为netcat,绑定到localhost的33333端口,
后面就可以通过“telnet localhost 33333”命令向Flume Source发送消息。'''
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 33333
'''把Flume Sink类别设置为avro,绑定到localhost的44444端口,Flume Source把采集到的消息汇集到Flume Sink
以后,Sink会把消息推送给localhost的44444端口,Spark Streaming程序一直在监听localhost的44444端口'''
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = localhost
a1.sinks.k1.port =44444
'''使用一个在内存中缓冲事件的通道,并进行配置'''
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000000
a1.channels.c1.transactionCapacity = 1000000
'''将source和sink通过channel连接起来'''
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
3.4 使用在IDEA中编写spark-streaming程序并打包
新建maven项目,选择建立scala文件
修改pom,添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>untitled2</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<scala.version>2.11</scala.version>
<hadoop.version>2.7.4</hadoop.version>
</properties>
<repositories>
<repository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-flume_2.11</artifactId>
<version>2.4.7</version>
</dependency>
</dependencies>
</project>
要注意的是,这里依赖的版本要与你的hadoop、scala、spark版本相适应。
新建scala文件夹并设置为源目录

新建scala类

FlumeEventCount.scala 内容如下:
import org.apache.spark.SparkConf
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming._
import org.apache.spark.streaming.flume._
import org.apache.spark.util.IntParam
object FlumeEventCount {
def main(args: Array[String]) {
// if (args.length < 2) {
// System.err.println(
// "Usage: FlumeEventCount <host> <port>")
// System.exit(1)
// }
StreamingExamples.setStreamingLogLevels()
// val Array(host, IntParam(port)) = args
val host = "localhost"
val port = 44444
val batchInterval = Milliseconds(2000)
// Create the context and set the batch size
val sparkConf = new SparkConf().setAppName("FlumeEventCount").setMaster("local[2]")
val ssc = new StreamingContext(sparkConf, batchInterval)
// Create a flume stream
val stream = FlumeUtils.createStream(ssc, host, port, StorageLevel.MEMORY_ONLY_SER_2)
// Print out the count of events received from this server in each batch
stream.count().map(cnt => "Received " + cnt + " flume events." ).print()
ssc.start()
ssc.awaitTermination()
}
}
StreamingExamples.scala 内容如下
import org.apache.log4j.{Level, Logger}
import org.apache.spark.internal.Logging
object StreamingExamples extends Logging {
/** Set reasonable logging levels for streaming if the user has not configured log4j. */
def setStreamingLogLevels() {
val log4jInitialized = Logger.getRootLogger.getAllAppenders.hasMoreElements
if (!log4jInitialized) {
// We first log something to initialize Spark's default logging, then we override the
// logging level.
logInfo("Setting log level to [WARN] for streaming example." +
" To override add a custom log4j.properties to the classpath.")
Logger.getRootLogger.setLevel(Level.WARN)
}
}
}
将程序打包,根据打包步骤File->Project Structure->Artifacts->"+"->JAR->From modules with dependencies
浏览选择主类 FlumeEventCount
注意,选择过主类过后,只保留jar包和output 其他的删去,如下

按住 shift 点击第一个便可以批量删除。
之后 build->build Artifacts->build 打成jar包,执行如下命令用docker将jar包传入主机的 /opt 目录下
docker cp F:/shiyan1.jar master:/opt
具体目录根据自己的实际情况来定。
3.5 打开终端 测试结果
打开第一个中端,运行程序
spark-submit --class FlumeEventCount /opt/untitled2.jar
没想到出现以下错误

查看原因,是因为jackson的版本过于老了,打开spark-streaming下的jars,发现jackson的jars下有多个版本的相同名称的jar包——
原因找到了!
于是一个个删去老版本的,启动
结果如下

因为接收的是流数据,所以在不停的滚动。
bingo!
打开第二个中端,进入flume下的 bin目录,输入
flume-ng agent --conf usr/local/flume/conf --conf-file /usr/local/flume/conf/flume-to-spark.conf --name a1 -Dflume.root.logger=INFO,console
如下,表示成功

打开第三个终端
输入
telnet localhost 33333
如下

便可以输入任意字符,第一个终端就可以显示结果啦!
实验完成!
四、实验中遇到问题及解决
1.安装软件和包的时候会遇到命令不存在的现象,这是首首先要检查是否下载。
2.将flume的jar包导入spark-streaming的时候千万要注意有无jar包重复,重复会造成版本冲突以至于运行spark出错。
3.用IDEA打jar包的时候要删去不必要的文件,否则会出错。
五、实验心得
1.遇到问题不要慌张,查看报错信息和日志是好方法;
2.很多时候环境出问题是因为jar包出问题了,要检查报错中是否出现jar包冲突。
该博客介绍了如何在Centos 8环境下进行Spark Streaming与Flume的集成实验。首先,详细讲述了Flume的安装与配置,包括使用netcat测试Flume数据传输。接着,通过下载Spark Streaming的Flume库,将Flume设置为Spark Streaming的数据源,并创建配置文件。最后,博主在IDEA中编写Spark Streaming程序,打包并解决因jar包冲突导致的问题,成功实现了数据的实时处理和展示。

3005

被折叠的 条评论
为什么被折叠?



