Elasticsearch安装及自动同步mysql数据库数据,elasticsearchmysql
1 环境:
CentOS 6.4 x64
JDK1.8.0_45
2 Elasticsearch的安装
https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.5.2.tar.gz
或者在浏览器中输入http://<ip>:9200出现下图
则表示elasticsearch安装配置并且启动成功
3 Elasticsearch插件的安装
4 在mysql中创建数据库、表并添加测试数据
create table person
(idint ,
namevarchar(20),
sex char(1),
createdate timestamp);
insert into person(id,name,sex) values(1,'zhangsan','F');
insert into person(id,name,sex) values(2,'zhaoliu','F');
5 创建elasticsearch索引并设置与mysql数据更新同步
curl -XPUT 'http://192.168.120.159:9200/test/person/_mapping' -d '
{
"person": {
"properties": {
"id": {
"type": "long",
"store": "yes"
},
"name": {
"type": "string",
"store": "yes"
},
"sex": {
"type": "string",
"store": "yes"
}
}
}
}'
curl -XPUT 'http://192.168.120.159:9200/_river/my_jdbc_river/_meta' -d '{
"type": "jdbc",
"jdbc": {
"driver": "com.mysql.jdbc.Driver",
"url": "jdbc:mysql://<ip>/test",
"user": "<username>",
"password": "<password>",
"sql": "select id as _id,name,sex from person",
"index": "test",
"type": "person",
"bulk_size": 100,
"max_bulk_requests": 30,
"bulk_timeout": "10s",
"flush_interval": "5s",
"schedule": "0/5 * * * * ?"
}
}'
这样就建立了一个任务,定时每5秒从mysql中获取一次数据并将数据更新到elasticsearch中
如果想后台运行,则执行 ./elasticsearch -d
注:示例写的比较简单每次是过去表中所有的数据更新到elasticsearch中,这里可以进行增量式获取即每次只获取新增的或者修改过的数据,只需要在sql后面加一个时间就可以了,具体的可以参照elasticsearch官方说明文档。
本文档参照
http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-plugins.html
https://github.com/jprante/elasticsearch-jdbc