flume

离线day-day11

01-Flume–软件概述

  • Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集、聚合和传输的软件。
  • 引水渠
    • source
    • channel
    • sink

02- Flume–运行机制&运行结构图

[外链图片转存失败(img-H1Aovbdm-1564197326019)(assert/flume1.png)]

  • Flume系统中核心的角色是agent,agent本身是一个Java进程,一般运行在日志收集节点。

    • Source:采集源,用于跟数据源对接,以获取数据;

    • Sink:下沉地,采集数据的传送目的,用于往下一级agent传递数据或者往最终存储系统传递数据;

    • Channel:agent内部的数据传输通道,用于从source将数据传递到sink;

    • 在整个数据的传输的过程中,流动的是event,它是Flume内部数据传输的最基本单元。

      • 一个完整的event包括:event headers、event body、event信息,其中event信息就是flume收集到的日记记录。

03- Flume–安装部署&简单入门

  • 上传安装包到数据源所在节点上

  • 然后解压 tar -zxvf apache-flume-1.8.0-bin.tar.gz

  • 然后进入flume的目录,修改conf下的flume-env.sh,在里面配置JAVA_HOME

  • 先用一个最简单的例子来测试一下程序环境是否正常

    • vi netcat-logger.conf
    # 定义这个agent中各组件的名字
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1
    
    # 描述和配置source组件:r1
    a1.sources.r1.type = netcat
    a1.sources.r1.bind = localhost
    a1.sources.r1.port = 44444
    
    # 描述和配置sink组件:k1
    a1.sinks.k1.type = logger
    
    # 描述和配置channel组件,此处使用是内存缓存的方式
    a1.channels.c1.type = memory
    a1.channels.c1.capacity = 1000
    a1.channels.c1.transactionCapacity = 100
    
    # 描述和配置source  channel   sink之间的连接关系
    a1.sources.r1.channels = c1
    a1.sinks.k1.channel = c1
    
  • 启动agent去采集数据

    bin/flume-ng agent -c conf -f conf/netcat-logger.conf -n a1  -Dflume.root.logger=INFO,console
    

​ -c conf 指定flume自身的配置文件所在目录

​ -f conf/netcat-logger.con 指定我们所描述的采集方案

​ -n a1 指定我们这个agent的名字

04-Flume–案例–监控采集文件夹变化(sqoopdir、HDFS)

  • 采集需求:服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就需要把文件采集到HDFS中去;

  • l 采集源,即source——监控文件目录 : spooldir

    l 下沉目标,即sink——HDFS文件系统 : hdfs sink

    l source和sink之间的传递通道——channel,可用file channel 也可以用内存channel

    # 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 = /root/logs
    a1.sources.r1.fileHeader = true
    
    # Describe the sink
    a1.sinks.k1.type = hdfs
    a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/
    a1.sinks.k1.hdfs.filePrefix = events-
    a1.sinks.k1.hdfs.round = true
    a1.sinks.k1.hdfs.roundValue = 10
    a1.sinks.k1.hdfs.roundUnit = minute
    a1.sinks.k1.hdfs.rollInterval = 3
    a1.sinks.k1.hdfs.rollSize = 20
    a1.sinks.k1.hdfs.rollCount = 5
    a1.sinks.k1.hdfs.batchSize = 1
    a1.sinks.k1.hdfs.useLocalTimeStamp = true
    #生成的文件类型,默认是Sequencefile,可用DataStream,则为普通文本
    a1.sinks.k1.hdfs.fileType = DataStream
    
    # 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
    

​ capacity:默认该通道中最大的可以存储的event数量

​ trasactionCapacity:每次最大可以从source中拿到或者送到sink中的event数量

05-Flume–案例–监控采集文件夹变化–执行演示&注意事项

启动命令:  
bin/flume-ng agent -c ./conf -f ./conf/spool-hdfs.conf -n a1 -Dflume.root.logger=INFO,console

测试: 往/home/hadoop/flumeSpool放文件(mv ././xxxFile /home/hadoop/flumeSpool),但是不要在里面生成文件

06-Flume–案例–监控文件变化(exec source)

  • 采集需求:比如业务系统使用log4j生成的日志,日志内容不断增加,需要把追加到日志文件中的数据实时采集到hdfs

    • l 采集源,即source——监控文件内容更新 : exec ‘tail -F file’

      l 下沉目标,即sink——HDFS文件系统 : hdfs sink

      l Source和sink之间的传递通道——channel,可用file channel 也可以用 内存channel

    # Name the components on this agent
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1
    
    # Describe/configure the source
    a1.sources.r1.type = exec
    a1.sources.r1.command = tail -F /root/logs/test.log
    a1.sources.r1.channels = c1
    
    # Describe the sink
    a1.sinks.k1.type = hdfs
    a1.sinks.k1.hdfs.path = /flume/tailout/%y-%m-%d/%H%M/
    a1.sinks.k1.hdfs.filePrefix = events-
    a1.sinks.k1.hdfs.round = true
    a1.sinks.k1.hdfs.roundValue = 10
    a1.sinks.k1.hdfs.roundUnit = minute
    a1.sinks.k1.hdfs.rollInterval = 3
    a1.sinks.k1.hdfs.rollSize = 20
    a1.sinks.k1.hdfs.rollCount = 5
    a1.sinks.k1.hdfs.batchSize = 1
    a1.sinks.k1.hdfs.useLocalTimeStamp = true
    #生成的文件类型,默认是Sequencefile,可用DataStream,则为普通文本
    a1.sinks.k1.hdfs.fileType = DataStream
    
    # 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
    
  • 启动命令

    启动命令:
    bin/flume-ng agent -c conf -f conf/tail-hdfs.conf -n a1 -Dflume.root.logger=INFO,console
    

07-Flume–高阶–负载均衡功能

  • 负载均衡是用于解决一台机器(一个进程)无法解决所有请求而产生的一种算法。Load balancing Sink Processor能够实现load balance功能[外链图片转存失败(img-4SxPfjqI-1564197326020)(assert/flume2.png)]

  • vi exec-avro.conf

    #agent1 name
    agent1.channels = c1
    agent1.sources = r1
    agent1.sinks = k1 k2
    
    #set gruop
    agent1.sinkgroups = g1
    
    #set channel
    agent1.channels.c1.type = memory
    agent1.channels.c1.capacity = 1000
    agent1.channels.c1.transactionCapacity = 100
    
    agent1.sources.r1.channels = c1
    agent1.sources.r1.type = exec
    agent1.sources.r1.command = tail -F /root/logs3/123.log
    
    # set sink1
    agent1.sinks.k1.channel = c1
    agent1.sinks.k1.type = avro
    agent1.sinks.k1.hostname = node02
    agent1.sinks.k1.port = 52020
    
    # set sink2
    agent1.sinks.k2.channel = c1
    agent1.sinks.k2.type = avro
    agent1.sinks.k2.hostname = node03
    agent1.sinks.k2.port = 52020
    
    #set sink group
    agent1.sinkgroups.g1.sinks = k1 k2
    
    #set failover
    agent1.sinkgroups.g1.processor.type = load_balance
    agent1.sinkgroups.g1.processor.backoff = true
    agent1.sinkgroups.g1.processor.selector = round_robin
    agent1.sinkgroups.g1.processor.selector.maxTimeOut=10000
    
    
  • 启动命令

    bin/flume-ng agent -c conf -f conf/exec-avro.conf -n agent1 -Dflume.root.logger=INFO,console
    
  • vi avro-logger.conf (在node02上)

    # Name the components on this agent
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1
    
    # Describe/configure the source
    a1.sources.r1.type = avro
    a1.sources.r1.channels = c1
    a1.sources.r1.bind = node02
    a1.sources.r1.port = 52020
    
    # Describe the sink
    a1.sinks.k1.type = logger
    
    # 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
    
  • vi avro-logger.conf(在node03上)

    # Name the components on this agent
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1
    
    # Describe/configure the source
    a1.sources.r1.type = avro
    a1.sources.r1.channels = c1
    a1.sources.r1.bind = node03
    a1.sources.r1.port = 52020
    
    # Describe the sink
    a1.sinks.k1.type = logger
    
    # 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
    

    bin/flume-ng agent -c conf -f conf/avro-logger.conf -n a1 -Dflume.root.logger=INFO,console

08-Flume–高阶–容错(故障转移)功能

  • Failover Sink Processor能够实现failover功能,具体流程类似load balance,但是内部处理机制与load balance完全不同。

  • vi exec-avro.conf

    
    #agent1 name
    agent1.channels = c1
    agent1.sources = r1
    agent1.sinks = k1 k2
    
    #set gruop
    agent1.sinkgroups = g1
    
    #set channel
    agent1.channels.c1.type = memory
    agent1.channels.c1.capacity = 1000
    agent1.channels.c1.transactionCapacity = 100
    
    agent1.sources.r1.channels = c1
    agent1.sources.r1.type = exec
    agent1.sources.r1.command = tail -F /root/logs/456.log
    
    # set sink1
    agent1.sinks.k1.channel = c1
    agent1.sinks.k1.type = avro
    agent1.sinks.k1.hostname = node02
    agent1.sinks.k1.port = 52020
    
    # set sink2
    agent1.sinks.k2.channel = c1
    agent1.sinks.k2.type = avro
    agent1.sinks.k2.hostname = node03
    agent1.sinks.k2.port = 52020
    
    #set sink group
    agent1.sinkgroups.g1.sinks = k1 k2
    
    #set failover
    agent1.sinkgroups.g1.processor.type = failover
    agent1.sinkgroups.g1.processor.priority.k1 = 10
    agent1.sinkgroups.g1.processor.priority.k2 = 1
    agent1.sinkgroups.g1.processor.maxpenalty = 10000
    

    bin/flume-ng agent -c conf -f conf/exec-avro.conf -n agent1 -Dflume.root.logger=INFO,console

  • vi avro-logger.conf (在node02 )

    
    # Name the components on this agent
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1
    
    # Describe/configure the source
    a1.sources.r1.type = avro
    a1.sources.r1.channels = c1
    a1.sources.r1.bind = node02
    a1.sources.r1.port = 52020
    
    # Describe the sink
    a1.sinks.k1.type = logger
    
    # 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
    
  • vi avro-logger.conf (在node03)

    # Name the components on this agent
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1
    
    # Describe/configure the source
    a1.sources.r1.type = avro
    a1.sources.r1.channels = c1
    a1.sources.r1.bind = node03
    a1.sources.r1.port = 52020
    
    # Describe the sink
    a1.sinks.k1.type = logger
    
    # 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
    
  • bin/flume-ng agent -c conf -f conf/avro-logger.conf -n a1 -Dflume.root.logger=INFO,console

09-Flume–静态拦截器–案例业务需求描述

  • A、B两台日志服务机器实时生产日志主要类型为access.log、nginx.log、web.log 。现在要求:把A、B 机器中的access.log、nginx.log、web.log 采集汇总到C机器上然后统一收集到hdfs中。但是在hdfs中要求的目录为:

    /source/logs/access/20160101/**

    /source/logs/nginx/20160101/**

    /source/logs/web/20160101/**

10-Flume–静态拦截器–功能实现

  • vi exec_source_avro_sink.conf

    #Name the components on this agent
    
    a1.sources = r1 r2 r3
    
    a1.sinks = k1
    
    a1.channels = c1
    
    #Describe/configure the source
    
    a1.sources.r1.type = exec
    
    a1.sources.r1.command = tail -F /root/logs/access.log
    
    a1.sources.r1.interceptors = i1
    
    a1.sources.r1.interceptors.i1.type = static
    
    a1.sources.r1.interceptors.i1.key = type
    
    a1.sources.r1.interceptors.i1.value = access
    
    a1.sources.r2.type = exec
    
    a1.sources.r2.command = tail -F /root/logs/nginx.log
    
    a1.sources.r2.interceptors = i2
    
    a1.sources.r2.interceptors.i2.type = static
    
    a1.sources.r2.interceptors.i2.key = type
    
    a1.sources.r2.interceptors.i2.value = nginx
    
    a1.sources.r3.type = exec
    
    a1.sources.r3.command = tail -F /root/logs/web.log
    
    a1.sources.r3.interceptors = i3
    
    a1.sources.r3.interceptors.i3.type = static
    
    a1.sources.r3.interceptors.i3.key = type
    
    a1.sources.r3.interceptors.i3.value = web
    
    #Describe the sink
    
    a1.sinks.k1.type = avro
    
    a1.sinks.k1.hostname = node02
    
    a1.sinks.k1.port = 41414
    
    #Use a channel which buffers events in memory
    
    a1.channels.c1.type = memory
    
    a1.channels.c1.capacity = 2000
    
    a1.channels.c1.transactionCapacity = 100
    
    #Bind the source and sink to the channel
    
    a1.sources.r1.channels = c1
    
    a1.sources.r2.channels = c1
    
    a1.sources.r3.channels = c1
    
    a1.sinks.k1.channel = c1
    
    
  • vi avro_source_hdfs_sink.conf

    
    #定义agent名, source、channel、sink的名称
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1
    #定义source
    
    a1.sources.r1.type = avro
    
    a1.sources.r1.bind = node02
    
    a1.sources.r1.port =41414
    
    #添加时间拦截器
    
    a1.sources.r1.interceptors = i1
    
    a1.sources.r1.interceptors.i1.type = org.apache.flume.interceptor.TimestampInterceptor$Builder
    
    #定义channels
    
    a1.channels.c1.type = memory
    
    a1.channels.c1.capacity = 20000
    
    a1.channels.c1.transactionCapacity = 10000
    
    #定义sink
    
    a1.sinks.k1.type = hdfs
    
    a1.sinks.k1.hdfs.path=hdfs://node01:50070/source/logs/%{type}/%Y%m%d
    
    a1.sinks.k1.hdfs.filePrefix =events
    
    a1.sinks.k1.hdfs.fileType = DataStream
    
    a1.sinks.k1.hdfs.writeFormat = Text
    
    #时间类型
    
    a1.sinks.k1.hdfs.useLocalTimeStamp = true
    
    #生成的文件不按条数生成
    
    a1.sinks.k1.hdfs.rollCount = 0
    
    #生成的文件不按时间生成
    
    a1.sinks.k1.hdfs.rollInterval = 20
    
    #生成的文件按大小生成
    
    a1.sinks.k1.hdfs.rollSize  = 10485760
    
    #批量写入hdfs的个数
    
    a1.sinks.k1.hdfs.batchSize = 20
    
    #flume操作hdfs的线程数(包括新建,写入等)
    
    a1.sinks.k1.hdfs.threadsPoolSize=10
    
    #操作hdfs超时时间
    
    a1.sinks.k1.hdfs.callTimeout=30000
    
    #组装source、channel、sink
    
    a1.sources.r1.channels = c1
    
    a1.sinks.k1.channel = c1
    
    

11-Flume–自定义拦截器–需求描述

  • 根据实际业务的需求,为了更好的满足数据在应用层的处理,通过自定义Flume拦截器,过滤掉不需要的字段,并对指定字段加密处理,将源数据进行预处理。减少了数据的传输量,降低了存储的开销。

12-Flume–自定义拦截器–代码逻辑梳理

  • pom.xml

    
    <?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>cn.itcast.cloud</groupId>
        <artifactId>example-flume-intercepter</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.flume</groupId>
                <artifactId>flume-ng-sdk</artifactId>
                <version>1.8.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.flume</groupId>
                <artifactId>flume-ng-core</artifactId>
                <version>1.8.0</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.0</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                        <!--    <verbal>true</verbal>-->
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.1.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <filters>
                                    <filter>
                                        <artifact>*:*</artifact>
                                        <excludes>
                                            <exclude>META-INF/*.SF</exclude>
                                            <exclude>META-INF/*.DSA</exclude>
                                            <exclude>META-INF/*.RSA</exclude>
                                        </excludes>
                                    </filter>
                                </filters>
                                <transformers>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass></mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    
  • java代码

    package cn.itcast.interceptor;
    
    import com.google.common.base.Charsets;
    import org.apache.flume.Context;
    import org.apache.flume.Event;
    import org.apache.flume.interceptor.Interceptor;
    
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import static cn.itcast.interceptor.CustomParameterInterceptor.Constants.*;
    
    /**
     * Created by itcast
     */
    public class CustomParameterInterceptor implements Interceptor{
        /** The field_separator.指明每一行字段的分隔符 */
      private final String fields_separator;
    
      /** The indexs.通过分隔符分割后,指明需要那列的字段 下标*/
      private final String indexs;
    
      /** The indexs_separator. 多个下标的分隔符*/
      private final String indexs_separator;
    
      /** The encrypted_field_index. 需要加密的字段下标*/
      private final String encrypted_field_index;
      /**
       *
       */
      public CustomParameterInterceptor( String fields_separator,
                                         String indexs, String indexs_separator,String encrypted_field_index) {
          String f = fields_separator.trim();
          String i = indexs_separator.trim();
          this.indexs = indexs;
          this.encrypted_field_index=encrypted_field_index.trim();
          if (!f.equals("")) {
              f = UnicodeToString(f);
          }
          this.fields_separator =f;
          if (!i.equals("")) {
              i = UnicodeToString(i);
          }
          this.indexs_separator = i;
      }
    

/*
 *
*  \t 制表符 ('\u0009')
    *
    	 */

  public static String UnicodeToString(String str) {
      Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
      Matcher matcher = pattern.matcher(str);
      char ch;
      while (matcher.find()) {
          ch = (char) Integer.parseInt(matcher.group(2), 16);
          str = str.replace(matcher.group(1), ch + "");
      }
      return str;
  }

  /*
   * @see org.apache.flume.interceptor.Interceptor#intercept(org.apache.flume.Event)
   */
  public Event intercept(Event event) {
      if (event == null) {
          return null;
      }
      try {
          String line = new String(event.getBody(), Charsets.UTF_8);
          String[] fields_spilts = line.split(fields_separator);
          String[] indexs_split = indexs.split(indexs_separator);
          String newLine="";
          for (int i = 0; i < indexs_split.length; i++) {
              int parseInt = Integer.parseInt(indexs_split[i]);
              //对加密字段进行加密
              if(!"".equals(encrypted_field_index)&&encrypted_field_index.equals(indexs_split[i])){
                  newLine+=StringUtils.GetMD5Code(fields_spilts[parseInt]);
              }else{
                  newLine+=fields_spilts[parseInt];
              }

              if(i!=indexs_split.length-1){
                  newLine+=fields_separator;
              }
          }
          event.setBody(newLine.getBytes(Charsets.UTF_8));
          return event;
      } catch (Exception e) {
          return event;
      }
  }

  /*
   * @see org.apache.flume.interceptor.Interceptor#intercept(java.util.List)
   */
  public List<Event> intercept(List<Event> events) {
      List<Event> out = new ArrayList<Event>();
      for (Event event : events) {
          Event outEvent = intercept(event);
          if (outEvent != null) {
              out.add(outEvent);
          }
      }
      return out;
  }

  /*
   * @see org.apache.flume.interceptor.Interceptor#initialize()
   */
  public void initialize() {
      // TODO Auto-generated method stub

  }

  /*
   * @see org.apache.flume.interceptor.Interceptor#close()
   */
  public void close() {
      // TODO Auto-generated method stub

  }



  public static class Builder implements Interceptor.Builder {

      /** The fields_separator.指明每一行字段的分隔符 */
      private  String fields_separator;

      /** The indexs.通过分隔符分割后,指明需要那列的字段 下标*/
      private  String indexs;

      /** The indexs_separator. 多个下标下标的分隔符*/
      private  String indexs_separator;

      /** The encrypted_field. 需要加密的字段下标*/
      private  String encrypted_field_index;

      /*
       * @see org.apache.flume.conf.Configurable#configure(org.apache.flume.Context)
       */
      public void configure(Context context) {
          fields_separator = context.getString(FIELD_SEPARATOR, DEFAULT_FIELD_SEPARATOR);
          indexs = context.getString(INDEXS, DEFAULT_INDEXS);
          indexs_separator = context.getString(INDEXS_SEPARATOR, DEFAULT_INDEXS_SEPARATOR);
          encrypted_field_index= context.getString(ENCRYPTED_FIELD_INDEX, DEFAULT_ENCRYPTED_FIELD_INDEX);

      }

      /*
       * @see org.apache.flume.interceptor.Interceptor.Builder#build()
       */
      public Interceptor build() {

          return new CustomParameterInterceptor(fields_separator, indexs, indexs_separator,encrypted_field_index);
      }
  }




  /**
   * The Class Constants.
   *
   */
  public static class Constants {
      /** The Constant FIELD_SEPARATOR. */
      public static final String FIELD_SEPARATOR = "fields_separator";

      /** The Constant DEFAULT_FIELD_SEPARATOR. */
      public static final String DEFAULT_FIELD_SEPARATOR =" ";

      /** The Constant INDEXS. */
      public static final String INDEXS = "indexs";

      /** The Constant DEFAULT_INDEXS. */
      public static final String DEFAULT_INDEXS = "0";

      /** The Constant INDEXS_SEPARATOR. */
      public static final String INDEXS_SEPARATOR = "indexs_separator";

      /** The Constant DEFAULT_INDEXS_SEPARATOR. */
      public static final String DEFAULT_INDEXS_SEPARATOR = ",";

      /** The Constant ENCRYPTED_FIELD_INDEX. */
      public static final String ENCRYPTED_FIELD_INDEX = "encrypted_field_index";

      /** The Constant DEFAUL_TENCRYPTED_FIELD_INDEX. */
      public static final String DEFAULT_ENCRYPTED_FIELD_INDEX = "";

      /** The Constant PROCESSTIME. */
      public static final String PROCESSTIME = "processTime";
      /** The Constant PROCESSTIME. */
      public static final String DEFAULT_PROCESSTIME = "a";

  }



  /**
   * 字符串md5加密
   */
  public static class StringUtils {
      // 全局数组
      private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
              "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

      // 返回形式为数字跟字符串
      private static String byteToArrayString(byte bByte) {
          int iRet = bByte;
          // System.out.println("iRet="+iRet);
          if (iRet < 0) {
              iRet += 256;
          }
          int iD1 = iRet / 16;
          int iD2 = iRet % 16;
          return strDigits[iD1] + strDigits[iD2];
      }

      // 返回形式只为数字
      private static String byteToNum(byte bByte) {
          int iRet = bByte;
          System.out.println("iRet1=" + iRet);
          if (iRet < 0) {
              iRet += 256;
          }
          return String.valueOf(iRet);
      }

      // 转换字节数组为16进制字串
      private static String byteToString(byte[] bByte) {
          StringBuffer sBuffer = new StringBuffer();
          for (int i = 0; i < bByte.length; i++) {
              sBuffer.append(byteToArrayString(bByte[i]));
          }
          return sBuffer.toString();
      }

      public static String GetMD5Code(String strObj) {
          String resultString = null;
          try {
              resultString = new String(strObj);
              MessageDigest md = MessageDigest.getInstance("MD5");
              // md.digest() 该函数返回值为存放哈希值结果的byte数组
              resultString = byteToString(md.digest(strObj.getBytes()));
          } catch (NoSuchAlgorithmException ex) {
              ex.printStackTrace();
          }
          return resultString;
      }
  }
    }

13-Flume–自定义拦截器–功能实现

14-Flume–自定义source(扩展)–需求、代码逻辑梳理

  • Source是负责接收数据到Flume Agent的组件。Source组件可以处理各种类型、各种格式的日志数据,包括avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy。官方提供的source类型已经很多,但是有时候并不能满足实际开发当中的需求,此时我们就需要根据实际需求自定义某些source。
  • 如:实时监控MySQL,从MySQL中获取数据传输到HDFS或者其他存储框架,所以此时需要我们自己实现MySQLSource

15-Flume–自定义source(扩展)–功能测试实现

16-Flume–自定义sink(扩展)–数据写入本地

  • 同自定义source类似,对于某些sink如果没有我们想要的,我们也可以自定义sink实现将数据保存到我们想要的地方去,例如kafka,或者mysql,或者文件等等都可以
  • 需求:从网络端口当中发送数据,自定义sink,使用sink从网络端口接收数据,然后将数据保存到本地文件当中去。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值