问题导读:
1、Flume有哪些组件?
2、日志采集如何配置Flume?
3、Flume的ETL和分类型拦截器有哪些?
4、日志采集Flume启动停止脚本如何设置?
上一篇:大数据项目之电商数仓(用户行为数据采集)(二)
4.4 采集日志Flume
4.4.1 日志采集Flume安装
集群规划:
4.4.2 项目经验之Flume组件
1)Source
(1)Taildir Source相比Exec Source、Spooling Directory Source的优势
TailDir Source:断点续传、多目录。Flume1.6以前需要自己自定义Source记录每次读取文件位置,实现断点续传。
Exec Source可以实时搜集数据,但是在Flume不运行或者Shell命令出错的情况下,数据将会丢失。
Spooling Directory Source监控目录,不支持断点续传。
(2)batchSize大小如何设置?
答:Event 1K左右时,500-1000合适(默认为100)
2)Channel
采用Kafka Channel,省去了Sink,提高了效率。
4.4.3 日志采集Flume配置
1)Flume配置分析
Flume直接读log日志的数据,log日志的格式是app-yyyy-mm-dd.log。
2)Flume的具体配置如下:
(1)在/opt/module/flume/conf目录下创建file-flume-kafka.conf文件
- [kgg@hadoop101 conf]$ vim file-flume-kafka.conf
- 在文件配置如下内容
- a1.sources=r1
- a1.channels=c1 c2
-
- # configure source
- a1.sources.r1.type = TAILDIR
- a1.sources.r1.positionFile = /opt/module/flume/test/log_position.json
- a1.sources.r1.filegroups = f1
- a1.sources.r1.filegroups.f1 = /tmp/logs/app.+
- a1.sources.r1.fileHeader = true
- a1.sources.r1.channels = c1 c2
-
- #interceptor
- a1.sources.r1.interceptors = i1 i2
- a1.sources.r1.interceptors.i1.type = com.kgg.flume.interceptor.LogETLInterceptor$Builder
- a1.sources.r1.interceptors.i2.type = com.kgg.flume.interceptor.LogTypeInterceptor$Builder
-
- a1.sources.r1.selector.type = multiplexing
- a1.sources.r1.selector.header = topic
- a1.sources.r1.selector.mapping.topic_start = c1
- a1.sources.r1.selector.mapping.topic_event = c2
-
- # configure channel
- a1.channels.c1.type = org.apache.flume.channel.kafka.KafkaChannel
- a1.channels.c1.kafka.bootstrap.servers = hadoop101:9092,hadoop102:9092,hadoop103:9092
- a1.channels.c1.kafka.topic = topic_start
- a1.channels.c1.parseAsFlumeEvent = false
- a1.channels.c1.kafka.consumer.group.id = flume-consumer
-
- a1.channels.c2.type = org.apache.flume.channel.kafka.KafkaChannel
- a1.channels.c2.kafka.bootstrap.servers = hadoop101:9092,hadoop102:9092,hadoop103:9092
- a1.channels.c2.kafka.topic = topic_event
- a1.channels.c2.parseAsFlumeEvent = false
- a1.channels.c2.kafka.consumer.group.id = flume-consumer
复制代码
注意:com.kgg.flume.interceptor.LogETLInterceptor和com.kgg.flume.interceptor.LogTypeInterceptor是自定义的拦截器的全类名。需要根据用户自定义的拦截器做相应修改。
4.4.4 Flume的ETL和分类型拦截器
本项目中自定义了两个拦截器,分别是:ETL拦截器、日志类型区分拦截器。
ETL拦截器主要用于,过滤时间戳不合法和Json数据不完整的日志
日志类型区分拦截器主要用于,将启动日志和事件日志区分开来,方便发往Kafka的不同Topic。
1)创建Maven工程flume-interceptor
2)创建包名:com.kgg.flume.interceptor
3)在pom.xml文件中添加如下配置
- <dependencies>
- <dependency>
- <groupId>org.apache.flume</groupId>
- <artifactId>flume-ng-core</artifactId>
- <version>1.7.0</version>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- </configuration>
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
复制代码
4)在com.kgg.flume.interceptor包下创建LogETLInterceptor类名
- Flume ETL拦截器LogETLInterceptor
- package com.kgg.flume.interceptor;
-
- import org.apache.flume.Context;
- import org.apache.flume.Event;
- import org.apache.flume.interceptor.Interceptor;
-
- import java.nio.charset.Charset;
- import java.util.ArrayList;
- import java.util.List;
-
- public class LogETLInterceptor implements Interceptor {
-
- @Override
- public void initialize() {
-
- }
-
- @Override
- public Event intercept(Event event) {
-
- // 1 获取数据
- byte[] body = event.getBody();
- String log = new String(body, Charset.forName("UTF-8"));
-
- // 2 判断数据类型并向Header中赋值
- if (log.contains("start")) {
- if (LogUtils.validateStart(log)){
- return event;
- }
- }else {
- if (LogUtils.validateEvent(log)){
- return event;
- }
- }
-
- // 3 返回校验结果
- return null;
- }
-
- @Override
- public List<Event> intercept(List<Event> events) {
-
- ArrayList<Event> interceptors = new ArrayList<>();
-
- for (Event event : events) {
- Event intercept1 = intercept(event);
-
- if (intercept1 != null){
- interceptors.add(intercept1);
- }
- }
-
- return interceptors;
- }
-
- @Override
- public void close() {
-
- }
-
- public static class Builder implements Interceptor.Builder{
-
- @Override
- public Interceptor build() {
- return new LogETLInterceptor();
- }
-
- @Override
- public void configure(Context context) {
-
- }
- }
- }
复制代码
4)Flume日志过滤工具类
- package com.kgg.flume.interceptor;
- import org.apache.commons.lang.math.NumberUtils;
-
- public class LogUtils {
-
- public static boolean validateEvent(String log) {
- // 服务器时间 | json
- // 1549696569054 | {"cm":{"ln":"-89.2","sv":"V2.0.4","os":"8.2.0","g":"M67B4QYU@gmail.com","nw":"4G","l":"en","vc":"18","hw":"1080*1920","ar":"MX","uid":"u8678","t":"1549679122062","la":"-27.4","md":"sumsung-12","vn":"1.1.3","ba":"Sumsung","sr":"Y"},"ap":"weather","et":[]}
-
- // 1 切割
- String[] logContents = log.split("\\|");
-
- // 2 校验
- if(logContents.length != 2){
- return false;
- }
-
- //3 校验服务器时间
- if (logContents[0].length()!=13 || !NumberUtils.isDigits(logContents[0])){
- return false;
- }
-
- // 4 校验json
- if (!logContents[1].trim().startsWith("{") || !logContents[1].trim().endsWith("}")){
- return false;
- }
-
- return true;
- }
-
- public static boolean validateStart(String log) {
- // {"action":"1","ar":"MX","ba":"HTC","detail":"542","en":"start","entry":"2","extend1":"","g":"S3HQ7LKM@gmail.com","hw":"640*960","l":"en","la":"-43.4","ln":"-98.3","loading_time":"10","md":"HTC-5","mid":"993","nw":"WIFI","open_ad_type":"1","os":"8.2.1","sr":"D","sv":"V2.9.0","t":"1559551922019","uid":"993","vc":"0","vn":"1.1.5"}
-
- if (log == null){
- return false;
- }
-
- // 校验json
- if (!log.trim().startsWith("{") || !log.trim().endsWith("}")){
- return false;
- }
-
- return true;
- }
- }
复制代码
5)Flume日志类型区分拦截器LogTypeInterceptor
- package com.kgg.flume.interceptor;
-
- import org.apache.flume.Context;
- import org.apache.flume.Event;
- import org.apache.flume.interceptor.Interceptor;
-
- import java.nio.charset.Charset;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
-
- public class LogTypeInterceptor implements Interceptor {
- @Override
- public void initialize() {
-
- }
-
- @Override
- public Event intercept(Event event) {
-
- // 区分日志类型: body header
- // 1 获取body数据
- byte[] body = event.getBody();
- String log = new String(body, Charset.forName("UTF-8"));
-
- // 2 获取header
- Map<String, String> headers = event.getHeaders();
-
- // 3 判断数据类型并向Header中赋值
- if (log.contains("start")) {
- headers.put("topic","topic_start");
- }else {
- headers.put("topic","topic_event");
- }
-
- return event;
- }
-
- @Override
- public List<Event> intercept(List<Event> events) {
-
- ArrayList<Event> interceptors = new ArrayList<>();
-
- for (Event event : events) {
- Event intercept1 = intercept(event);
-
- interceptors.add(intercept1);
- }
-
- return interceptors;
- }
-
- @Override
- public void close() {
-
- }
-
- public static class Builder implements Interceptor.Builder{
-
- @Override
- public Interceptor build() {
- return new LogTypeInterceptor();
- }
-
- @Override
- public void configure(Context context) {
-
- }
- }
- }
复制代码
6)打包
拦截器打包之后,只需要单独包,不需要将依赖的包上传。打包之后要放入Flume的lib文件夹下面。
注意:为什么不需要依赖包?因为依赖包在flume的lib目录下面已经存在了。
7)需要先将打好的包放入到hadoop101的/opt/module/flume/lib文件夹下面。
- [kgg@hadoop101 lib]$ ls | grep interceptor
- flume-interceptor-1.0-SNAPSHOT.jar
复制代码
8)分发Flume到hadoop102、hadoop103
- [kgg@hadoop101 module]$ xsync flume/
- [kgg@hadoop101 flume]$ bin/flume-ng agent --name a1 --conf-file conf/file-flume-kafka.conf &
复制代码
4.4.5 日志采集Flume启动停止脚本
1)在/home/kgg/bin目录下创建脚本f1.sh
- [kgg@hadoop101 bin]$ vim f1.sh
- 在脚本中填写如下内容
- #! /bin/bash
-
- case $1 in
- "start"){
- for i in hadoop101 hadoop102
- do
- echo " --------启动 $i 采集flume-------"
- ssh $i "nohup /opt/module/flume/bin/flume-ng agent --conf-file /opt/module/flume/conf/file-flume-kafka.conf --name a1 -Dflume.root.logger=INFO,LOGFILE > /dev/null 2>&1 &"
- done
- };;
- "stop"){
- for i in hadoop101 hadoop102
- do
- echo " --------停止 $i 采集flume-------"
- ssh $i "ps -ef | grep file-flume-kafka | grep -v grep |awk '{print \$2}' | xargs kill"
- done
-
- };;
- esac
复制代码
说明1:nohup,该命令可以在你退出帐户/关闭终端之后继续运行相应的进程。nohup就是不挂起的意思,不挂断地运行命令。
说明2:/dev/null代表linux的空设备文件,所有往这个文件里面写入的内容都会丢失,俗称“黑洞”。
标准输入0:从键盘获得输入 /proc/self/fd/0
标准输出1:输出到屏幕(即控制台) /proc/self/fd/1
错误输出2:输出到屏幕(即控制台) /proc/self/fd/2
2)增加脚本执行权限
- [kgg@hadoop101 bin]$ chmod 777 f1.sh
复制代码
3)f1集群启动脚本
- [kgg@hadoop101 module]$ f1.sh start
复制代码
4)f1集群停止脚本
- [kgg@hadoop101 module]$ f1.sh stop
复制代码
最新经典文章,欢迎关注公众号
|