Flume作业 nc|exec-memory-logger

需求:

    Flume的Source从NetCat 和 Exec上采集数据到通过内存输出到控制台




这里memory也可以是file聚合,checkpointDir记录偏移量

# Use a channel which buffers events in memory
agent1.channels.channel1.type = file
agent1.channels.channel1.checkpointDir=/var/checkpoint 
agent1.channels.channel1.dataDirs=/var/tmp
agent1.channels.channel1.capacity = 1000
agent1.channels.channel1.transactionCapactiy = 100


这里采取memory配置文件:

a1.sources = r1 r2
a1.sinks = k1
a1.channels = c1
a1.sources.r1.channels = c1
a1.sources.r1.type = netcat
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port = 44444
a1.sources.r2.channels = c1
a1.sources.r2.type = exec
a1.sources.r2.command = tail -F /home/hadoop/data/data.log
# 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.sources.r2.channels = c1
a1.sinks.k1.channel = c1


测试结果:

[hadoop@hadoop001 ~]$ telnet localhost 44444
Trying ::1...
Connected to localhost.
Escape character is '^]'.
ZOURC123456789
OK
[hadoop@hadoop001 data]$ echo 123 >> data.log
[hadoop@hadoop001 data]$


控制台输出结果:

2018-08-10 20:12:10,426 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{} body: 31 32 33                                        123 }
2018-08-10 20:12:32,439 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{} body: 5A 4F 55 52 43 31 32 33 34 35 36 37 38 39 0D    ZOURC123456789. }


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31441024/viewspace-2199833/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31441024/viewspace-2199833/

### Apache Flume 配置监听端口教程 Apache Flume 是一个分布式、可靠且高可用的日志收集系统,支持多种数据源输入方式。其中 `NetCat Source` 和 `Exec Source` 是常见的两种数据采集方法。 #### NetCat Source 监听端口配置 通过 `NetCat Source` 可以让 Flume 实现基于 TCP 协议的数据接收功能。以下是具体实现步骤: 1. **创建 Agent 配置文件** 使用命令进入 Flume 的配置目录并创建一个新的配置文件: ```bash cd /root/software/apache-flume-1.9.0-bin/conf/ touch flume_netcat.conf ``` 2. **编辑配置文件内容** 编辑刚刚创建的 `flume_netcat.conf` 文件,添加如下内容来定义 NetCat Source、Memory Channel 和 Logger Sink[^2]: ```properties # 定义组件名称 agent.sources = netcatSource agent.channels = memoryChannel agent.sinks = loggerSink # 配置 Source (NetCat Source) agent.sources.netcatSource.type = netcat agent.sources.netcatSource.bind = 0.0.0.0 agent.sources.netcatSource.port = 44444 # 配置 Channel (Memory Channel) agent.channels.memoryChannel.type = memory agent.channels.memoryChannel.capacity = 1000 agent.channels.memoryChannel.transactionCapacity = 100 # 配置 Sink (Logger Sink) agent.sinks.loggerSink.type = logger # 绑定 Source 到 Channel agent.sources.netcatSource.channels = memoryChannel # 绑定 Sink 到 Channel agent.sinks.loggerSink.channel = memoryChannel ``` 3. **启动 Flume Agent** 启动 Flume 并加载刚才编写的配置文件: ```bash flume-ng agent \ --conf conf/ \ --name agent \ --conf-file /root/software/apache-flume-1.9.0-bin/conf/flume_netcat.conf \ -Dflume.root.logger=INFO,console ``` 4. **测试连接** 使用 `nc` 工具向指定端口发送消息进行验证: ```bash echo "Test message from client" | nc localhost 44444 ``` 如果一切正常,在 Flume 控制台会打印接收到的消息。 --- #### Exec Source 日志监控与 Avro Sink 数据转发 如果需要使用 `Exec Source` 来实时读取日志文件并将数据传递给其他节点,则可以按照以下方式进行配置[^3]: 1. 创建新的配置文件 `exec_to_avro.conf`: ```bash touch exec_to_avro.conf ``` 2. 添加配置内容: ```properties # 定义组件名称 agent.sources = execSource agent.channels = avroChannel agent.sinks = avroSink # 配置 Source (Exec Source) agent.sources.execSource.type = exec agent.sources.execSource.command = tail -F /root/software/apache-flume-1.9.0-bin/logs/flume.log agent.sources.execSource.shell = /bin/bash # 配置 Channel (Memory Channel) agent.channels.avroChannel.type = memory agent.channels.avroChannel.capacity = 1000 agent.channels.avroChannel.transactionCapacity = 100 # 配置 Sink (Avro Sink) agent.sinks.avroSink.type = avro agent.sinks.avroSink.hostname = next_flume_host agent.sinks.avroSink.port = 41414 # 绑定 Source 到 Channel agent.sources.execSource.channels = avroChannel # 绑定 Sink 到 Channel agent.sinks.avroSink.channel = avroChannel ``` 3. 启动第一个 Flume 节点作为生产者: ```bash flume-ng agent \ --conf conf/ \ --name agent \ --conf-file /root/software/apache-flume-1.9.0-bin/conf/exec_to_avro.conf \ -Dflume.root.logger=INFO,console ``` 4. 在目标主机上运行第二个 Flume 节点作为消费者(假设其 IP 地址为 `next_flume_host`),该节点需配置对应的 `Avro Source` 接收来自上游的数据流。 --- ```python # Python 示例:模拟客户端向 Flume 发送数据 import socket def send_data(host='localhost', port=44444, data="Hello Flume"): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((host, port)) s.sendall(data.encode('utf-8')) send_data() ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值