简体   繁体   English

mysql 关系数据库同步到弹性搜索的最佳实践

[英]Best practice for mysql relational database syncing to elastic search

I have a user field which depends on lot of other fields, it has multiple foreign keys to other table and have many-to-many relation using pivot table and one to many relations.我有一个用户字段,它依赖于许多其他字段,它具有其他表的多个外键,并且使用 pivot 表和一对多关系具有多对多关系。

The API is like the nested JSON structure. API 类似于嵌套的 JSON 结构。 The relational data looks like this for a particular user.对于特定用户,关系数据看起来像这样。

user = {
  id : _212dasd,
  name: "alphabeta",
  key1_id = 23,
  key1 : {
     id: 23,
     name: "new name"
  },
  key2 : [
      {
         id: _asdak61526,
         key2_id : 2
         user_id : _212dasd,

      },
      {
         id: _asdak6152a,
         key2_id : 3
         user_id : _212dasd
      }
  ],
  key3: {
     name : "gama",
     key_3_nested : {
       name : "abc",
       key_3_nested_levle2 : {
            name : "xyz",
       }
     }
   }
 }

How can I have mysql relational data synced to elastic search, as most read will go with the elastic search, I am using log-stash for syncing.我怎样才能将 mysql 关系数据同步到弹性搜索,因为大多数读取将 go 与弹性搜索,我使用 log-stash 进行同步。

input {
  jdbc { 
    jdbc_connection_string => "jdbc:mysql://localhost:3306/database_name"
    # The user we wish to execute our statement as
    jdbc_user => "root"
    jdbc_password => "root"
    # The path to our downloaded jdbc driver
    jdbc_driver_library => "/~/mysql-connector-java-5.1.17.jar"
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    # our query
    statement => "SELECT * FROM users INNER JOIN table2 ON table2.user_id = users.id" // just a demo query
    schedule => "* * * * * *"
    }
  }
output {
  elasticsearch {
    document_id=> "%{sk_id}.%{user_id}"
    document_type => "doc"
    index => "db_prd"
    hosts => ["http://localhost:9200"]
  }
  stdout{
    codec => json_lines 
  }
}

Question: How can I maintain the relation, is creation multiple join query a good idea as every time it will make a multiple big join on lot of users data and its not optimised structure.问题:如何维护关系,创建多重连接查询是一个好主意,因为每次它都会对大量用户数据进行多重大连接,并且结构没有优化。 For 10000 users and join to multiple tables will slow the sync.对于 10000 个用户并加入多个表会减慢同步速度。 Will it sync the every index or only changed one?它会同步每个索引还是只更改一个?

How can I get the nested structure on the API from elastic search.如何从弹性搜索中获取 API 上的嵌套结构。

You can use jdbcstream in filter to add Nested object like:您可以在过滤器中使用 jdbcstream 来添加嵌套的 object,例如:

jdbc_streaming {
            jdbc_driver_library => "/~/mysql-connector-java-5.1.17.jar"
            jdbc_driver_class => "org.mariadb.jdbc.Driver"
            jdbc_connection_string =>  "jdbc:mysql://localhost:3306/database_name"
            jdbc_user => "root"
            jdbc_password => "12345678@Abc"
            statement => "SELECT * FROM table2 ON table2.user_id = cus_id"
            parameters => {"cus_id" => "id"}
            target => "table2"
          }

It will create nested "table2" with data is row responsed from statement => "SELECT * FROM table2 ON table2.user_id = cus_id" with "id" in statement => "SELECT * FROM users;"它将创建嵌套的“table2”,其中数据是从语句 => "SELECT * FROM table2 ON table2.user_id = cus_id" with "id" => "SELECT * FROM users;"响应的行( fix of this statement => "SELECT * FROM users INNER JOIN table2 ON table2.user_id = users.id" ) (此语句的修复=> "SELECT * FROM users INNER JOIN table2 ON table2.user_id = users.id"

Notice: It will slow your process注意:它会减慢你的进程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM