栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Flume 收集 java 请求数据

Flume 收集 java 请求数据

1.源码
package com.yqq.log;
import org.apache.flume.Event;
import org.apache.flume.EventDeliveryException;
import org.apache.flume.api.RpcClient;
import org.apache.flume.api.RpcClientFactory;
import org.apache.flume.event.EventBuilder;
import org.junit.Before;
import org.junit.Test;

import java.nio.charset.Charset;


public class FlumeClient {

    private MyRpcClientFacade client;
    private Event event;
    @Before
    public void init(){
        client = new MyRpcClientFacade();
        client.initAttribute("node3",10086);
    }

    @Test
    public void doSendMsg(){
        String sampleData = "Hello Flume!";
        for (int i = 0; i < 10; i++) {
            System.out.println(sampleData + "---" + i);
            client.sendDataToFlume(sampleData + "---" + i);
        }
        client.close();
    }
    class MyRpcClientFacade{
        private RpcClient client;
        private String hostname;
        private int port;
        public void initAttribute(String hostname,int port){
            this.hostname = hostname;
            this.port = port;
            this.client = RpcClientFactory.getDefaultInstance(hostname, port);
        }

        public void sendDataToFlume(String data) {
            event = EventBuilder.withBody(data, Charset.forName("UTF-8"));
            try {
                client.append(event);
            } catch (EventDeliveryException e) {
                client.close();
                client = null;
                client = RpcClientFactory.getDefaultInstance(hostname,port);
            }
        }

        public void close() {
            client.close();
        }
    }
}

2.flume 的项目设置配置
  1. node3 上创建配置文件 option3
# 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 = avro
a1.sources.r1.bind = node3
a1.sources.r1.port = 10086
# 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 
a1.sinks.k1.channel = c1 
  1. 启动 node3 上 flume
[root@node3 ~]# flume-ng agent -n a1 -conf-file option3 -Dflume.root.logger=INFO,console
  1. 运行项目源码
log4j:WARN No appenders could be found for logger (org.apache.flume.api.NettyAvroRpcClient).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hello Flume!---0
Hello Flume!---1
Hello Flume!---2
Hello Flume!---3
Hello Flume!---4
Hello Flume!---5
Hello Flume!---6
Hello Flume!---7
Hello Flume!---8
Hello Flume!---9
  1. 查看node3服务器
21/11/21 14:11:26 INFO ipc.NettyServer: Connection to /192.168.134.1:52739 disconnected.
21/11/21 15:59:04 INFO ipc.NettyServer: [id: 0x4c5737cc, /192.168.134.1:60824 => /192.168.134.103:10086] OPEN
21/11/21 15:59:04 INFO ipc.NettyServer: [id: 0x4c5737cc, /192.168.134.1:60824 => /192.168.134.103:10086] BOUND: /192.168.134.103:10086
21/11/21 15:59:04 INFO ipc.NettyServer: [id: 0x4c5737cc, /192.168.134.1:60824 => /192.168.134.103:10086] CONNECTED: /192.168.134.1:60824
21/11/21 15:59:05 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 46 6C 75 6D 65 21 2D 2D 2D 30 Hello Flume!---0 }
21/11/21 15:59:05 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 46 6C 75 6D 65 21 2D 2D 2D 31 Hello Flume!---1 }
21/11/21 15:59:05 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 46 6C 75 6D 65 21 2D 2D 2D 32 Hello Flume!---2 }
21/11/21 15:59:05 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 46 6C 75 6D 65 21 2D 2D 2D 33 Hello Flume!---3 }
21/11/21 15:59:05 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 46 6C 75 6D 65 21 2D 2D 2D 34 Hello Flume!---4 }
21/11/21 15:59:05 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 46 6C 75 6D 65 21 2D 2D 2D 35 Hello Flume!---5 }
21/11/21 15:59:05 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 46 6C 75 6D 65 21 2D 2D 2D 36 Hello Flume!---6 }
21/11/21 15:59:05 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 46 6C 75 6D 65 21 2D 2D 2D 37 Hello Flume!---7 }
21/11/21 15:59:05 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 46 6C 75 6D 65 21 2D 2D 2D 38 Hello Flume!---8 }
21/11/21 15:59:05 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 46 6C 75 6D 65 21 2D 2D 2D 39 Hello Flume!---9 }
21/11/21 15:59:05 INFO ipc.NettyServer: [id: 0x4c5737cc, /192.168.134.1:60824 :> /192.168.134.103:10086] DISConNECTED
21/11/21 15:59:05 INFO ipc.NettyServer: [id: 0x4c5737cc, /192.168.134.1:60824 :> /192.168.134.103:10086] UNBOUND
21/11/21 15:59:05 INFO ipc.NettyServer: [id: 0x4c5737cc, /192.168.134.1:60824 :> /192.168.134.103:10086] CLOSED

  1. 查看HDFS上logs目录
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/582591.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号