Flume实战一,Flume配合telnet从指定网络端口采集数据输出到控制台

1、使用Flume的关键就是写配置文件

1)配置Source
2)配置Channel
3)配置Sinks
4)将以上三种组件串联起来
在cd $FLUME_HOME/conf
vim example.conf

    #example.conf内容
    # a1可以看做是flume服务的名称,每个flume都由sources、channels和sinks三部分组成
    # sources可以看做是数据源头、channels是中间转存的渠道、sinks是数据后面的去向
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1
    
    # 配置Source
    a1.sources.r1.type = netcat
    a1.sources.r1.bind = localhost
    a1.sources.r1.port = 44444
    
    # 配置Channel
    a1.channels.c1.type = memory
    
    # 配置Sinks
    a1.sinks.k1.type = logger
    
    # Bind the source and sink to the channel 
    #将指定source(r1)和channel(c1)绑定
    #再将sink(k1)和channel(c1)绑定
    #则:source(r1)和 sink(k1)已经连通
    a1.sources.r1.channels = c1
    a1.sinks.k1.channel = c1

2、启动Flume

1)启动Flume

    $FLUME_HOME/bin/flume-ng agent \
    --name a1 \
    --conf $FLUME_HOME/conf \
    --conf-file $FLUME_HOME/conf/example.conf \
    -Dflume.root.logger=INFO,console

    #$FLUME_HOME/bin/flume-ng agent  启动flume-ng的agent服务
    #--name a1  指定服务名
    #--conf $FLUME_HOME/conf  Flume配置文件目录flume-env.sh
    #--conf-file $FLUME_HOME/conf/example.conf   我们编写的配置文件的目录
    #-Dflume.root.logger=INFO,console   在控制台输出日志

2)使用telnet进行测试

#启动telnet后在控制台输入内容即可

    > telnet localhost 44444
    Trying 127.0.0.1...
    Connected to localhost.localdomain (127.0.0.1).
    Escape character is '^]'.
    Hello world! 

此时就可以在flume-ng agent的控制台看到如下输出

    12/06/19 15:32:19 INFO source.NetcatSource: Source starting
    12/06/19 15:32:19 INFO source.NetcatSource: Created serverSocket:sun.nio.ch.ServerSocketChannelImpl[/127.0.0.1:44444]
    12/06/19 15:32:34 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 77 6F 72 6C 64 21 0D          Hello world!. }

说明

Event: { headers:{} body: 48 65 6C 6C 6F 20 77 6F 72 6C 64 21 0D Hello world!. }
Event是Flume传输数据的基本单元
Event = 可选的header + byte array

你可能感兴趣的:(环境,Flume)