洞察金融行业需要转型,如何利用鸿蒙app开发提升运营效率
900
2022-08-31
logstash同步mysql数据到elasticsearch
更多知识,可以前往我的个人博客网站: 等待下一个秋
1. mysql建表test;
2. 安装logstash(跟es版本一致)
# -wget 解压tar -zxvf logstash-7.14.2-linux-x86_64.tar.gz# 需要mysql-connector-java-5.1.40.jar,随便放到比如目录# /var/lib/hadoop-hdfs/logstash-7.14.2/lib/mysql-connector-java-5.1.40.jar
3. 新建es索引test
curl -u elastic:changeme -X PUT -H 'Content-Type: application/json' -d'{ "settings" : { "number_of_shards" : 1, "number_of_replicas" : 1 }, "mappings" : { "properties": { "id": { "type" : "long" }, "type": { "type": "keyword" }, "keyword_1": { "type": "text", "analyzer" : "ik_smart" }, "keyword_2": { "type": "text", "analyzer" : "ik_smart" }, "keyword_3": { "type": "text", "analyzer" : "ik_smart" }, "data": { "type": "keyword" }, "created_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd" }, "updated_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd" } } }}'
4. 编辑配置文件,vim ~/script/logstash/logstash_mysql2es.conf
input { stdin{ } jdbc{ # 连接的数据库地址和数据库,指定编码格式,禁用ssl协议,设定自动重连 # 此处10.112.103.2为MySQL所在IP地址,也是elastic search所在IP地址 jdbc_connection_string => "jdbc:mysql://192.168.13.28:3306/test?characterEncoding=UTF-8&useSSL=FALSE&autoReconnect=true" #数据库用户名 jdbc_user => "root" # 数据库用户名对应的密码 jdbc_password => "root" # jar包存放位置 jdbc_driver_library => "/var/lib/hadoop-hdfs/logstash-7.14.2/lib/mysql-connector-java-5.1.40.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" jdbc_default_timezone => "Asia/Shanghai" jdbc_paging_enabled => "true" jdbc_page_size => "320000" lowercase_column_names => false statement => "select id, type, tags, title from test" }}filter { # 移除无关的字段 mutate { remove_field => ["@version", "@timestamp"] }}output { elasticsearch { hosts => [" user => "elastic" password => "changeme" index => "test" document_type => "_doc" # 将字段type和id作为文档id document_id => "%{type}_%{id}" } stdout { codec => json_lines }}
重要配置参数说明:
remove_field => ["@version", "@timestamp"]: 默认logstash会添加这两个字段,这里去掉;document_id => "%{type}_%{id}": 将两个字段拼接作为es的文档id;
5. 启动任务
./logstash-7.14.2/bin/logstash -f script/logstash/logstash_mysql2es.conf
参考资料
Jdbc input pluginElasticsearch output plugin通过logstash将mysql数据同步到elastic search
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~