flume flume自定义source从数据库采集数据

flume自定义source链接数据库

  • 理论准备
    • 自定义source
    • PollableSource实现流程
    • 自定义flume开发流程
  • 示例
    • 需求
    • 数据准备
    • pom文件
    • IDEA工程MYSQL配置
    • 代码实现
      • 封装数据库操作
      • 自定义source
    • flume配置文件
  • 官网地址

理论准备

自定义source

自定义source可以实现一些原生source没有的功能,如:从网上下载,链接数据库等
自定义的消息有两种类型Source

  • PollableSource (轮询拉取)
  • EventDrivenSource (事件驱动)

两者的区别在于:

  • PollableSource是通过线程不断去调用process方法,主动拉取消息。在利用PollableSource实现自定义Source时还需要实现Configurable接口,以便在项目中初始化某些配置用的
  • EventDrivenSource是需要触发一个调用机制,即被动等待。

flume flume自定义source从数据库采集数据_第1张图片

PollableSource实现流程

  • 继承AbstractSource并实现Configurable和PollableSource接口
  • configure(Context context):获得flume配置文件中的参数值
  • process():核心处理数据方法
  • 从mysql表中获取数据,然后把数据封装成event对象写入到channel(该方法被一直调用)
  • stop():关闭相关资源

自定义flume开发流程

  • 编写自定义java代码
  • 打包上传到flume下的lib文件夹下
  • 编写flume的conf配置文件

示例

需求

  • 实现从MySQL采集数据并打印到控制台
  • 实现可以增量采集数据(每次记录采集到的哪,下次采集没有采集过的文件)
  • 指定时间间隔采集

flume flume自定义source从数据库采集数据_第2张图片

数据准备

CREATE DATABASE IF NOT EXISTS mysqlsource DEFAULT CHARACTER SET utf8 ;
# 创建一个表,用户保存拉取目标表位置的信息
CREATE TABLE mysqlsource.flume_meta (
  source_tab VARCHAR(255) NOT NULL,
  currentIndex VARCHAR(255) NOT NULL,
  PRIMARY KEY (source_tab)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

# 插入数据
INSERT  INTO mysqlsource.flume_meta(source_tab,currentIndex) VALUES ('student','4');

# 创建要拉取数据的表
CREATE TABLE mysqlsource.student(
  id INT(11) NOT NULL AUTO_INCREMENT,
  NAME VARCHAR(255) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=INNODB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;


# 向student表中添加测试数据
INSERT  INTO mysqlsource.student(id,NAME) VALUES (1,'zhangsan'),(2,'lisi'),(3,'wangwu'),(4,'zhaoliu');

SELECT * FROM mysqlsource.`flume_meta`
SELECT * FROM mysqlsource.`student`

pom文件

<dependencies>
        <dependency>
            <groupId>org.apache.flume</groupId>
            <artifactId>flume-ng-core</artifactId>
            <version>1.6.0-cdh5.14.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.6</version>
        </dependency>
    </dependencies>

IDEA工程MYSQL配置

在的resouces下添加配置文件下添加jdbc.properties

dbDriver=com.mysql.jdbc.Driver
dbUrl=jdbc:mysql://node03:3306/mysqlsource?useUnicode=true&characterEncoding=utf-8
dbUser=root
dbPassword=111111

代码实现

封装数据库操作

import org.apache.flume.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.naming.ConfigurationException;
import java.sql.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class QueryMysql {
     
    private static final Logger LOG = LoggerFactory.getLogger(QueryMysql.class);

    private int runQueryDelay,   //两次查询的时间间隔
            startFrom,            //开始id
            currentIndex,       //当前id
            recordSixe = 0,        //每次查询返回结果的条数
            maxRow;                //每次查询的最大条数

    private String table,          //要操作的表
            columnsToSelect,       //用户传入的查询的列
            customQuery,          //用户传入的查询语句
            query,                 //构建的查询语句
            defaultCharsetResultSet;//编码集

    //上下文,用来获取配置文件
    private Context context;

    //为定义的变量赋值(默认值),可在flume任务的配置文件中修改
    private static final int DEFAULT_QUERY_DELAY = 10000;
    private static final int DEFAULT_START_VALUE = 0;
    private static final int DEFAULT_MAX_ROWS = 2000;
    private static final String DEFAULT_COLUMNS_SELECT = "*";
    private static final String DEFAULT_CHARSET_RESULTSET = "UTF-8";

    private static Connection conn = null;
    private static PreparedStatement ps = null;
    private static String connectionURL, connectionUserName, connectionPassword;

    //加载静态资源
    static {
     
        Properties p = new Properties();
        try {
     
            p.load(QueryMysql.class.getClassLoader().getResourceAsStream("jdbc.properties"));
            connectionURL = p.getProperty("dbUrl");
            connectionUserName = p.getProperty("dbUser");
            connectionPassword = p.getProperty("dbPassword");
            Class.forName(p.getProperty("dbDriver"));
        } catch (Exception e) {
     
            LOG.error(e.toString());
        }
    }

    //获取JDBC连接
    private static Connection InitConnection(String url, String user, String pw) {
     
        try {
     
            Connection conn = DriverManager.getConnection(url, user, pw);
            if (conn == null)
                throw new SQLException();
            return conn;
        } catch (SQLException e) {
     
            e.printStackTrace();
        }
        return null;
    }

    //构造方法
    QueryMysql(Context context) throws ParseException, ConfigurationException {
     
        //初始化上下文
        this.context = context;

        //有默认值参数:获取flume任务配置文件中的参数,读不到的采用默认值
        this.columnsToSelect = context.getString("columns.to.select", DEFAULT_COLUMNS_SELECT);
        this.runQueryDelay = context.getInteger("run.query.delay", DEFAULT_QUERY_DELAY);
        this.startFrom = context.getInteger("start.from", DEFAULT_START_VALUE);
        this.defaultCharsetResultSet = context.getString("default.charset.resultset", DEFAULT_CHARSET_RESULTSET);

        //无默认值参数:获取flume任务配置文件中的参数
        this.table = context.getString("table");
        this.customQuery = context.getString("custom.query");

        connectionURL = context.getString("connection.url");
        connectionUserName = context.getString("connection.user");
        connectionPassword = context.getString("connection.password");
        conn = InitConnection(connectionURL, connectionUserName, connectionPassword);

        //校验相应的配置信息,如果没有默认值的参数也没赋值,抛出异常
        checkMandatoryProperties();
        //获取当前的id
        currentIndex = getStatusDBIndex(startFrom);
        //构建查询语句
        query = buildQuery();
    }

    //校验相应的配置信息(表,查询语句以及数据库连接的参数)
    private void checkMandatoryProperties() throws ConfigurationException {
     
        if (table == null) {
     
            throw new ConfigurationException("property table not set");
        }
        if (connectionURL == null) {
     
            throw new ConfigurationException("connection.url property not set");
        }
        if (connectionUserName == null) {
     
            throw new ConfigurationException("connection.user property not set");
        }
        if (connectionPassword == null) {
     
            throw new ConfigurationException("connection.password property not set");
        }
    }

    //构建sql语句
    private String buildQuery() {
     
        String sql = "";
        //获取当前id
        currentIndex = getStatusDBIndex(startFrom);
        LOG.info(currentIndex + "");
        if (customQuery == null) {
     
            sql = "SELECT " + columnsToSelect + " FROM " + table;
        } else {
     
            sql = customQuery;
        }
        StringBuilder execSql = new StringBuilder(sql);
        //以id作为offset
        if (!sql.contains("where")) {
     
            execSql.append(" where ");
            execSql.append("id").append(">").append(currentIndex);
            return execSql.toString();
        } else {
     
            int length = execSql.toString().length();
            return execSql.toString().substring(0, length - String.valueOf(currentIndex).length()) + currentIndex;
        }
    }

    //执行查询
    List<List<Object>> executeQuery() {
     
        try {
     
            //每次执行查询时都要重新生成sql,因为id不同
            customQuery = buildQuery();
            //存放结果的集合
            List<List<Object>> results = new ArrayList<>();
            if (ps == null) {
     
                //初始化PrepareStatement对象
                ps = conn.prepareStatement(customQuery);
            }
            ResultSet result = ps.executeQuery(customQuery);
            while (result.next()) {
     
                //存放一条数据的集合(多个列)
                List<Object> row = new ArrayList<>();
                //将返回结果放入集合
                for (int i = 1; i <= result.getMetaData().getColumnCount(); i++) {
     
                    row.add(result.getObject(i));
                }
                results.add(row);
            }
            LOG.info("execSql:" + customQuery + "\nresultSize:" + results.size());
            return results;
        } catch (SQLException e) {
     
            LOG.error(e.toString());
            // 重新连接
            conn = InitConnection(connectionURL, connectionUserName, connectionPassword);
        }
        return null;
    }

    //将结果集转化为字符串,每一条数据是一个list集合,将每一个小的list集合转化为字符串
    List<String> getAllRows(List<List<Object>> queryResult) {
     
        List<String> allRows = new ArrayList<>();
        if (queryResult == null || queryResult.isEmpty())
            return allRows;
        StringBuilder row = new StringBuilder();
        for (List<Object> rawRow : queryResult) {
     
            Object value = null;
            for (Object aRawRow : rawRow) {
     
                value = aRawRow;
                if (value == null) {
     
                    row.append(",");
                } else {
     
                    row.append(aRawRow.toString()).append(",");
                }
            }
            allRows.add(row.toString());
            row = new StringBuilder();
        }
        return allRows;
    }

    //更新offset元数据状态,每次返回结果集后调用。必须记录每次查询的offset值,为程序中断续跑数据时使用,以id为offset
    void updateOffset2DB(int size) {
     
        //以source_tab做为KEY,如果不存在则插入,存在则更新(每个源表对应一条记录)
        String sql = "insert into flume_meta(source_tab,currentIndex) VALUES('"
                + this.table
                + "','" + (recordSixe += size)
                + "') on DUPLICATE key update source_tab=values(source_tab),currentIndex=values(currentIndex)";
        LOG.info("updateStatus Sql:" + sql);
        execSql(sql);
    }

    //执行sql语句
    private void execSql(String sql) {
     
        try {
     
            ps = conn.prepareStatement(sql);
            LOG.info("exec::" + sql);
            ps.execute();
        } catch (SQLException e) {
     
            e.printStackTrace();
        }
    }

    //获取当前id的offset
    private Integer getStatusDBIndex(int startFrom) {
     
        //从flume_meta表中查询出当前的id是多少
        String dbIndex = queryOne("select currentIndex from flume_meta where source_tab='" + table + "'");
        if (dbIndex != null) {
     
            return Integer.parseInt(dbIndex);
        }
        //如果没有数据,则说明是第一次查询或者数据表中还没有存入数据,返回最初传入的值
        return startFrom;
    }

    //查询一条数据的执行语句(当前id)
    private String queryOne(String sql) {
     
        ResultSet result = null;
        try {
     
            ps = conn.prepareStatement(sql);
            result = ps.executeQuery();
            while (result.next()) {
     
                return result.getString(1);
            }
        } catch (SQLException e) {
     
            e.printStackTrace();
        }
        return null;
    }

    //关闭相关资源
    void close() {
     
        try {
     
            ps.close();
            conn.close();
        } catch (SQLException e) {
     
            e.printStackTrace();
        }
    }

    int getCurrentIndex() {
     
        return currentIndex;
    }

    void setCurrentIndex(int newValue) {
     
        currentIndex = newValue;
    }

    int getRunQueryDelay() {
     
        return runQueryDelay;
    }

    String getQuery() {
     
        return query;
    }

    String getConnectionURL() {
     
        return connectionURL;
    }

    private boolean isCustomQuerySet() {
     
        return (customQuery != null);
    }

    Context getContext() {
     
        return context;
    }

    public String getConnectionUserName() {
     
        return connectionUserName;
    }

    public String getConnectionPassword() {
     
        return connectionPassword;
    }

    String getDefaultCharsetResultSet() {
     
        return defaultCharsetResultSet;
    }

}

自定义source

public class MySqlSource extends AbstractSource implements Configurable,PollableSource{
     
    //打印日志
    private static final Logger LOGGER = LoggerFactory.getLogger(MySqlSource.class);
    //定义sqlHelper
    private QueryMysql sqlSourceHelper;

    //核心处理方法
    @Override
    public Status process() throws EventDeliveryException {
     
        try {
     
            //查询数据
            List<List<Object>> result = sqlSourceHelper.executeQuery();
            //存放event集合
            ArrayList<Event> events = new ArrayList<>();
            //存放event的header集合
            HashMap<String, String> headers = new HashMap<>();
            //如果有数据返回,将数据封装成event
            if (!result.isEmpty()){
     
                List<String> allRows = sqlSourceHelper.getAllRows(result);
                Event event = null;
                for (String row : allRows){
     
                    event = new SimpleEvent();
                    event.setBody(row.getBytes());
                    event.setHeaders(headers);
                    events.add(event);
                }
            }
            //查询间隔
            Thread.sleep(sqlSourceHelper.getRunQueryDelay());
            //表示已经成功创建了一条或多条events
            return Status.READY;
        } catch (InterruptedException e) {
     
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getBackOffSleepIncrement() {
     
        return 0;
    }

    @Override
    public long getMaxBackOffSleepInterval() {
     
        return 0;
    }

    //获得flume配置文件中的参数值
    @Override
    public void configure(Context context) {
     
        try {
     
            sqlSourceHelper = new QueryMysql(context);
        } catch (ParseException e) {
     
            e.printStackTrace();
        } catch (ConfigurationException e) {
     
            e.printStackTrace();
        }

    }
}

flume配置文件

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = com.MySqlSource
a1.sources.r1.connection.url = jdbc:mysql://node03:3306/mysqlsource
a1.sources.r1.connection.user = root
a1.sources.r1.connection.password = 111111
a1.sources.r1.table = student
a1.sources.r1.columns.to.select = *
a1.sources.r1.start.from=0
a1.sources.r1.run.query.delay=3000

# Describe the channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Describe the sink
a1.sinks.k1.type = logger


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

官网地址

官网地址

你可能感兴趣的:(flume)