Spark实战(五)spark streaming + flume(Python版)

一、flume安装

(一)概述

   Flume是一个分布式、可靠、和高可用的海量日志采集、聚合和传输的系统。Flume可以采集文件,socket数据包等各种形式源数据,又可以将采集到的数据输出到HDFS、hbase、hive、kafka等众多外部存储系统中,一般的采集需求,通过对flume的简单配置即可实现,Flume针对特殊场景也具备良好的自定义扩展能力,因此flume可以适用于大部分的日常数据采集场景

(二)运行机制

   1、 Flume分布式系统中最核心的角色是agent,flume采集系统就是由一个个agent所连接起来形成

   2、 每一个agent相当于一个数据传递员,内部有三个组件:

a)	Source:采集源,用于跟数据源对接,以获取数据
b)	Sink:下沉地,采集数据的传送目的,用于往下一级agent传递数据或者往最终存储系统传递数据
c)	Channel:angent内部的数据传输通道,用于从source将数据传递到sink

在这里插入图片描述

(三)Flume采集系统结构图

1、简单结构

   单个agent采集数据

在这里插入图片描述

2、复杂结构

   多级agent之间串联
在这里插入图片描述

(四)Flume的安装部署

   1、去apache官网上下载安装包,并解压tar -zxvf apache-flume-1.8.0-bin,并修改conf目录下flume-env.sh,在里面配置JAVA_HOME

   2、根据数据采集的需求配置采集方案,描述在配置文件中(文件名可任意自定义)
   3、指定采集方案配置文件,在相应的节点上启动flume agent

二、flume push方式

1、spark streaming程序

   首先是flume通过push方式将采集到的数据传递到spark程序上,这种方式基本不用。spark代码如下:

import pyspark
from pyspark.sql import SparkSession
from pyspark.streaming import StreamingContext
from pyspark.streaming.flume import FlumeUtils

if __name__ == "__main__":
    spark = SparkSession\
            .builder\
            .appName("PythonWordCount") \
            .master("local[2]") \
            .getOrCreate()
    sc = spark.sparkContext
    ssc = StreamingContext(sc, 5)
    # hostname = sys.argv[1]
    # port = int(sys.argv[2])
    flumeStream = FlumeUtils.createStream(ssc, "localhost", 8888, pyspark.StorageLevel.MEMORY_AND_DISK_SER_2)
    line = flumeStream.map(lambda x: x[1])
    words = line.flatMap(lambda x: x.split(" "))
    datas = words.map(lambda x: (x, 1))
    result = datas.reduceByKey(lambda agg, obj: agg + obj)
    result.pprint()
    ssc.start()
    ssc.awaitTermination()

   注意:要指定并行度,如在本地运行设置setMaster(“local[2]”),相当于启动两个线程,一个给receiver,一个给computer。否则会出现如下问题

2019-01-09 19:36:16 INFO  ReceiverSupervisorImpl:54 - Called receiver 0 onStart
2019-01-09 19:36:16 INFO  ReceiverSupervisorImpl:54 - Waiting for receiver to be stopped
2019-01-09 19:36:20 INFO  JobScheduler:54 - Added jobs for time 1547033780000 ms
2019-01-09 19:36:25 INFO  JobScheduler:54 - Added jobs for time 1547033785000 ms
2019-01-09 19:36:30 INFO  JobScheduler:54 - Added jobs for time 1547033790000 ms
2019-01-09 19:36:35 INFO  JobScheduler:54 - Added jobs for time 1547033795000 ms
2019-01-09 19:36:40 INFO  JobScheduler:54 - Added jobs for time 1547033800000 ms

   如果是在集群中运行,必须要求集群中可用core数大于1

2、flume conf文件

<font size=4>&emsp; &emsp;在flume的conf目录下新建flume-push.conf内容如下</font></br>
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# source
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /home/hadoop/log/flume
a1.sources.r1.fileHeader = true

# Describe the sink
a1.sinks.k1.type = avro
#这是接收方
a1.sinks.k1.hostname = 192.168.62.131
a1.sinks.k1.port = 8888

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

   需要先将spark程序运行,使用以下命令:

spark/bin/spark-submit  --driver-class-path /home/hadoop/spark/jars/*:/home/hadoop/jar/flume/* /tmp/pycharm_project_563/day5/FlumePushWordCount.py

   可能会出现以下问题

	Spark Streaming's Flume libraries not found in class path. Try one of the following.

  1. Include the Flume library and its dependencies with in the
     spark-submit command as

     $ bin/spark-submit --packages org.apache.spark:spark-streaming-flume:2.4.0 ...

  2. Download the JAR of the artifact from Maven Central http://search.maven.org/,
     Group Id = org.apache.spark, Artifact Id = spark-streaming-flume-assembly, Version = 2.4.0.
     Then, include the jar in the spark-submit command as

     $ bin/spark-submit --jars <spark-streaming-flume-assembly.jar> ...
Traceback (most recent call last):
  File "/tmp/pycharm_project_563/day5/FlumePushWordCount.py", line 12, in <module>
    flumeStream = FlumeUtils.createStream(ssc, "192.168.62.131", "8888")
  File "/home/hadoop/spark/python/pyspark/streaming/flume.py", line 67, in createStream
    helper = FlumeUtils._get_helper(ssc._sc)
  File "/home/hadoop/spark/python/pyspark/streaming/flume.py", line 130, in _get_helper
    return sc._jvm.org.apache.spark.streaming.flume.FlumeUtilsPythonHelper()
TypeError: 'JavaPackage' object is not callable
   需要去maven仓库下载spark-streaming-flume-assembly.jar,然后放到上面指定的jar目录中去。

   然后运行flume

bin/flume-ng agent -n a1 -c conf/ -f conf/flume-push.conf -Dflume.root.logger=WARN,console
   然后在/home/hadoop/log/flume目录下新建log文件,运行spark的日志中出现如下:

在这里插入图片描述

三、poll方式

1、spark streaming程序

   这种方式是有spark主动去flume拉取数据,代码如下:

from pyspark.sql import SparkSession
from pyspark.streaming import StreamingContext
from pyspark.streaming.flume import FlumeUtils


if __name__ == "__main__":
    spark = SparkSession\
            .builder\
            .appName("PythonWordCount") \
            .master("local[2]") \
            .getOrCreate()
    sc = spark.sparkContext
    ssc = StreamingContext(sc, 5)
    addresses = [("localhost", 8888)]
    flumeStream = FlumeUtils.createPollingStream(ssc, addresses)
    line = flumeStream.map(lambda x: x[1])
    words = line.flatMap(lambda x: x.split(" "))
    datas = words.map(lambda x: (x, 1))
    result = datas.reduceByKey(lambda agg, obj: agg + obj)

    result.pprint()
    ssc.start()
    ssc.awaitTermination()

   如果是本地模式同样需要指定并行度,如果是在集群中运行,必须要求集群中可用core数大于1

2、flume conf文件

   在flume的conf目录下新建flume-poll.conf内容如下:

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# source
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /home/hadoop/log/flume
a1.sources.r1.fileHeader = true

# Describe the sink
a1.sinks.k1.type = org.apache.spark.streaming.flume.sink.SparkSink
a1.sinks.k1.hostname = localhost
a1.sinks.k1.port = 8888

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
   由于是poll方式,需要的flume
bin/flume-ng agent -n a1 -c conf/ -f conf/flume-poll.conf -Dflume.root.logger=WARN,console
   启动spark程序
spark/bin/spark-submit  --driver-class-path /home/hadoop/spark/jars/*:/home/hadoop/jar/flume/* /tmp/pycharm_project_563/day5/FlumePollWordCount.py 
   同样在/home/hadoop/log/flume目录下新建log文件,将原先生成的COMPLETED文件删除,rm flume/aaa.txt.COMPLETED ,运行spark的日志中出现如下:

在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python爬虫是一种用于抓取网页数据的程序,它可以通过发送HTTP请求并解析HTML内容来提取所需的数据。通过使用Python库如BeautifulSoup和Scrapy,我们可以编写爬虫来自动化数据收集和提取。 Flume是一个分布式的、可靠的、可扩展的日志收集、聚合和传输系统。它可以从多个源采集实时数据,并将其传输到其他处理系统中,如Hadoop和Spark。 Kafka是一个高吞吐量的分布式数据流平台,它允许以实时方式收集、处理和存储数据流。它被广泛用于大数据和流处理应用中,包括实时推荐、日志处理和事件驱动的架构。 Spark Streaming是Apache Spark的一个子项目,它允许在实时流数据中进行高效的流处理。Spark Streaming可以与Kafka等数据源集成,以实时的方式处理来自不同源的数据,并进行转换、分析和存储。 MySQL是一种关系型数据库管理系统,它被广泛用于存储和管理结构化数据。在上述技术栈中,MySQL可以被用作存储爬虫抓取的数据、Kafka传输的数据和Spark Streaming处理的数据。 ECharts是一种用于数据可视化的JavaScript图表库,它可以将数据转化为图表和图形,使数据更易于理解和分析。 综上所述,以上提到的技术可以结合使用来构建一个完整的实时数据处理和可视化系统。Python爬虫用于抓取实时数据,Flume用于收集和传输数据,Kafka用于数据流处理,Spark Streaming用于实时分析,MySQL用于数据存储,最后使用ECharts将数据可视化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值