flume kafak to hdfs

#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 = org.apache.flume.source.kafka.KafkaSource
a1.sources.r1.kafka.bootstrap.servers = hdp4:6667,hdp5:6667,hdp6:6667
a1.sources.r1.kafka.consumer.group.id = flume_test
# 定义kafka所在zk的地址
a1.sources.r1.zookeeperConnect = hdp1.hdp:2181,hdp2.hdp:2181,hdp3.hdp:2181
a1.sources.r1.kafka.topics = IOT_DS_DATA_BACK2
# 消费超时时间
a1.sources.r1.kafka.consumer.timeout.ms = 10000
#向channel写入消息的最多条数
a1.sources.r1.batchSize = 50000
#向channel书写的最大时间 (毫秒)
a1.sources.r1.batchDurationMillis = 2000
#9.7mb
a1.sources.r1.kafka.consumer.max.partition.fetch.bytes = 10240000
# Describe the sink
a1.sinks.k1.type = hdfs
# hdfs://hdp1.hdp:8020/IOT/*/%y-%m-%d/%H-%M
a1.sinks.k1.hdfs.path = hdfs://hdp1.hdp:8020/IOT/*/20%y/%m/%d-%H-%M
#用本地时间格式化目录
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#下沉后, 生成的文件类型,默认是Sequencefile,可用DataStream,则为普通文本  a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.writeFormat = Text
a1.sinks.k1.hdfs.fileType = DataStream
#fileType: 默认值为SequenceFile,文件格式主要包括:SequenceFile, DataStream, CompressedStream;当使用DataStream时,文件不会被压缩,则不需要设置hdfs.codeC; 当使用CompressedStream时,则必须设置一个正确的hdfs.codeC值
#a1.sinks.k1.hdfs.fileType = CompressedStream
#codeC: 文件压缩格式,包括:gzip, bzip2, lzo, lzop, snappy;
#a1.sinks.k1.hdfs.codeC = lzop

#前缀
a1.sinks.k1.hdfs.filePrefix = data
#后缀
a1.sinks.k1.hdfs.fileSuffix=.json
#文件回滚之前等待的时时间单位 秒
a1.sinks.k1.hdfs.rollInterval = 1800
#文件滚动大的大小限制(bytes)8000000  /1024/1024
a1.sinks.k1.hdfs.rollSize =  90000000
#写入多少个 event 数据后滚动文件(事件个数)
a1.sinks.k1.hdfs.rollCount = 70000
#hdfs sink启动的操作HDFS的线程数。
a1.sinks.k1.hdfs.threadsPoolSize = 100
#默认值:1,hdfs sink 启动的根据时间滚动文件的线程数。
a1.sinks.k1.hdfs.rollTimerPoolSize = 10
# 事件就往里面写入
a1.sinks.k1.hdfs.batchSize = 10000
a1.sinks.k1.hdfs.txnEventMax = 10000


# 10 分钟就创建文件
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
#默认值为0, 当目前被打开的临时文件在该参数指定的时间(秒)内,没有任何数据写入,则将该临时文件关闭并重命名成目标文件
#a1.sinks.k1.hdfs.idleTimeout=300
#默认值为HDFS副本数;写入HDFS文件块的最小副本数,该参数会影响文件的滚动配置,一般将该参数配置成1,才可以按照配置正确滚动文件;
a1.sinks.k1.hdfs.minBlockReplicas=1
#默认值5000,最大允许打开的HDFS文件数,当打开的文件数达到该值,最早打开的文件将会被关闭;
a1.sinks.k1.hdfs.maxOpenFiles=5000
#默认值:10000,执行HDFS操作的超时时间(单位:毫秒);
a1.sinks.k1.hdfs.callTimeout=100000


# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 800000000
a1.channels.c1.transactionCapacity = 500000
a1.channels.c1.keep-alive = 50


# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
      private String filePathUrl;
                       

 public Status process() throws EventDeliveryException {
    Channel channel = getChannel();
    Transaction transaction = channel.getTransaction();
    transaction.begin();
    try {
      Set<BucketWriter> writers = new LinkedHashSet<>();
      int txnEventCount = 0;
      for (txnEventCount = 0; txnEventCount < batchSize; txnEventCount++) {
        Event event = channel.take();
        if (event == null) {
          break;
        }
        //自定义-----------------------
        byte[] body = event.getBody();
        String msg = new String(body);
        //System.out.println("event.getBody().toString():"+msg);
        Map data = parseData(msg);
        String date = data.get("t").toString();
        String orgId = data.get("orgId").toString();


        filePath="hdfs://hdp1.hdp:8020/IOT/"+newInteger(orgId)%100+"/"+orgId+"/"+filePathUrl.substring(filePathUrl.indexOf("*")+2,filePathUrl.length());

        //自定义----------------------------------
        // reconstruct the path name by substituting place holders
        String realPath = BucketPath.escapeString(filePath, event.getHeaders(),
            timeZone, needRounding, roundUnit, roundValue, useLocalTime);
        String realName = BucketPath.escapeString(fileName, event.getHeaders(),
            timeZone, needRounding, roundUnit, roundValue, useLocalTime);

        String lookupPath = realPath + DIRECTORY_DELIMITER + realName;
        BucketWriter bucketWriter;
        HDFSWriter hdfsWriter = null;
        // Callback to remove the reference to the bucket writer from the
        // sfWriters map so that all buffers used by the HDFS file
        // handles are garbage collected.
        WriterCallback closeCallback = new WriterCallback() {
          @Override
          public void run(String bucketPath) {

### Flume -> Kafka -> Flume -> HDFS 数据流架构确认 数据流架构为 Flume -> Kafka -> Flume -> HDFS 是可行的,并且在实际生产环境中被广泛采用。以下是对此架构的专业分析和详细说明: #### 1. **Flume 到 Kafka** Flume 负责从各种数据源(如日志文件、网络流量等)中采集数据,并将其发送到 Kafka。这种设计的好处在于 Flume 的灵活性和可靠性,能够处理不同来源的数据并将其统一写入 Kafka[^1]。 配置示例: ```properties # 定义 agent 的名称 a1.sources = r1 a1.sinks = k1 a1.channels = c1 # 配置 source,从本地文件或网络流中读取数据 a1.sources.r1.type = exec a1.sources.r1.command = tail -F /var/log/myapp.log # 配置 sink,将数据写入 Kafka a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink a1.sinks.k1.topic = test_topic a1.sinks.k1.brokerList = localhost:9092 # 配置 channel,使用内存通道 a1.channels.c1.type = memory a1.channels.c1.capacity = 1000 a1.channels.c1.transactionCapacity = 100 ``` #### 2. **Kafka 作为消息队列** Kafka 在此架构中充当消息队列的角色,负责存储和分发数据。Flume 将数据写入 Kafka 后,Kafka 可以确保数据的高可靠性和高吞吐量,同时支持多个消费者组进行数据消费[^3]。 #### 3. **Kafka 到 Flume** 另一个 Flume 实例可以从 Kafka 中消费数据。通过 Kafka Source 插件,Flume 能够高效地读取 Kafka 中的消息并进行后续处理。这种设计允许对数据进行进一步的过滤、转换或聚合操作。 配置示例: ```properties # 定义 agent 的名称 a2.sources = r1 a2.sinks = k1 a2.channels = c1 # 配置 source,从 Kafka 中读取数据 a2.sources.r1.type = org.apache.flume.source.kafka.KafkaSource a2.sources.r1.zookeeperConnect = localhost:2181 a2.sources.r1.topic = test_topic a2.sources.r1.groupId = flume-consumer-group # 配置 channel,使用文件通道 a2.channels.c1.type = file a2.channels.c1.capacity = 10000 a2.channels.c1.transactionCapacity = 1000 # 配置 sink,将数据写入 HDFS a2.sinks.k1.type = hdfs a2.sinks.k1.hdfs.path = hdfs://namenode:8020/user/flume/logs/%Y%m%d a2.sinks.k1.hdfs.fileType = DataStream a2.sinks.k1.hdfs.rollInterval = 600 a2.sinks.k1.hdfs.rollSize = 134217728 a2.sinks.k1.hdfs.rollCount = 0 ``` #### 4. **FlumeHDFS** 最终,Flume 将数据写入 HDFS。为了优化性能和避免小文件问题,可以通过调整 `rollInterval`、`rollSize` 和 `rollCount` 等参数来控制文件滚动策略[^4]。 --- ### 注意事项 - 在停止 Flume 服务时,由于使用了 File Channel 并且 Flume 消费 Kafka 数据到 HDFS,因此不会出现数据丢失的问题,但可能会有重复信息的问题[^4]。 - 如果需要重新消费 Kafka 中的数据,可以通过删除对应的消费者组和 Flume 的 checkpoint 文件实现[^4]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值