Logstash调研mysql oracle 全量/增量导入es方案

Logstash 简单介绍   

Logstash 是一款日志收集工具可以将收集到的日志同步到es中,Logstash提供了input 和 output 插件进行数据收集,其中有Jdbc input plugin 支持关系型数据库读取,elasticsearch output plugin 输出到es中。

 

 

Logstash配置文件说明

    在下载的安装包中的conf/ 编写配置,配置xxx.conf 怎个配置文件主要包含input 和 output 两个模块,中间的filters是解析和转换数据用的。

官网提供的Example:

input {
  jdbc {
    jdbc_driver_library => "mysql-connector-java-5.1.36-bin.jar"
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_connection_string => "jdbc:mysql://localhost:3306/mydb"
    jdbc_user => "mysql"
    parameters => { "favorite_artist" => "Beethoven" }
    schedule => "* * * * *"
    statement => "SELECT * from songs where artist = :favorite_artist"
  }
}

配置说明:

jdbc_driver_library        数据库连接包

jdbc_driver_class               数据库连接驱动

jdbc_connection_string      数据库连接地址

jdbc_user                  数据库用户名

jdbc_password              数据库密码

schedule                   多久扫描一次

sql_last_value            仅提取自上次运行以来更改的数据(实际上是时间值如果设置use_column_value为true,tracking_column(需要配置以哪个字段为增量更新)则设置为 0在随后的查询运行之后,将对其进行相应的更新。)

parameters       查询参数的散列,例如 { "target_id" => "321" }

jdbc_fetch_size      JDBC提取大小。如果未提供,将使用各自的驱动

jdbc_page_size             JDBC页面大小

jdbc_paging_enabled               是否分页

last_run_metadata_path     保存sql_last_value的地址

 mysql 全量/增量导入脚本

input {

  jdbc {

    jdbc_driver_library => "/home/mysql/mysql_connector/mysql-connector-java-8.0.13.jar"

    jdbc_driver_class => "com.mysql.cj.jdbc.Driver"

    jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"

    jdbc_user => "root"

    jdbc_password => "Aa123456"

    schedule => "* * * * *"

    statement => "select * from test WHERE id >= :sql_last_value"     #仅提取自上次运行以来更改的数据一般使用自增id或者使用update_time 最后更新时间

    use_column_value => true                                     #是否使用column 作为增量字段

    tracking_column_type => "numeric"                            # 增量的类型  numeric(默认),timestamp 

    tracking_column => "id"                                      # 以id做为增量值
      
    jdbc_page_size => 150000                                    # 分页显示的条数

    jdbc_paging_enabled => true                                  #是否分页

    last_run_metadata_path => "/etc/logstash/sql_last_value"      # 记录存在数据库的最后一个增量值,本例子为id
c
  }

}

output {

    elasticsearch {

        hosts => ["127.0.0.1:9200"]

        index => "logstash05"

        document_id => "%{id}"

        document_type => "_doc"

    }

    stdout {
        codec => json_lines
    }

}

 

oracle 全量/增量更新脚本

input {

  stdin {

  }

  jdbc {

    codec => plain { charset => "UTF-8"}

      jdbc_connection_string => "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = ordb)))"

      jdbc_driver_library => "/home/oracle/11.2.0.4.0-atlassian-hosted/ojdbc6-11.2.0.4.0-atlassian-hosted.jar"  

      jdbc_driver_class => "Java::oracle.jdbc.driver.OracleDriver"

      jdbc_user => "user1"

      jdbc_password => "123456"

      jdbc_paging_enabled => "true"   

      jdbc_page_size => "150000"  

      statement => "select * from  student where update_time>= :sql_last_value"    #以时间为增量更新

      schedule => "* * * * *"

  }

}

 

# 把oracle导入到elasticSearch

output {

    elasticsearch {

        index => "oracle_001" 

        document_type => "_doc"   

        hosts => "localhost:9200"  

        document_id => "%{id}" 

    }

}

 

启动配置

  在pipelines.yml 中配置多管道,然后启动bin下面的logstash 就好了。

- pipeline.id: my-pipeline_1
  path.config: "/etc/path/to/p1.config"
  pipeline.workers: 3
- pipeline.id: my-other-pipeline
  path.config: "/etc/different/path/p2.cfg"
  queue.type: persisted

 

其他方面测试:

经测试100万条数据耗时大概12分钟左右(2核 4g)

附上logstash   官网  https://www.elastic.co/guide/en/logstash/current/index.html

 

posted @ 2020-09-07 10:08  午后一杯红茶  阅读(707)  评论(0编辑  收藏  举报