Flume NG flume-hdfs-sink 源代码分析

本文详细介绍了HDFSEventSink组件的配置、启动、处理及停止方法,包括从context中读取配置参数、设置编码、写入格式以及与不同格式的文件交互流程,并针对FLUME-1104问题提出解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C1: HDFSEventSink

0. HDFSEventSink.configure() also needs to implement a Configurable interface for processing its own configuration settings.

0.1 从context中读取配置参数configure

0.2 设置编码,

      codeC = getCodec(codecName);
      // TODO : set proper compression type
      compType = CompressionType.BLOCK;

0.2.1 getCodec()

(1) 通过 CompressionCodecFactory.getCodecClasses(conf); 获取所能兼容的编码类型codecs 

(2) 通过codecMatches(cls, codecName)判断是否相等,以获取编码名codecName所对应的编码类;

(3) 获取codec = cls.newInstance(),

(4) 


0.3 set writeFormat

if writeFormat = null, 

then set format according to file type, if fileType= DataStreamType or CompStreamType, set 


1. HDFSEventSink.start()  method should initialize the sink and bring it to a state where it can forward the events to its next destination.


2. HDFSEventSink.process() method from sink interface is should do the core processing of extracting the event from channel and forwarding it. 


3. HDFSEventSink.stop() method should do the necessary cleanup. 




HDFSEventSink will call 

(2) HDFSFormatterFactory  

(2.1) HDFSWriterableFormatter

(2.2) HDFSTextFormatter

(3) HDFSWriterFactory

(3.1) HDFSSequenceFile

(3.2) HDFSDataStream

(3.3) HDFSCompressDataStream

(4) BucketWriter

(5) HDFSWriter




 FLUME-1104 : HDFS rolls the first file incorrectly

The sink process() keep tracks of the buckets opened during the transaction. At the end of transaction, we need to flush all the buckets that has pending data. This is required in order to ensure that the data removed from channel should be safely in HDFS during commit.
Currently the files are tracked only when they are created and also getting closed during the cleanup instead of flush.

The fix is to track buckets every time they are written to in the current transaction. Also buckets with pending data should be flushed instead of close.

### Flume 配置写入 OSS-HDFS 示例 Flume 是一个分布式、可靠和高可用的海量日志采集、聚合和传输系统,能够将数据写入多种存储系统,包括 HDFS 和 OSS-HDFS。以下是配置 Flume 将数据写入 OSS-HDFS 的详细说明。 #### 配置示例 以下是一个完整的 Flume 配置文件示例,用于将数据写入 OSS-HDFS: ```properties # 定义 Source、Channel 和 Sink agent.sources = netcat-source agent.channels = memory-channel agent.sinks = oss-hdfs-sink # 配置 Source agent.sources.netcat-source.type = netcat agent.sources.netcat-source.bind = localhost agent.sources.netcat-source.port = 44444 # 配置 Channel agent.channels.memory-channel.type = memory agent.channels.memory-channel.capacity = 1000 agent.channels.memory-channel.transactionCapacity = 100 # 配置 Sink agent.sinks.oss-hdfs-sink.type = hdfs agent.sinks.oss-hdfs-sink.hdfs.path = oss://${your_bucket}/flume_dir/%Y-%m-%d/%H agent.sinks.oss-hdfs-sink.hdfs.filePrefix = your_topic agent.sinks.oss-hdfs-sink.hdfs.rollSize = 3600 agent.sinks.oss-hdfs-sink.hdfs.rollCount = 0 agent.sinks.oss-hdfs-sink.hdfs.rollInterval = 60 agent.sinks.oss-hdfs-sink.hdfs.batchSize = 100000 agent.sinks.oss-hdfs-sink.hdfs.round = true agent.sinks.oss-hdfs-sink.hdfs.roundValue = 15 agent.sinks.oss-hdfs-sink.hdfs.unit = minute agent.sinks.oss-hdfs-sink.hdfs.threadsPoolSize = 30 agent.sinks.oss-hdfs-sink.hdfs.kerberosPrincipal = agent.sinks.oss-hdfs-sink.hdfs.kerberosKeytab = # 绑定 Source 和 Channel agent.sources.netcat-source.channels = memory-channel # 绑定 Sink 和 Channel agent.sinks.oss-hdfs-sink.channel = memory-channel ``` #### 参数解释 - `hdfs.path`: 指定 OSS-HDFS 的存储路径,使用 `${your_bucket}` 替换为实际的 OSS Bucket 名称[^1]。 - `hdfs.filePrefix`: 指定生成文件的前缀名称。 - `hdfs.rollSize`: 文件滚动大小(以字节为单位),例如设置为 3600 表示当文件达到 3600 字节时会触发滚动操作。 - `hdfs.rollCount`: 每个文件中包含的最大事件数,设置为 0 表示不启用此功能。 - `hdfs.rollInterval`: 文件滚动的时间间隔(以秒为单位),例如设置为 60 表示每分钟滚动一次文件。 - `hdfs.batchSize`: 每次写入 HDFS 的事件数[^1]。 - `hdfs.round`: 是否启用时间对齐功能,设置为 `true` 表示启用。 - `hdfs.roundValue` 和 `hdfs.unit`: 时间对齐的具体值和单位,例如设置为 15 分钟对齐[^1]。 - `hdfs.threadsPoolSize`: 写入线程池的大小,影响并发性能。 #### 注意事项 - 确保 OSS-HDFS 的环境已经正确配置,包括 JindoFS 或其他类似的加速套件[^4]。 - 如果需要 Kerberos 认证,请填写 `hdfs.kerberosPrincipal` 和 `hdfs.kerberosKeytab` 的值[^2]。 - 配置文件中的 `${your_bucket}` 需要替换为实际的 OSS Bucket 名称。 ### 示例代码验证 以下是一个简单的测试代码,用于验证 Flume 是否能够正确写入数据到 OSS-HDFS: ```bash echo "Test data for OSS-HDFS" | nc localhost 44444 ``` 运行上述命令后,Flume 将接收到的数据写入指定的 OSS-HDFS 路径。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值