wangJiaLun-go

王加伦的技术分享

0%

基于logstash同步mysql数据到elasticsearch

基于logstash同步mysql数据到elasticsearch

  • 解压 tar -zxvf logstash-6.4.3.tar.gz

  • 下载一个mysql驱动包mysql-connector-java-5.1.41.jar可以从本地maven库上传

  • 创建一个配置文件如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    input {
    jdbc {
    # 设置 MySql/MariaDB 数据库url以及数据库名称
    jdbc_connection_string => "jdbc:mysql://192.168.31.29:3306/foodie-shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect&useSSL=false"
    # 用户名和密码
    jdbc_user => "root"
    jdbc_password => "root1234"
    # 数据库驱动所在位置,可以是绝对路径或者相对路径
    jdbc_driver_library => "/usr/local/logstash-6.4.3/sync/mysql-connector-java-5.1.41.jar"
    # 驱动类名
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    # 开启分页
    jdbc_paging_enabled => "true"
    # 分页每页数量,可以自定义
    jdbc_page_size => "1000"
    # 执行的sql文件路径
    statement_filepath => "/usr/local/logstash-6.4.3/sync/foodie-items.sql"
    # 设置定时任务间隔 含义:分、时、天、月、年,全部为*默认含义为每分钟跑一次任务
    schedule => "* * * * *"
    # 索引类型
    type => "_doc"
    # 是否开启记录上次追踪的结果,也就是上次更新的时间,这个会记录到 last_run_metadata_path 的文件
    use_column_value => true
    # 记录上一次追踪的结果值
    last_run_metadata_path => "/usr/local/logstash-6.4.3/sync/track_time"
    # 如果 use_column_value 为true, 配置本参数,追踪的 column 名,可以是自增id或者时间
    tracking_column => "updated_time"
    # tracking_column 对应字段的类型
    tracking_column_type => "timestamp"
    # 是否清除 last_run_metadata_path 的记录,true则每次都从头开始查询所有的数据库记录
    clean_run => false
    # 数据库字段名称大写转小写
    lowercase_column_names => false
    }
    }

    output {
    elasticsearch {
    # es地址
    hosts => ["192.168.31.226:9200"]
    # 同步的索引名
    index => "foodie-items"
    # 设置_docID和数据相同
    # document_id => "%{id}"
    document_id => "%{itemId}"

    # 定义模板名称
    template_name => "myik"
    # 模板所在位置
    template => "/usr/local/logstash-6.4.3/sync/logstash-ik.json"
    # 重写模板
    template_overwrite => true
    # 默认为true, false关闭logstas 自动管理模板功能,如果自定义模板,则设置为false
    manage_template => false
    }

    # 日志输出
    stdout {
    codec => json_lines
    }
    }
  • 参照配置文件中路径创建待执行的sql脚本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    SELECT
    i.item_name AS itemName,
    i.id AS itemId,
    i.sell_counts AS sellCounts,
    ii.url AS imgUrl,
    tempSpec.priceDiscount as price,
    i.updated_time
    FROM
    items i
    LEFT JOIN items_img ii ON i.id = ii.item_id
    LEFT JOIN (
    SELECT item_id, MIN(price_discount) AS priceDiscount FROM items_spec GROUP BY item_id
    ) tempSpec on i.id = tempSpec.item_id
    WHERE
    ii.is_main = 1
    ## :sql_last_value 是logstash维护的上次更新时间
    and i.updated_time >= :sql_last_value
  • 自定义模板配置(配置中文分词) logstash-ik.json

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    {
    "order": 0,
    "version": 1,
    "index_patterns": ["*"],
    "settings": {
    "index": {
    "refresh_interval": "5s"
    }
    },
    "mappings": {
    "_default_": {
    "dynamic_templates": [
    {
    "message_field": {
    "path_match": "message",
    "match_mapping_type": "string",
    "mapping": {
    "type": "text",
    "norms": false
    }
    }
    },
    {
    "string_fields": {
    "match": "*",
    "match_mapping_type": "string",
    "mapping": {
    "type": "text",
    "norms": false,
    "analyzer": "ik_max_word",
    "fields": {
    "keyword": {
    "type": "keyword",
    "ignore_above": 256
    }
    }
    }
    }
    }
    ],
    "properties": {
    "@timestamp": {
    "type": "date"
    },
    "@version": {
    "type": "keyword"
    },
    "geoip": {
    "dynamic": true,
    "properties": {
    "ip": {
    "type": "ip"
    },
    "location": {
    "type": "geo_point"
    },
    "latitude": {
    "type": "half_float"
    },
    "longitude": {
    "type": "half_float"
    }
    }
    }
    }
    }
    },
    "aliases": {}
    }
  • logstash启动命令./logstash -f /usr/local/logstash-6.4.3/sync/logstatsh-db-sync.conf