项目:elastic 搜索数据库内容,达到快速匹配的效果
elasticsearch:
上面是官方链接,我也对它不做过多介绍,有兴趣的小伙伴可以直接跳转。
目前我们是通过elastic node的api调用它的
直接npm install -S elasticsearch
api 的文档地址:https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html
同步mysql的手段:logstash
logstash的功能十分强大
它的运作流程:input ---> filter ---> output
input 可以用任意数据
filter 就是对输入源检索,格式化等
output 就是要输出到的容器
不废话 执行上配置文件
xxx.conf
input {
jdbc {
type => "xxx" // 类似数据库表名称
# mysql jdbc connection string to our backup databse 后面的test对应mysql中的test数据库
jdbc_connection_string => "jdbc:mysql://ip:port/databse"
# the user we wish to excute our statement as
jdbc_user => "xxx"
jdbc_password => "xxx"
record_last_run => "true" //是否记录最后一次更正的值
#use_column_value => "true" // 默认是false 这个跟下面的sql_last_value 有关系,如果为true的话 他的默认值是0(所以数据库有索引最后设置成true)
tracking_column => xxx //你要记录的字段名称(你通过sql语句查询出来的)
last_run_metadata_path => "/config-dir/xxx" 保存记录的文件
clean_run => "false"
# the path to our downloaded jdbc driver
jdbc_driver_library => "/jar/mysql-connector-java-5.1.46-bin.jar" // 这是jdbc依赖的库文件
# the name of the driver class for mysql
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "500"
statement => "select name, id from Test updatetime > :sql_last_value"
#定时字段 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新
schedule => "* * * * * *"
#设定ES索引类型
}
}
filter {
if [type] == "xxx" {
// 不同的database 俩个表联合查询
jdbc_streaming {
jdbc_driver_library => "/jar/mysql-connector-java-5.1.46-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "jdbc:mysql://ip:port/databse"
jdbc_user => "xxx"
jdbc_password => "xxx"
statement => "select * from Test2 where oldname := name "
parameters => { "name" => "name"}
target => "xxx" // 指定生成的名字
}
}
}
output {
if [type] == "xxx" {
elasticsearch {
#ESIP地址与端口
hosts => "127.0.0.1:9200" // elasticsearch 的 server 部署的ip跟端口
#ES索引名称(自己定义的)
index => "%{type}"
#自增ID编号
document_id => "%{id}" // 上面查询的id
}
}
}
在上面的配置我们看到 他需要远程访问mysql
我们要给mysql 设置访问权限
grant all privileges on *.* to username@'ip' identified by "password"; // ip 为%号时 即为任意ip可以访问你的mysql