【无标题】编写flume测试文件

首先我们要切换到Flume的conf目录下

编辑配置文件 用 vim+文件名  就可以实现编辑

添加配置代码

这里面的配置代码,可以在flume的官网里面去复制,我这里用的是avro source,hdfs sinks,file channels。大家可以根据自己的需要去复制不同类型的source,sinks,channels。

Flume 1.9用户手册中文版 — 可能是目前翻译最完整的版本了 (liyifeng.org)

这是flume1.9版本的使用手册

然后使用以下代码去启动我们的配置文件

这样就成功启动了我们的配置文件。但是我们也只是成功启动了我们的配置文件,我们怎么去测试我们的配置文件呢

首先我们另起一个hadoop01节点

使用代码 yum install telnet -y 去安装Telnet服务

然后使用代码 telnet localhost +端口号 去模拟数据发送

最后输入数据后显示ok就表示我们配置成功了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Flume拦截器示例,它将日志记录在文件中并添加一个时间戳: ``` package com.example.flume.interceptor; import org.apache.flume.Context; import org.apache.flume.Event; import org.apache.flume.interceptor.Interceptor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; public class TimestampInterceptor implements Interceptor { private static final Logger logger = LoggerFactory.getLogger(TimestampInterceptor.class); private FileWriter fileWriter; private TimestampInterceptor(FileWriter fileWriter) { this.fileWriter = fileWriter; } @Override public void initialize() { // 初始化方法,可以用于读取配置文件等 } @Override public Event intercept(Event event) { // 对每个事件进行拦截处理 String body = new String(event.getBody()); String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); String log = String.format("[%s] %s", timestamp, body); try { fileWriter.write(log); fileWriter.flush(); } catch (IOException e) { logger.error("Failed to write log to file", e); } return event; } @Override public List<Event> intercept(List<Event> events) { // 对事件列表进行批量处理 for (Event event : events) { intercept(event); } return events; } @Override public void close() { // 关闭方法,可以用于释放资源等 try { fileWriter.close(); } catch (IOException e) { logger.error("Failed to close file writer", e); } } public static class Builder implements Interceptor.Builder { private FileWriter fileWriter; @Override public void configure(Context context) { // 配置方法,可以用于读取参数等 String fileName = context.getString("fileName", "flume.log"); try { File file = new File(fileName); if (!file.exists()) { file.createNewFile(); } fileWriter = new FileWriter(file, true); } catch (IOException e) { logger.error("Failed to create file writer", e); } } @Override public Interceptor build() { // 构建方法,返回一个Interceptor实例 return new TimestampInterceptor(fileWriter); } } } ``` 这个拦截器会在Flume接收到事件后将日志写入文件中,并在每行日志前添加一个时间戳。在Flume配置文件中使用这个拦截器的方法如下: ``` agent.sources = source1 agent.channels = channel1 agent.sinks = sink1 agent.sources.source1.type = ... agent.sources.source1.interceptors = interceptor1 agent.sources.source1.interceptors.interceptor1.type = com.example.flume.interceptor.TimestampInterceptor$Builder agent.sources.source1.interceptors.interceptor1.fileName = /path/to/log/file agent.channels.channel1.type = ... agent.channels.channel1.capacity = ... agent.channels.channel1.transactionCapacity = ... agent.sinks.sink1.type = ... agent.sinks.sink1.channel = channel1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值