zoukankan      html  css  js  c++  java
  • flume学习

    下载

    自定义sink(mysql)

    1、ide打开下载后的源码 

    2、代码如下:

    /**
     * Licensed to the Apache Software Foundation (ASF) under one
     * or more contributor license agreements.  See the NOTICE file
     * distributed with this work for additional information
     * regarding copyright ownership.  The ASF licenses this file
     * to you under the Apache License, Version 2.0 (the
     * "License"); you may not use this file except in compliance
     * with the License.  You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
     
    /**
     * User: 过往记忆
     * Date: 14-9-4
     * Time: 下午13:16
     * bolg: https://www.iteblog.com
     * 本文地址:https://www.iteblog.com/archives/1109
     * 过往记忆博客,专注于hadoop、hive、spark、shark、flume的技术博客,大量的干货
     * 过往记忆博客微信公共帐号:iteblog_hadoop
     */
    package com.pf;
    import com.google.common.base.Preconditions;
    import com.google.common.base.Throwables;
    import com.google.common.collect.Lists;
    import org.apache.flume.*;
    import org.apache.flume.conf.Configurable;
    import org.apache.flume.sink.AbstractSink;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.util.List;
     
    public class MysqlSink extends AbstractSink implements Configurable {
     
        private Logger LOG = LoggerFactory.getLogger(MysqlSink.class);
        private String hostname;
        private String port;
        private String databaseName;
        private String tableName;
        private String user;
        private String password;
        private PreparedStatement preparedStatement;
        private Connection conn;
        private int batchSize;
     
        public MysqlSink() {
            LOG.info("MysqlSink start...");
        }
     
        @Override
        public void configure(Context context) {
            hostname = context.getString("hostname");
            Preconditions.checkNotNull(hostname, "hostname must be set!!");
            port = context.getString("port");
            Preconditions.checkNotNull(port, "port must be set!!");
            databaseName = context.getString("databaseName");
            Preconditions.checkNotNull(databaseName, "databaseName must be set!!");
            tableName = context.getString("tableName");
            Preconditions.checkNotNull(tableName, "tableName must be set!!");
            user = context.getString("user");
            Preconditions.checkNotNull(user, "user must be set!!");
            password = context.getString("password");
            Preconditions.checkNotNull(password, "password must be set!!");
            batchSize = context.getInteger("batchSize", 100);
            Preconditions.checkNotNull(batchSize > 0, "batchSize must be a positive number!!");
        }
     
        @Override
        public void start() {
            super.start();
            try {
                //调用Class.forName()方法加载驱动程序
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
     
            String url = "jdbc:mysql://" + hostname + ":" + port + "/" + databaseName; 
            //调用DriverManager对象的getConnection()方法,获得一个Connection对象
     
            try {
                conn = DriverManager.getConnection(url, user, password);
                conn.setAutoCommit(false);
                //创建一个Statement对象
                preparedStatement = conn.prepareStatement("insert into " + tableName + 
                                                   " (content) values (?)");
     
            } catch (SQLException e) {
                e.printStackTrace();
                System.exit(1);
            }
     
        }
     
        @Override
        public void stop() {
            super.stop();
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
     
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
     
        @Override
        public Status process() throws EventDeliveryException {
            Status result = Status.READY;
            Channel channel = getChannel();
            Transaction transaction = channel.getTransaction();
            Event event;
            String content;
     
            List<String> actions = Lists.newArrayList();
            transaction.begin();
            try {
                for (int i = 0; i < batchSize; i++) {
                    event = channel.take();
                    if (event != null) {
                        content = new String(event.getBody());
                        actions.add(content);
                    } else {
                        result = Status.BACKOFF;
                        break;
                    }
                }
     
                if (actions.size() > 0) {
                    preparedStatement.clearBatch();
                    for (String temp : actions) {
                        preparedStatement.setString(1, temp);
                        preparedStatement.addBatch();
                    }
                    preparedStatement.executeBatch();
     
                    conn.commit();
                }
                transaction.commit();
            } catch (Throwable e) {
                try {
                    transaction.rollback();
                } catch (Exception e2) {
                    LOG.error("Exception in rollback. Rollback might not have been" +
                            "successful.", e2);
                }
                LOG.error("Failed to commit transaction." +
                        "Transaction rolled back.", e);
                Throwables.propagate(e);
            } finally {
                transaction.close();
            }
     
            return result;
        }
    }
    View Code

    3、pom文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at
    
         http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    -->
    <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>
    
       <parent>
        <artifactId>flume-ng-sinks</artifactId>
        <groupId>org.apache.flume</groupId>
        <version>1.7.0</version>
      </parent>
      <artifactId>pf</artifactId>
      <packaging>jar</packaging>
    
      <name>pf</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.flume</groupId>
                <artifactId>flume-ng-core</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.apache.flume</groupId>
                <artifactId>flume-ng-configuration</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.25</version>
            </dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        
       <!--  <build>
        <plugins>
          <plugin>
            <groupId>org.apache.rat</groupId>
            <artifactId>apache-rat-plugin</artifactId>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
              <execution>
                <goals>
                  <goal>test-jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build> -->
    </project>
    View Code

    4、建库建表

    create database ngmonitor;
    
    create table mysqltest(
        id int(11) NOT NULL AUTO_INCREMENT,
        content varchar(50000) NOT NULL,
        PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
    View Code

    5、打包--> lib目录下(含mysql的jar)

    6、参考conf 

    # example2.conf:
    
    # Name the components on this agent
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1
    
    # Describe/configure the source
    a1.sources.r1.type = spooldir
    a1.sources.r1.spoolDir = /home/wwj/flumeSpool
    a1.sources.r1.fileHeader = false
    a1.sources.r1.maxBackoff = 60000  
    #a1.sources.r1.ignorePattern = ^[s]*
     
    
    # Describe the sink
    a1.sinks.k1.type = com.pf.MysqlSink
    a1.sinks.k1.hostname = localhost
    a1.sinks.k1.port = 3306
    a1.sinks.k1.databaseName = ngmonitor
    a1.sinks.k1.tableName = mysqltest
    a1.sinks.k1.user = root
    a1.sinks.k1.password= 123456
    
    # Use a channel which buffers events in memory
    a1.channels.c1.type = memory
    a1.channels.c1.capacity = 1000
    a1.channels.c1.transactionCapacity = 100
    
    # Bind the source and sink to the channel
    a1.sources.r1.channels = c1
    a1.sinks.k1.channel = c1
    View Code

    7、启动

    bin/flume-ng agent --conf conf --conf-file conf/example2.conf --name a1 -Dflume.root.logger=INFO,console

    8、成功

  • 相关阅读:
    Android开发之Path类使用详解,自绘各种各样的图形!
    json数值和结构
    ajax异步请求不能刷新数据的问题
    关于javaBean中boolean类型变量的set和get注入后传到前端JS中的问题
    Js中的window.parent ,window.top,window.self详解
    db2中修改表字段的长度,查看表字段长度,以及查看表字段已存放值大小
    db2数据库中查找数据库表
    分页查询SQL
    ibatis动态语句加and 和不加and
    win7计划任务执行BAT文件问题
  • 原文地址:https://www.cnblogs.com/sprinng/p/5993419.html
Copyright © 2011-2022 走看看