flume拦截器

flume拦截器代码

1.依赖

bash 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>flume-interceptor</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.flume</groupId>
            <artifactId>flume-ng-core</artifactId>
            <version>1.9.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>



</project>

2.核心代码

bash 复制代码
package com.atguigu.gmall.flume.log.interceptor;

import com.alibaba.fastjson.JSONObject;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

public class TimestampAndTableNameInterceptor implements Interceptor {
    @Override
    public void initialize() {

    }

    @Override
    public Event intercept(Event event) {

        Map<String, String> headers = event.getHeaders();
String log = new String(event.getBody(), StandardCharsets.UTF_8);

 		JSONObject jsonObject = JSONObject.parseObject(log);

 		Long ts = jsonObject.getLong("ts");
 		//Maxwell输出的数据中的ts字段时间戳单位为秒,Flume HDFSSink要求单位为毫秒
 		String timeMills = String.valueOf(ts * 1000);

 		String tableName = jsonObject.getString("table");
 		String databaseName = jsonObject.getString("database");

 		headers.put("timestamp", timeMills);
 		headers.put("tableName", tableName);
 		headers.put("databaseName", databaseName);
		return event;

    }

    @Override
    public List<Event> intercept(List<Event> events) {

        for (Event event : events) {
            intercept(event);
        }

        return events;
    }

    @Override
    public void close() {

    }

    public static class Builder implements Interceptor.Builder {


        @Override
        public Interceptor build() {
            return new TimestampAndTableNameInterceptor ();
        }

        @Override
        public void configure(Context context) {

        }
    }
}
相关推荐
奥顺2 小时前
算命网站源码PHP框架_附2025新版设计书教程
大数据·mysql·开源·php
小强签名设计2 小时前
Flink DataSet API
大数据·flink
昊昊该干饭了2 小时前
数仓建模(二) 从关系型数据库到数据仓库的演变
大数据·数据仓库·数据库架构
Apache Spark中国社区3 小时前
Celeborn Spark 集成最新进展
大数据·分布式·spark
赛逸展张胜4 小时前
5G+工业互联网”迎来新机遇,CES Asia 2025见证产业腾飞
大数据·人工智能·科技·5g·智慧城市
SeaTunnel4 小时前
开源数据集成平台白皮书重磅发布《Apache SeaTunnel 2024用户案例合集》!
大数据
wq54wq4 小时前
优化现金流:CRM回款自动化的关键步骤
大数据·运维·人工智能·自动化
神技圈子5 小时前
源码讲解MinIO 如何分布数据
大数据·golang·对象存储
Blood_J6 小时前
elasticsearch实战应用
大数据·elasticsearch·搜索引擎