1、介绍

对mysql、oracle等数据库数据进行同步到ES有三种做法:一个是通过elasticsearch提供的API进行增删改查,一个就是通过中间件进行数据全量、增量的数据同步,另一个是通过收集日志进行同步。

     明显通过API增上改查比较麻烦,这里介绍的是利用中间件进行数据同步。
 
2、常用的同步中间件的介绍和对比
 
(1)elasticsearch-jdbc独立的第三方工具 https://github.com/jprante/elasticsearch-jdbc 
(2)elasticsearch-river-mysql https://github.com/scharron/elasticsearch-river-mysql 
(3)go-mysql-elasticsearch(国内) https://github.com/siddontang/go-mysql-elasticsearch
 
都可以完成数据同步;
elasticsearch-jdbc更通用,GitHub活跃度很高;
elasticsearch-river-mysql 自2013年后便不再更新;
go-mysql-elasticsearch仍处理开发不稳定阶段;
elasticsearch-river-jdbc和elasticsearch-river-mysql都不支持对删掉的数据进行同步,go-mysql-elasticsearch希望可以改善这个问题。
总的来说,elasticsearch-jdbc更适合使用,对于删掉的数据可以采用API进行同步,或者在数据中不进行物理删除可以避免该问题的出现。
 
3、elasticsearch的安装
这里使用的是2.3.2版本,可以到官方网站下载,这里不提供官方地址,或者访问 http://download.csdn.net/detail/carboncomputer/9648227 下载本篇文章所用到的两个安装包。
得到elasticsearch-2.3.2.tar.gz
 
[zsz@zsz ~]$ tar -zxvf elasticsearch-2.3.2.tar.gz
[zsz@zsz ~]$ mv elasticsearch-2.3.2 /usr/local/elasticsearch-2.3.2
 
启动elasticsearch服务
[zsz@zsz ~]$./bin/elasticsearch
另外,bin/elasticsearch -d(后台运行);
如何需要修改配置,可以查看/elasticsearch-2.3.2/config/elasticsearch.yml;
 
查看节点情况:
[zsz@zsz downloads]$ curl 'localhost:9200/_cat/nodes?v'

host      ip        heap.percent ram.percent load node.role master name  

127.0.0.1 127.0.0.1           12          79 0.18 d         *      node-1
 
查看索引,当前为无索引:
[zsz@zsz downloads]$  curl 'localhost:9200/_cat/indices?v'

health status index pri rep docs.count docs.deleted store.size pri.store.size 
 
创建索引:
[zsz@zsz downloads]$ curl -XPUT 'localhost:9200/customer?pretty'

            {

              "acknowledged" : true

            }
 
[zsz@zsz downloads]$  curl 'localhost:9200/_cat/indices?v'

health status index    pri rep docs.count docs.deleted store.size pri.store.size

yellow open   customer   5   1          0            0       650b           650b 
 
增加索引并搜索:
[zsz@zsz downloads]$ curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '

>         {

>           "name": "John Doe"

>         }'

{

  "_index" : "customer",

  "_type" : "external",

  "_id" : "1",

  "_version" : 1,

  "_shards" : {

    "total" : 2,

    "successful" : 1,

    "failed" : 0

  },

  "created" : true

}
可以看到,一个新的文档在customer索引和external类型中被成功创建。文档也有一个内部id 1, 这个id是在增加索引的时候指定的。下面来检索这个记录:
[zsz@zsz downloads]$ curl -XGET 'localhost:9200/customer/external/1?pretty'

{

  "_index" : "customer",

  "_type" : "external",

  "_id" : "1",

  "_version" : 1,

  "found" : true,

  "_source" : {

    "name" : "John Doe"

  }

}
[zsz@zsz downloads]$ curl 'localhost:9200/customer/_search?q=John'

{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.19178301,"hits":[{"_index":"customer","_type":"external","_id":"1","_score":0.19178301,"_source":

        {

          "name": "John Doe"

        }}]}}
 
更新这个索引:
[zsz@zsz downloads]$ curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

>         {

>           "doc": { "name": "Jane Doe Haha" }

>         }'

{

  "_index" : "customer",

  "_type" : "external",

  "_id" : "1",

  "_version" : 2,

  "_shards" : {

    "total" : 2,

    "successful" : 1,

    "failed" : 0

  }

}
[zsz@zsz downloads]$ curl -XGET 'localhost:9200/customer/external/1?pretty'

{

  "_index" : "customer",

  "_type" : "external",

  "_id" : "1",

  "_version" : 2,

  "found" : true,

  "_source" : {

    "name" : "Jane Doe Haha"

  }

}
 
删除该索引:
[zsz@zsz downloads]$ curl -XDELETE 'localhost:9200/customer/external/1?pretty'

{

  "found" : true,

  "_index" : "customer",

  "_type" : "external",

  "_id" : "1",

  "_version" : 3,

  "_shards" : {

    "total" : 2,

    "successful" : 1,

    "failed" : 0

  }

}

[zsz@zsz downloads]$ curl -XGET 'localhost:9200/customer/external/1?pretty'

{

  "_index" : "customer",

  "_type" : "external",

  "_id" : "1",

  "found" : false

}
ES必要的插件

必要的Head、kibana、IK(中文分词)、graph等插件的详细安装和使用。
http://blog.csdn.net/column/details/deep-elasticsearch.html
 
ES对外接口

JAVA API接口

http://www.ibm.com/developerworks/library/j-use-elasticsearch-java-apps/index.html

RESTful API接口

常见的增、删、改、查操作实现:
http://blog.csdn.net/laoyang360/article/details/51931981
 
3、elasticsearch-jdbc的安装配置
需要的安装包:elasticsearch-jdbc-2.3.2.0-dist.zip,它是与elasticsearch-2.3.2.tar.gz相对应的,其他版本会出错。
 
[zsz@zsz downloads]$ vi /etc/profile
#增加elasticsearch-jdbc插件的环境变量
export JDBC_IMPORTER_HOME=/home/downloads/elasticsearch-jdbc-2.3.2.0
 
[zsz@zsz downloads]$ source /etc/profile
 
 创建同步:
[zsz@zsz downloads]$ mkdir /odbc_es
[zsz@zsz downloads]$ cd  /odbc_es
[zsz@zsz odbc_es]$ vi mysql_import_es.sh
#!/bin/sh
bin=$JDBC_IMPORTER_HOME/bin
lib=$JDBC_IMPORTER_HOME/lib
echo '{
"type" : "jdbc",
"jdbc": {
"elasticsearch.autodiscover":true,
"elasticsearch.cluster":"elasticsearch",##需要与/elasticsearch-2.3.2/config/elasticsearch.yml的配置对应
"url":"jdbc:mysql://***:3306/**",
"user":"**",
"password":"**",
"sql":"select * from news",
"elasticsearch" : {
  "host" : "127.0.0.1",
  "port" : 9300
},
"index" : "myindex",
"type" : "mytype"
}
}'| java \
  -cp "${lib}/*" \
  -Dlog4j.configurationFile=${bin}/log4j2.xml \
  org.xbib.tools.Runner \
  org.xbib.tools.JDBCImporter
 
##根据个人项目情况填写以上的地址
 
运行数据同步脚本mysql_import_es.sh:
[zsz@zsz odbc_es]$ ./mysql_import_es.sh
查看是否同步成功:
[zsz@zsz odbc_es]$ curl 'localhost:9200/_cat/indices?v'

health status index    pri rep docs.count docs.deleted store.size pri.store.size 
yellow open   myindex    5   1        163            0    146.5kb        146.5kb 

yellow open   customer   5   1          0            0       795b           795b 
[zsz@zsz odbc_es]$ curl -XGET 'http://127.0.0.1:9200/myindex/mytype/_search?pretty'

{

  "took" : 9,

  "timed_out" : false,

  "_shards" : {

    "total" : 5,

    "successful" : 5,

    "failed" : 0

  }
......
说明同步数据成功。
 
问题与解决:
1、提示no cluster nodes available, check settings 
解决:请查看/elasticsearch-2.3.2/config/elasticsearch.yml。一般都是该文件配置错误造成的,比如单机模式的,配置了node节点,或者cluster.name错误。
 
有问题请与我联系。
原文地址:http://www.cnblogs.com/zhongshengzhen/p/elasticsearch_mysql.html
 
 

Elasticsearch和mysql数据同步(elasticsearch-jdbc)的更多相关文章

  1. Mysql数据同步Elasticsearch方案总结

    Mysql数据同步Elasticsearch方案总结 https://my.oschina.net/u/4000872/blog/2252620

  2. Elasticsearch和mysql数据同步(logstash)

    1.版本介绍 Elasticsearch: https://www.elastic.co/products/elasticsearch 版本:2.4.0   Logstash: https://www ...

  3. 同步mysql数据到ElasticSearch的最佳实践

    Elasticsearch是一个实时的分布式搜索和分析引擎.它可以帮助你用前所未有的速度去处理大规模数据.ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全 ...

  4. Logstash学习之路(四)使用Logstash将mysql数据导入elasticsearch(单表同步、多表同步、全量同步、增量同步)

    一.使用Logstash将mysql数据导入elasticsearch 1.在mysql中准备数据: mysql> show tables; +----------------+ | Table ...

  5. Centos8 部署 ElasticSearch 集群并搭建 ELK,基于Logstash同步MySQL数据到ElasticSearch

    Centos8安装Docker 1.更新一下yum [root@VM-24-9-centos ~]# yum -y update 2.安装containerd.io # centos8默认使用podm ...

  6. 推荐一个同步Mysql数据到Elasticsearch的工具

    把Mysql的数据同步到Elasticsearch是个很常见的需求,但在Github里找到的同步工具用起来或多或少都有些别扭. 例如:某记录内容为"aaa|bbb|ccc",将其按 ...

  7. 几篇关于MySQL数据同步到Elasticsearch的文章---第一篇:Debezium实现Mysql到Elasticsearch高效实时同步

    文章转载自: https://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247484358&idx=1&sn=3a78347 ...

  8. 实战ELK(6)使用logstash同步mysql数据到ElasticSearch

    一.准备 1.mysql 我这里准备了个数据库mysqlEs,表User 结构如下 添加几条记录 2.创建elasticsearch索引 curl -XPUT 'localhost:9200/user ...

  9. mysql数据同步到Elasticsearch

    1.版本介绍 Elasticsearch: https://www.elastic.co/products/elasticsearch 版本:2.4.0   Logstash: https://www ...

随机推荐

  1. asp.net清除页面缓存防止同时登录

    //清除页面缓存,防止页面回退重复提交数据 在页面里做以下设置就可以使页面的缓存失效,每次都需要获取新页面. Response.Cache.SetCacheability(System.Web.Htt ...

  2. Brew 编译mod错误Error: L6265E: Non-RWPI Section libspace.o(.bss) cannot be assigned to PI Exec region ER_ZI

    Error: L6265E: Non-RWPI Section libspace.o(.bss) cannot be assigned to PI Exec region ER_ZI.: Error: ...

  3. None

    0 值的整型 / 浮点型.空字符串('').空列表([]). 空元组((,)).空字典({}).空集合(set())都等价于 False,但是不等于 None thing = None if thin ...

  4. 我的c语言经历

    作为一名计算机专业的学生,c语言是我的启蒙编程语言.当时,是刘慧老师带的课.很庆幸,是刘老师带的课.因为,后来当我这个人有了一些经历就会知道.对于像一张 白纸一样的大一新生.老师,如果能给学生很好的启 ...

  5. Java [leetcode 5] Longest Palindromic Substring

    问题描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  6. TCP/IP详解学习笔记(4)-ICMP协议,ping和Traceroute

    1.IMCP协议介绍 前面讲到了,IP协议并不是一个可靠的协议,它不保证数据被送达,那么,自然的,保证数据送达的工作应该由其他的模块来完成.其中一个重要的模块就是ICMP(网络控制报文)协议. 当传送 ...

  7. 无人机DLG生产作业流程

    参考文章 无人机(AVIAN)低空摄影测量作业流程 无人机低空遥感测绘作业流程及主要质量控制点 微型无人机低空摄影测量系 无人机航空摄影测量系统引进与发展 基于复杂地形的无人机航摄系统1∶500 DL ...

  8. 如何进行Monkey Test

    如何进行Monkey Test   目录 一 简介 二 测试准备 三 基本命令格式 四 测试Log获取 五 Monkey命令参数介绍 六 保存monkey log以及手机log到sdcard(新增) ...

  9. 36、Android Bitmap 全面解析

    Android Bitmap 全面解析(一)加载大尺寸图片 http://www.eoeandroid.com/thread-331669-1-1.html Android Bitmap 全面解析(二 ...

  10. hdu1792 水题

    最近转到vim上来了,用vim编写代码,用gcc编译,用gdb调试.这是用vim做的第一道题,纪念下.题目很水,就不说了. /* * Author : ben */ #include <cstd ...