好的网站:
初始实时流处理
需求:统计主站每个(指定)课程访问的客户端、地域信息分布
地域:ip转换, SparkSQL项目实战
客户端:useragent获取, hadoop基础课程
实现步骤:
课程编号、IP信息、useragent
进行相应的统计分析操作:MR/Spark
项目框架:
日志收集: Flume
离线分析:MR/Spark
统计结果图形化展示
问题:
小时级别
10分钟
5分钟
1分钟
实时流处理产生背景
时效姓高,数据量大,
离线计算与实时计算的对比
1)数据来源
离线:HDFS历史数据,数据量比较大;
实时:消息队列kafka,实时新增/修改记录过来的某一笔数据
2)处理过程
离线:MapReduce:map+reduce
实时:Spark(DStream/SS)
3)处理速度
离线:慢
实时:快
4)进程
离线:启动+销毁
实时:一直
实时流处理架构与技术选型
1.用户行为产生日志;在hadoop集群之外;
2.收集日志,Flume;
3.缓存日志,保证高峰期也不会造成后端压力;Kafka;
4.实时流处理,SparkStreaming
5.入库到关系型数据库;
6.可视化展示;
分布式日志收集框架Flume
日志收集
业务现状分析:
1.WebServer/ApplicationServer分散在各个机器上;
2.想大数据平台Hadoop进行统计分析;
3.日志如何收集到Hadoop平台上;
4.解决方案及存在的问题;
Flume概述
一个分布式、高可靠、高可用的服务,用于分布式的海量日志的高效收集、聚合、移动系统。
设计目标
可靠性,扩展性,管理性
业界同类产品对比
Flume
Logstash:ELK(ELasticSearch,Kibana)
FLume架构及核心组件
1)source(收集)
2)channel(聚集)
3)sink(输出)
Flume安装条件
1.安装JDK;
2.Memory
3.Disk
4.Directory
Flume实战
使用Flume的关键就是写配置文件
1.配置SOURCE
2.配置channel
3.配置Sink
4.把以上三个组件串起来
需求一:从指定网络端口采集数据输出到控制台
a1:agent名称
r1:source的名称
k1:sink的名称
c1:channel的名称
# example.conf: A single-node Flume configuration # Name the components on this agent a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure the source a1.sources.r1.type = netcat a1.sources.r1.bind = hadoop1 a1.sources.r1.port = 44444 # Describe the sink a1.sinks.k1.type = logger # 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 #一个source可以对应多个channels a1.sinks.k1.channel = c1 #一个sink只对应一个channel
启动Agent
--conf 指定默认conf的目录
--conf-file 指定conf配置文件
$ bin/flume-ng agent --conf conf --conf-file example.conf --name a1 -Dflume.root.logger=INFO,console
产生数据
telnet hadoop000 44444
Event:{ headers:{} body: 68 65 6C 6C 6F 0D hello. }
Event是Flume数据传输的基本单元;
Event = 可选的header+ byte array
需求二:监控一个文件实施采集新增的数据输出到控制台
Agent选型:
启动Agent
监控data.log文件
需求三:将A服务器上的日志实时采集到B服务器(输出到HDFS)
技术选型:
exe source + memory channel + avro sink
avro source + memory channel + logger sink
exec-memory-avo. conf
avro-memory-logger.conf
Kafka概述
消息中间件:生成者和消费者
生产者和消费者的速率不一样,所以需要消息中间件
kafka架构和核心概念
kafka架构
producer:生产者
consumer:消费者
broker:主机(篮子)
topic:主题,给消息打个标签,只能制定的消费者消费
record:消息,(key,value,timestamp)
kafka部署及使用
1.单节点单Broker部署及使用
配置文件:
$KAFKA_HOME/config/server.properties
参数:
broker.id = 0
listeners=PLAINTEXT://:9092
host.name=hadoop000
log.dirs=/data/log
zookeeper.connect=hadoop000:2181
启动Broker
bin/kafka-server-start.sh config/server.properties &
创建topic
> bin/kafka-topics.sh --zookeeper hadoop000:2181 --create --topic test --partitions 1 --replication-factor 1
查看topic
bin/kafka-topics.sh --zookeeper hadoop000:2181 --describe --topic test
bin/kafka-topics.sh --zookeeper hadoop000:2181 --list
总结:create,list都需要Zookeeper的支持
生成消息
> bin/kafka-console-producer.sh --broker-list hadoop000:9092 --topic test
消费消息
> bin/kafka-console-consumer.sh --bootstrap-server hadoop000:9092 --topic test --from-beginning
--consumer-property可以指定消费者参数,如 group.id=test_group。多个参数可以用逗号隔开。
> bin/kafka-console-consumer.sh --broker-list cluster101:9092 --topic test --consumer-property group.id=test_group
2.单节点多Broker部署及使用
修改配置文件
启动多个broker
bin/kafka-server-start.sh config/server.properties &
bin/kafka-server-start.sh config/server1.properties &
bin/kafka-server-start.sh config/server2.properties &
jps -m 查看kafka进程
> bin/kafka-console-producer.sh --broker-list hadoop000:9092, hadoop000:9093,hadoop000:9094 --topic test
> bin/kafka-console-consumer.sh --bootstrap-server hadoop000:9092 --topic test
3.多节点多Broker部署及使用
kafka容错性
副本策略:
leader掉了,follower会自动选举成leader;
Kafka JAVA API
Kafka Sink
整合Flume
配置flume到Kafka
启动Flume
测试:
生产数据
消费数据
后续消费数据改为SparkStreaming;
SparkStreaming
将不同的数据源的数据经过SparkStreaming处理之后,将结果输出到外部文件系统。
集成Spark生态系统
SparkStreaming工作原理
粗粒度
SparkStreamingContext接收到实施数据流,把数据按照指定的时间段切成一片片小的数据块;然后把小的数据库传给SparkEngine处理。
细粒度
SparkStreaming核心
详情看http://spark.apache.org/docs/latest/streaming-programming-guide.html
1.StreamingContext
创建SteamingContext的最常用两种方式:
To initialize a Spark Streaming program, a StreamingContext object has to be created which is the main entry point of all Spark Streaming functionality.
StreamingContext是所有SPA入库Streaming的主入口
The
appName
parameter is a name for your application to show on the cluster UI.master
is a Spark, Mesos, Kubernetes or YARN cluster URL, or a special “local[*]” string to run in local mode.appName是一个应用程序的名字,在SparkUI中显示,也可以使用local模式
After a context is defined, you have to do the following.
- Define the input sources by creating input DStreams.
- Define the streaming computations by applying transformation and output operations to DStreams.
- Start receiving data and processing it using
streamingContext.start()
.