Elasticsearch搜索引擎学习记录2-数据同步

来源:互联网 发布:java 数组 属于 那个类 编辑:程序博客网 时间:2024/06/06 01:27

es服务同mysql的数据同步

我用的是river同步数据,ps:river代表es的一个数据源,也是其它存储方式(如:数据库)同步数据到es的一个方法。它是以插件方式存在的一个es服务,通过读取river中的数据并把它索引到es中,官方的river有couchDB的,RabbitMQ的,Twitter的,Wikipedia的。样例针对mysql的river。
1. 配置
确认是否已安装elasticsearch-river-jdbc的jar包,由于的我的es为1.6版本,所以安装的1.4.0.8.jar包;
将mysql-connector-java-5.1.31.jar包放入es安装目录的lib包下
2. 执行脚本
- 新建索引
curl -XPUT ‘http://localhost:9200/my-clothes’
- 创建数据表与索引映射

 curl -XPUT 'http://localhost:9200/myclothes/clothes_user/_mapping' -d '                {                    "clothes_user": {                        "properties": {                            "id": {                                "type": "int",                                "store": "yes"                            },                            "username": {                                "type": "string",                                "store": "yes"                            },                            "password": {                                "type": "string",                                "store": "yes"                            },                            "real_name": {                                "type": "string",                                "store": "yes"                            }                        }                    }                }'
  • 同步数据脚本
 curl -XPUT 'http://localhost:9200/_river/clothes/_meta' -d '{    "type": "jdbc",    "jdbc": {        "driver": "com.mysql.jdbc.Driver",       "url": "jdbc:mysql://localhost:3306/myclothes",        "user": "root",        "password": "111111",       "sql": [            {                "statement": "SELECT r.id AS 'id',r.id AS '_id',r.username,r.password,r.real_name FROM  clothes_user r"            }        ],        "index": "my-clothes",        "type": "clothes",        "bulk_size": 100,        "max_bulk_requests": 30,        "bulk_timeout": "10s",        "flush_interval": "5s",        "interval":"10"    }}'
  • 删除指定的river
    curl -XDELETE ‘localhost:9200/_river/clothes’

由于脚本里设置的interval为10秒同步一次,重启es服务以后,我们可以测验一下同步结果:
数据库中数据这里写图片描述
10秒以后,观察es服务
这里写图片描述
修改数据库数据,超管1为超管1111111,再观察一下同步后的数据
这里写图片描述

0 0
原创粉丝点击