Logstash同步ES

Logstash 简介

Logstash 是一个数据流引擎:
它是用于数据物流的开源流式 ETL(Extract-Transform-Load)引擎
在几分钟内建立数据流管道
具有水平可扩展及韧性且具有自适应缓冲
不可知的数据源
具有200多个集成和处理器的插件生态系统
使用 Elastic Stack 监视和管理部署

Logstash 是如何工作的?

Laravel

Logstash 包含3个主要部分: 输入(inputs),过滤器(filters)和输出(outputs)。 你必须定义这些过程的配置才能使用 Logstash,尽管不是每一个都必须的。在有些情况下,我们可以甚至没有过滤器。在过滤器的部分,它可以对数据源的数据进行分析,丰富,处理等等。

安装

docker pull logstash:7.12.1
----------------------------------
vim logstash.conf

配置文件如下

input {
 stdin { }
    jdbc {
        #注意mysql连接地址一定要用ip,不能使用localhost等
        jdbc_connection_string => "jdbc:mysql://172.18.12.1:3306/lmrs_2008_shops"
        jdbc_user => "root"
        jdbc_password => "root"
        #数据库重连尝试
        connection_retry_attempts => "3"
        #数据库连接可用校验超时时间,默认为3600s
        jdbc_validation_timeout => "3600"
        #这个jar包的地址是容器内的地址
        jdbc_driver_library => "/etc/logstash/pipeline/mysql-connector-java-8.0.24.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        #开启分页查询(默认是falsejdbc_paging_enabled => "true"
        #单次分页查询条数(默认100000,字段较多的话,可以适当调整这个数值)
        jdbc_page_size => "50000"
        #执行的sql语句
        statement => "select a.id,a.`name`,a.long_name,a.brand_id,a.three_category_id as category_id,a.shop_id,a.price,a.sold_count,a.review_count,a.`status`,a.create_time,a.last_time,b.`name` as category,b.path from lmrs_products as a LEFT JOIN lmrs_product_categorys as b on a.three_category_id = b.id where a.id > :sql_last_value"
        #需要记录查询结果某字段的值时,此字段为true,否则默认tracking_colum为timestamp的值
        use_column_value => true
        #是否将字段名转为小写,默认为true(如果具备序列化或者反序列化,建议设置为falselowercase_column_names => false
        #需要记录的字段,同于增量同步,需要是数据库字段
        tracking_column => id
        #记录字段的数据类型
        tracking_column_type => numeric
        #上次数据存放位置
        record_last_run => true
        #上一个sql_last_value的存放路径,必须在文件中指定字段的初始值
        last_run_metadata_path => "/etc/logstash/pipeline/products.txt"
        #是否清除last_run_metadata_path的记录,需要增量同步这个字段的值必须为false
        clean_run => false
        #同步的频率(分 时 天 月 年)默认为每分钟同步一次
        schedule => "* * * * *"
        type => "_doc"
    }
    jdbc {
        #注意mysql连接地址一定要用ip,不能使用localhost等
        jdbc_connection_string => "jdbc:mysql://172.18.12.1:3306/lmrs_2008_shops"
        jdbc_user => "root"
        jdbc_password => "root"
        #数据库重连尝试
        connection_retry_attempts => "3"
        #数据库连接可用校验超时时间,默认为3600s
        jdbc_validation_timeout => "3600"
        #这个jar包的地址是容器内的地址
        jdbc_driver_library => "/etc/logstash/pipeline/mysql-connector-java-8.0.24.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        #开启分页查询(默认是falsejdbc_paging_enabled => "true"
        #单次分页查询条数(默认100000,字段较多的话,可以适当调整这个数值)
        jdbc_page_size => "50000"
        #执行的sql语句
        statement => "select c.*,d.`name`as category,d.path as category_path from (select b.id,a.`name`,b.`name` as `value`,a.sort as attribute_sort,b.sort as attribute_value_sort,a.category_id from lmrs_attributes as a LEFT JOIN lmrs_attribute_values as b on a.id = b.attribute_id) as c LEFT JOIN lmrs_product_categorys as d on c.category_id = d.id where c.id
> :sql_last_value"
        #需要记录查询结果某字段的值时,此字段为true,否则默认tracking_colum为timestamp的值
        use_column_value => true
        #是否将字段名转为小写,默认为true(如果具备序列化或者反序列化,建议设置为falselowercase_column_names => false
        #需要记录的字段,同于增量同步,需要是数据库字段
        tracking_column => id
        #记录字段的数据类型
        tracking_column_type => numeric
        #上次数据存放位置
        record_last_run => true
        #上一个sql_last_value的存放路径,必须在文件中指定字段的初始值
        last_run_metadata_path => "/etc/logstash/pipeline/attributes.txt"
        #是否清除last_run_metadata_path的记录,需要增量同步这个字段的值必须为false
        clean_run => false
        #同步的频率(分 时 天 月 年)默认为每分钟同步一次
        schedule => "* * * * *"
        type => "attributes"
    }
 }

filter {
    jdbc_streaming {
         jdbc_driver_library => "/etc/logstash/pipeline/mysql-connector-java-8.0.24.jar"
         jdbc_driver_class => "com.mysql.jdbc.Driver"
         jdbc_connection_string => "jdbc:mysql://172.18.12.1:3306/lmrs_2008_shops"
         jdbc_user => "root"
         jdbc_password => "root"
         parameters => { "sensor_identifier" => "id"}
         statement => "select `name`,price from lmrs_product_skus where product_id= :sensor_identifier"
         target => "skus"
    }

    jdbc_streaming {
        jdbc_driver_library => "/etc/logstash/pipeline/mysql-connector-java-8.0.24.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        jdbc_connection_string => "jdbc:mysql://172.18.12.1:3306/lmrs_2008_shops"
        jdbc_user => "root"
        jdbc_password => "root"
        parameters => { "sensor_identifiers" => "id"}
        statement => "select c.`name`,f.`name` as `value` from (select b.`name`,b.id FROM lmrs_product_attribute_values as a LEFT JOIN lmrs_attributes as b on a.attribute_id= b.id where a.product_id = :sensor_identifiers) as c LEFT JOIN(select d.attribute_id,d.`name` from lmrs_attribute_values as d LEFT JOIN lmrs_product_attribute_values as e on d.id = e.attribute_value_id where product_id = :sensor_identifiers) as f on c.id = f.attribute_id GROUP BY f.`name`"
        target => "attributes"
    }
}

output {
    if [type] == "_doc" {
        elasticsearch {
            #注意mysql连接地址一定要用ip,不能使用localhost等
            hosts => "172.18.12.6:9200"
            index => "products"
            document_type => "_doc"
            document_id => "%{id}"
        }
    }
    if [type] == "attributes" {
        elasticsearch {
            #注意mysql连接地址一定要用ip,不能使用localhost等
            hosts => "172.18.12.6:9200"
            index => "attributes"
            document_type => "_doc"
            document_id => "%{id}"
        }
    }
    stdout {
        codec => json_lines
    }
}

索引

PUT /products/
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "long_name":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "brand_id":{
        "type": "integer"
      },
      "category_id":{
        "type":"integer"
      },
      "category":{
        "type": "keyword"
      },
      "category_path":{
        "type": "keyword"
      },
      "shop_id":{
        "type":"integer"
      },
      "price":{
        "type":"scaled_float",
        "scaling_factor":100
      },
      "sold_count":{
        "type":"integer"
      },
      "review_count":{
        "type":"integer"
      },
      "status":{
        "type":"integer"
      },
      "create_time" : {
          "type" : "date"
      },
      "last_time" : {
          "type" : "date"
      },
      "skus":{
        "type": "nested",
        "properties": {
          "name":{
            "type":"text",
            "analyzer": "ik_smart"
          },
          "price":{
            "type":"scaled_float",
            "scaling_factor":100
          }
        }
      },
      "attributes":{
          "type": "nested",
          "properties": {
            "name": { "type": "keyword" },
            "value": { "type": "keyword"}
          }
      }
    }
  }
}



PUT /attributes
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "value": {
        "type": "keyword"
      },
      "category_id": {
        "type": "integer"
      },
      "attribute_sort": {
        "type": "integer"
      },
      "attribute_value_sort": {
        "type": "integer"
      },
      "category": {
        "type": "keyword"
      },
      "category_path": {
        "type": "text"
      }
    }
  }
}

es查询结果

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.18232156,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.18232156,
        "_source" : {
          "status" : 1,
          "category_id" : 440,
          "long_name" : "HUAWEI Mate Book 14 32GB 1TB 触屏 集显",
          "path" : "-425-438-",
          "id" : 2,
          "type" : "_doc",
          "shop_id" : 2,
          "review_count" : 222,
          "attributes" : [
            {
              "value" : "翡冷翠",
              "name" : "颜色"
            },
            {
              "value" : "冰霜银",
              "name" : "颜色"
            },
            {
              "value" : "星际蓝",
              "name" : "颜色"
            },
            {
              "value" : "R5/32GB/1TB 触屏",
              "name" : "配置"
            },
            {
              "value" : "R7/32GB/1TB 触屏",
              "name" : "配置"
            },
            {
              "value" : "集成显卡",
              "name" : "显卡"
            },
            {
              "value" : "官方标配",
              "name" : "类型"
            }
          ],
          "brand_id" : 2,
          "skus" : [
            {
              "name" : "翡冷翠 R5/32GB/1TB 触屏 触屏 集成显卡 官方标配",
              "price" : 7999
            },
            {
              "name" : "翡冷翠 R7/32GB/1TB 触屏 触屏 集成显卡 官方标配",
              "price" : 9999
            },
            {
              "name" : "冰霜银 R5/32GB/1TB 触屏 触屏 集成显卡 官方标配",
              "price" : 7999
            },
            {
              "name" : "冰霜银 R7/32GB/1TB 触屏 触屏 集成显卡 官方标配",
              "price" : 9999
            },
            {
              "name" : "星际蓝 R5/32GB/1TB 触屏 触屏 集成显卡 官方标配",
              "price" : 7999
            },
            {
              "name" : "星际蓝 R7/32GB/1TB 触屏 触屏 集成显卡 官方标配",
              "price" : 9999
            }
          ],
          "@timestamp" : "2021-06-03T08:55:01.124Z",
          "category" : "笔记本电脑",
          "name" : "HUAWEI Mate Book 14",
          "@version" : "1",
          "create_time" : "2021-05-28T20:02:02.000Z",
          "sold_count" : 222,
          "last_time" : "2021-05-28T21:10:04.000Z",
          "price" : 7999.0
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.18232156,
        "_source" : {
          "status" : 1,
          "category_id" : 440,
          "long_name" : "HUAWEI Mate Book 13 16GB 512GB 触屏 集显",
          "path" : "-425-438-",
          "id" : 1,
          "type" : "_doc",
          "shop_id" : 1,
          "review_count" : 1111,
          "attributes" : [
            {
              "value" : "皓月银",
              "name" : "颜色"
            },
            {
              "value" : "深空灰",
              "name" : "颜色"
            },
            {
              "value" : "樱粉金",
              "name" : "颜色"
            },
            {
              "value" : "I5/16GB/512GB 触屏",
              "name" : "配置"
            },
            {
              "value" : "I7/16GB/512GB 触屏",
              "name" : "配置"
            },
            {
              "value" : "集成显卡",
              "name" : "显卡"
            },
            {
              "value" : "官方标配",
              "name" : "类型"
            }
          ],
          "brand_id" : 1,
          "skus" : [
            {
              "name" : "皓月银 I5/16GB/512GB 触屏 集成显卡 官方标配",
              "price" : 6299
            },
            {
              "name" : "皓月银 I7/16GB/512GB 触屏 集成显卡 官方标配",
              "price" : 6599
            },
            {
              "name" : "深空灰 I5/16GB/512GB 触屏 集成显卡 官方标配",
              "price" : 6299
            },
            {
              "name" : "深空灰 I7/16GB/512GB 触屏 集成显卡 官方标配",
              "price" : 6599
            },
            {
              "name" : "樱粉金 I5/16GB/512GB 触屏 集成显卡 官方标配",
              "price" : 6299
            },
            {
              "name" : "樱粉金 I7/16GB/512GB 触屏 集成显卡 官方标配",
              "price" : 6599
            }
          ],
          "@timestamp" : "2021-06-03T08:55:01.122Z",
          "category" : "笔记本电脑",
          "name" : "HUAWEI Mate Book 13",
          "@version" : "1",
          "create_time" : "2021-05-25T15:12:09.000Z",
          "sold_count" : 111,
          "last_time" : "2021-05-25T15:12:14.000Z",
          "price" : 6299.0
        }
      }
    ]
  }
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
有梦想的人睡不着,没有梦想的人睡不醒。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
文章
88
粉丝
21
喜欢
134
收藏
267
排名:228
访问:4.2 万
私信
所有博文
博客标签
redis
1
php
1
laravel
7
docker
3
orm
2
sync
1
pivot
1
detach
2
attach
2
算法
1
递归
1
多对多
1
lnmp环境搭建
1
GO变量
1
GO数据类型
1
IOC注入反转
1
IOC容器的绑定解析过程(绑定单例)
1
原生微信网页授权登录(natapp穿墙)
1
VMwareNAT网卡配置
1
MySQL基础架构
1
redis 主从搭建
1
Sentinel哨兵模式解决故障转移
1
elasticsearch安装
1
elasticsearch集群安装3台
1
安装kibana
1
必须了解的mysql三大日志-binlog、redo log和undo log
1
何处理数据恢复 数据丢失 面试tx的架构师的岗位问的
1
分库分表插入数据
1
创建分库分表(在主从复制的基本上)
1
分库分表总结
1
mysql总结
1
haproxy状态检测脚本(完成高可用)
1
mysql高可用衡搭建(Keepalived)
1
mysql负载均衡搭建(haproxy)
1
mysql主从恢复数据一致性(pt工具-t-table-checksum和pt-table-sync)
1
终于解决了《====》记一次mysql热备份xtrabackup(没有解决问题)
1
mysql事务
1
MYSQL8.0安装
1
Redis-cluster分布式集群搭建部署
1
比Redis-cluster还好的redis分布式集群(twemproxy)
1
Redis缓存穿透/缓存雪崩/缓存击穿(案例:产生的原因 解决方案利/弊)
1
数据结构之MySQL独爱B+树(二叉树、AVL树、红黑树、B树对比)
1
B-tree
1
B+tree
1
Mycat实现mysql的负载均衡读写分离
2
mysql双主双从 搭建配置
1
mycat 双主双从-负载均衡-高可用
1
Mycat垂直分库
1
记一次mysql高可用技术分享
1
【rabbitmq】安装ampq的扩展的踩坑总结
1
PHP操作MongoDB(增删改查)
1
golang总结
5
社区赞助商