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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值