Hadoop Flume代理:将主机添加到消息,然后发布到卡夫卡主题

Hadoop Flume代理:将主机添加到消息,然后发布到卡夫卡主题,hadoop,apache-kafka,flume,flume-ng,Hadoop,Apache Kafka,Flume,Flume Ng,我们开始通过将消息发布到Kafka主题来整合应用程序中的事件日志数据。虽然我们可以直接从应用程序编写Kafka,但我们选择将其视为一般问题并使用Flume代理。这提供了一些灵活性:如果我们想从服务器捕获其他内容,我们可以跟踪不同的源并发布到不同的卡夫卡主题 我们创建了Flume agent conf文件来跟踪日志并发布到Kafka主题: tier1.sources = source1 tier1.channels = channel1 tier1.sinks = sink1 tier1.so

我们开始通过将消息发布到Kafka主题来整合应用程序中的事件日志数据。虽然我们可以直接从应用程序编写Kafka,但我们选择将其视为一般问题并使用Flume代理。这提供了一些灵活性:如果我们想从服务器捕获其他内容,我们可以跟踪不同的源并发布到不同的卡夫卡主题

我们创建了Flume agent conf文件来跟踪日志并发布到Kafka主题:

tier1.sources  = source1
tier1.channels = channel1
tier1.sinks = sink1

tier1.sources.source1.type = exec
tier1.sources.source1.command = tail -F /var/log/some_log.log
tier1.sources.source1.channels = channel1

tier1.channels.channel1.type = memory
tier1.channels.channel1.capacity = 10000
tier1.channels.channel1.transactionCapacity = 1000

tier1.sinks.sink1.type = org.apache.flume.sink.kafka.KafkaSink
tier1.sinks.sink1.topic = some_log
tier1.sinks.sink1.brokerList = hadoop01:9092,hadoop02.com:9092,hadoop03.com:9092
tier1.sinks.sink1.channel = channel1
tier1.sinks.sink1.batchSize = 20
不幸的是,消息本身没有指定生成它们的主机。如果我们有一个应用程序在多个主机上运行,并且发生错误,那么我们无法确定是哪个主机生成了消息

我注意到,如果Flume直接向HDFS写入数据,我们就可以向特定的HDFS位置写入数据。虽然我们可能会对卡夫卡做一些类似的事情,即为每台服务器创建一个新主题,但这可能会变得笨拙。我们最终会有成千上万的话题


Flume是否可以在发布到Kafka topic时附加/包括原始主机的主机名?

您可以创建一个自定义TCP源,读取客户端地址并将其添加到标头

@Override
    public void configure(Context context) {
        port = context.getInteger("port");
        buffer = context.getInteger("buffer");

        try{
            serverSocket = new ServerSocket(port);
            logger.info("FlumeTCP source initialized");
        }catch(Exception e) {
            logger.error("FlumeTCP source failed to initialize");
        }
    }

@Override
    public void start() {
        try {
            clientSocket = serverSocket.accept();
            receiveBuffer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            logger.info("Connection established with client : " + clientSocket.getRemoteSocketAddress());
            final ChannelProcessor channel = getChannelProcessor();
            final Map<String, String> headers = new HashMap<String, String>();
            headers.put("hostname", clientSocket.getRemoteSocketAddress().toString());
            String line = "";
            List<Event> events = new ArrayList<Event>();

            while ((line = receiveBuffer.readLine()) != null) {
                Event event = EventBuilder.withBody(
                        line, Charset.defaultCharset(),headers);

                logger.info("Event created");
                events.add(event);
                if (events.size() == buffer) {
                    channel.processEventBatch(events);
                }
            }
        } catch (Exception e) {

        }
        super.start();
    }
我发送了一条测试消息来测试它,它看起来像:

Event: { headers:{hostname=/127.0.0.1:50999} body: 74 65 73 74 20 6D 65 73 73 61 67 65             test message }

我已在我的

中上载了项目。如果您使用的是
exec
源代码,则没有任何东西可以阻止您运行smart命令将主机名作为日志文件内容的前缀

注意:如果命令使用管道之类的东西,则还需要如下指定shell:

tier1.sources.source1.type = exec
tier1.sources.source1.shell = /bin/sh -c
tier1.sources.source1.command =  tail -F /var/log/auth.log | sed --unbuffered "s/^/$(hostname) /"
frb.hi.inet 2015-11-17 08:39:39.432 INFO [...]
这些消息如下所示:

tier1.sources.source1.type = exec
tier1.sources.source1.shell = /bin/sh -c
tier1.sources.source1.command =  tail -F /var/log/auth.log | sed --unbuffered "s/^/$(hostname) /"
frb.hi.inet 2015-11-17 08:39:39.432 INFO [...]
。。。其中
frb.hi.inet
us是我的主机名