• Logstash 与 Beats 入门


    公号:码农充电站pro
    主页:https://codeshellme.github.io

    Logstash 是一款免费开放的服务器端数据处理管道,能够从多个来源采集并转换数据,然后将数据发送到后端存储中。

    在这里插入图片描述

    1,Logstash 处理流程

    Logstash 的处理流程分为三个阶段,这三个阶段合称为一个 Pipeline

    • Inputs:将输入数据转换成 Events。
    • Filters:处理修改 Events。
    • Outputs:将 Events 输出到存储。

    在这里插入图片描述

    同时在输入/输出阶段可以对数据进行编解码处理。

    用户通过配置文件告诉 Logstash 如何处理数据。

    2,Logstash 插件

    Logstash 的每个处理阶段都由一个或多个插件来完成,Logstash 目前支持 200 多个插件

    Logstash 的一些常用插件:

    • Inputs 阶段
      • stdin、file
      • beats、log4j
      • elasticsearch、jdbc、kafka、rabbitmq、redis
      • jmx、http、websocket、tcp、udp
    • Filters 阶段
      • mutate
      • metrics
      • ruby
      • csv
    • Outputs 阶段
      • email、pageduty
      • elasticsearch、kafka、mongodb
      • http、tcp、websocket
    • 编码处理
      • line、multiline
      • json、dots

    3,Logstash Queue

    Logstash 在实际处理数据时,会先将输入数据放入队列中,作为缓冲。

    在这里插入图片描述

    Logstash Queue 分为两种:

    • Memory Queue(默认方式):放在内存中;如果意外宕机数据会丢失。
    • Persistent Queue:会进行持久化;意外宕机数据不会丢失。
      • 可通过 queue.max_bytes 参数设置队列能存放的数据大小,默认为 1G。

    4,Logstash 使用示例

    Logstash 通过 -e 参数在命令行指定一个 pipeline,通过 -f 参数指定一个配置文件。

    4.1,-e 参数

    通过 -e 在命令行指定一个 pipeline

    logstash -e "input{stdin{codec=>line}}output{stdout{codec=>rubydebug}}"
    logstash -e "input{stdin{codec=>json}}output{stdout{codec=>rubydebug}}"
    logstash -e "input{stdin{codec=>line}}output{stdout{codec=>dots}}"
    

    4.2,-f 参数

    数据示例,一个 movies.csv 文件:

    movieId,title,genres
    1,Toy Story (1995),Adventure|Animation|Children|Comedy|Fantasy
    2,Jumanji (1995),Adventure|Children|Fantasy
    3,Grumpier Old Men (1995),Comedy|Romance
    4,Waiting to Exhale (1995),Comedy|Drama|Romance
    5,Father of the Bride Part II (1995),Comedy
    6,Heat (1995),Action|Crime|Thriller
    7,Sabrina (1995),Comedy|Romance
    8,Tom and Huck (1995),Adventure|Children
    9,Sudden Death (1995),Action
    10,GoldenEye (1995),Action|Adventure|Thriller
    11,"American President, The (1995)",Comedy|Drama|Romance
    12,Dracula: Dead and Loving It (1995),Comedy|Horror
    13,Balto (1995),Adventure|Animation|Children
    14,Nixon (1995),Drama
    15,Cutthroat Island (1995),Action|Adventure|Romance
    16,Casino (1995),Crime|Drama
    17,Sense and Sensibility (1995),Drama|Romance
    18,Four Rooms (1995),Comedy
    19,Ace Ventura: When Nature Calls (1995),Comedy
    20,Money Train (1995),Action|Comedy|Crime|Drama|Thriller
    

    配置文件 logstash.conf

    input {        # 定义 inputs
      file {       # 一个 file input
        path => "/path/movies.csv"
        start_position => "beginning"
        sincedb_path => "/dev/null"
      }
    }
    
    filter {       # 定义 filters
      csv {        # 一个 csv filter
        separator => ","
        columns => ["id","content","genre"]
      }
    
      mutate {     # 一个 mutate filter
        split => { "genre" => "|" }
        remove_field => ["path", "host","@timestamp","message"]
      }
    
      mutate {     # 一个 mutate filter
        split => ["content", "("]
        add_field => { "title" => "%{[content][0]}"}
        add_field => { "year" => "%{[content][1]}"}
      }
    
      mutate {     # 一个 mutate filter
        convert => {"year" => "integer"}
        strip => ["title"]
        remove_field => ["path", "host","@timestamp","message","content"]
      }
    }
    
    output {             # 定义 outputs
       elasticsearch {   # 一个 elasticsearch output
         hosts => "http://localhost:9200"
         index => "movies"
         document_id => "%{id}"
       }
       stdout {}         # 一个 stdout output
    }
    

    5,Beats 介绍

    在这里插入图片描述

    Beats 是一个轻量型数据采集器,能够方便的与 Logstash 和 ElasticSearch 配合使用。

    Beats 是基于 Golang 开发的。

    在这里插入图片描述

    Beats 官方提供了一些具体的 beats 供我们使用:

    (本节完。)


    欢迎关注作者公众号,获取更多技术干货。

    码农充电站pro

  • 相关阅读:
    kaggle CTR预估
    基于大规模语料的新词发现算法【转自matix67】
    vim E437: terminal capability "cm" required
    makefile 中的符号替换($@、$^、$<、$?)
    【转】Makefile 中:= ?= += =的区别
    python urljoin问题
    python 写文件刷新缓存
    python Popen卡死问题
    nohup 日志切割
    换行和回车野史
  • 原文地址:https://www.cnblogs.com/codeshell/p/14478485.html
Copyright © 2020-2023  润新知