Flume三个器

拦截器

自定义拦截器方法

java实现其interceptor接口
并实现其4个方法
1.初始化方法
2.单event方法
3.多event方法
4.close方法
5.创建一个静态的内部类 Builder

选择器

Replicating(默认):会自动的发往全部的channel

Multiplexing:会选择发往对应的channel

监控器

ganglia 发现尝试提交的次数 远远大于最终提交成功次数; 说明flume性能不行;
自身;提高自己的内存 4-6g flume_env.sh (因为高于6G时性能并不会提升特别大所以最高设置为6G)
外援:增加flume台数

自定义拦截器代码及使用

导入相关依赖及打包依赖(因为我们最后是需要在Linux环境下运行我们的flume脚本)

<dependencies>
    <dependency>
        <groupId>org.apache.flume</groupId>
        <artifactId>flume-ng-core</artifactId>
        <version>1.9.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>
package com.qqhru.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 FlumeInterceptor implements Interceptor{

	public void initialize() {

	}

	public Event intercept(Event event) {

		//1.获取数据
		byte[] body = event.getBody();
		String line = new String(body, Charset.forName("utf-8"));
		Map<String, String> headers = event.getHeaders();
		if (line.contains("start")) {
			headers.put("topic","topic_start");
		}else {
			headers.put("topic","topic_event");
		}

		return event;
	}

	public List<Event> intercept(List<Event> events) {
		ArrayList<Event> events1 = new ArrayList<Event>();
		for (Event event : events) {
			events1.add(intercept(event));

		}
		return events1;
	}

	public void close() {

	}
	
	public static class Buileder implements Interceptor.Builder{

		@Override
		public Interceptor build() {
			return new FlumeInterceptor();
		}

		@Override
		public void configure(Context context) {

		}
	}
}


a1.sources = r1
a1.channels = c1 c2
a1.sinks  = k1

#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 = /opt/module/hive//logs/*
a1.sources.r1.fileHeader = true
a1.sources.r1.channels = c1 c2


#interceptor
a1.sources.r1.interceptors =  i1
a1.sources.r1.interceptors.i1.type=com.atguigu.flume.interceptor.FlumeInterceptor$Builder

#selector
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 = localhost1:9092,localhost2:9092,localhost3: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 = localhost1:9092,localhost2:9092,localhost3:9092
a1.channels.c2.kafka.topic = topic_event
a1.channels.c2.parseAsFlumeEvent = false
a1.channels.c2.kafka.consumer.group.id = flume-consumer
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值