首页 > 代码库 > ElasticSearch使用
ElasticSearch使用
安装之前,请参考https://github.com/richardwilly98/elasticsearch-river-mongodb根据你的MongoDB版本号决定需要的elasticsearch版本号和插件号。
1)安装ES
下载ElasticSearch_版本号.tar.gz,官网上有,下载好之后。
tar -zvxf elasticsearch-1.1.0.tar.gz
cd elasticsearch-1.1.0
安装一下插件,也可以不安装,这个插件用来监控用的
./bin/plugin -i elasticsearch/marvel/latest
想了解这个插件可以参考官方文档
http://www.elasticsearch.org/guide/en/marvel/current/index.html
2)执行程序
./elasticsearch
看到以下的就表示成功了
[2014-04-09 10:12:41,414][INFO ][node ] [Lorna Dane] version[1.1.0], pid[839], build[2181e11/2014-03-25T15:59:51Z]
[2014-04-09 10:12:41,415][INFO ][node ] [Lorna Dane] initializing ...
[2014-04-09 10:12:41,431][INFO ][plugins ] [Lorna Dane] loaded [], sites []
[2014-04-09 10:12:44,383][INFO ][node ] [Lorna Dane] initialized
[2014-04-09 10:12:44,384][INFO ][node ] [Lorna Dane] starting ...
[2014-04-09 10:12:44,495][INFO ][transport ] [Lorna Dane] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/XXXXXX:9300]}
[2014-04-09 10:12:47,522][INFO ][cluster.service ] [Lorna Dane] new_master [Lorna Dane][Ml-gTu_ZTniHR2mkpbMQ_A][XXXXX][inet[/XXXXXX:9300]], reason: zen-disco-join (elected_as_master)
[2014-04-09 10:12:47,545][INFO ][discovery ] [Lorna Dane] elasticsearch/Ml-gTu_ZTniHR2mkpbMQ_A
[2014-04-09 10:12:47,572][INFO ][http ] [Lorna Dane] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/XXXXX:9200]}
[2014-04-09 10:12:47,607][INFO ][gateway ] [Lorna Dane] recovered [0] indices into cluster_state
[2014-04-09 10:12:47,607][INFO ][node ] [Lorna Dane] started
如果想后台运行,则执行
./elasticsearch -d
想确认程序是否运行,则运行
lsof -i:9200
lsof -i:9300
一个是节点对外服务端口,一个是节点间交互端口(如果有集群的话)。
3)建立集群
配置文件路径是:
.....(你的实际路径)/config/elasticsearch.yml
默认是全部配置项都屏蔽的,
我修改后配置项如下:
cluster.name: ctoes ---配置集群的名字
node.name: "QiangZiGeGe"---配置节点的名字,注意有双引号
bootstrap.mlockall: true
没有提到的配置项都采用默认值,具体参数如何设置,还需要具体情况具体分析。
修改好后,启动es,可以看到打印的消息里有别的节点名字,就表示建立集群成功。
注意:es是自动探测局域网内的同名集群节点的。
查看集群的状态,可以通过:
curl ‘http://localhost:9200/_cluster/health?pretty‘
响应如下:
{
"cluster_name" : "ctoes",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 2,
"number_of_data_nodes" : 2,
"active_primary_shards" : 5,
"active_shards" : 10,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0
}
接下来来使用一下来得到直观感受
4)使用数据库感受一下
创建索引(相当于创建数据库)
示例如下:
[deployer@XXXXXXX0013 ~]$ curl -XPUT ‘http://localhost:9200/test1?pretty‘ -d‘
> {
> "settings":{
> "number_of_shards":2,
> "number_of_replicas":1
> }
> }
> ‘
{
"acknowledged" : true
}
注意,这里的number_of_shards参数是一次性设置,设置之后永远不可以再修改的,但是number_of_replicas是可以随后可以修改的。
上面的url里的test1其实就是建立的索引(数据库)的名字,根据需要自己修改即可。
创建文档
curl -XPUT ‘http://localhost:9200/test1/table1/1‘ -d ‘
{ "first":"dewmobile",
"last":"technology",
"age":3000,
"about":"hello,world",
"interest":["basketball","music"]
}
‘
响应如下:
{"_index":"test1","_type":"table1","_id":"1","_version":1,"created":true}
表明创建文档成功
test1:建立的数据库名字
table1:建立的type名字,type与关系数据库的table对应
1:自己制定的文档的主键,也可以不指定主键由数据库自己分配。
5)安装数据库同步插件
由于我们的数据源是放在MongoDB中的,所以这里只讲MongoDB数据源的数据同步。
插件源码:https://github.com/richardwilly98/elasticsearch-river-mongodb/
MongoDB River Plugin (作者 Richard Louapre)
简介:mongodb同步插件,mongodb必须搭成副本集的模式,因为这个插件的原理是通过定期读取mongodb中的oplog来同步数据。
如何安装使用呢?需要安装2个插件
1)插件1
./plugin -install elasticsearch/elasticsearch-mapper-attachments/2.0.0
2)插件2
./bin/plugin --install com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.0
安装过程如下:
./bin/plugin --install com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.0
-> Installing com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.0...
Trying http://download.elasticsearch.org/com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/elasticsearch-river-mongodb-2.0.0.zip...
Trying http://search.maven.org/remotecontent?filepath=com/github/richardwilly98/elasticsearch/elasticsearch-river-mongodb/2.0.0/elasticsearch-river-mongodb-2.0.0.zip...
Trying https://oss.sonatype.org/service/local/repositories/releases/content/com/github/richardwilly98/elasticsearch/elasticsearch-river-mongodb/2.0.0/elasticsearch-river-mongodb-2.0.0.zip...
Downloading .............................................................................................DONE
Installed com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.0 into /usr/local/elasticsearch_1.1.0/elasticsearch/elasticsearch-1.1.0/plugins/river-mongodb
3)安装elasticsearch-MySql插件
具体请参考:
https://github.com/jprante/elasticsearch-river-jdbc可以直接下载二进制jar包。
https://github.com/jprante/elasticsearch-river-jdbc
4)安装mysql驱动jar包(必须!)
这样,插件就装好了。
6)使用插件告知ES添加监听数据库任务
模板如下:
curl -XPUT localhost:9200/_river/mongo_resource/_meta -d ‘
{
"type":"mongodb",
"mongodb":{
"servers":
[{"host":"10.XX.XX.XX","port":"60004"}
],
"db":"zapya_api",
"collection":"resources"
},
"index":{
"name":"mongotest",
"type":"resources"
}}‘
如果看到下面的内容表示创建成功
{"_index":"_river","_type":"mongodb","_id":"_meta","_version":1,"created":true}
然后,数据就导入到了es中了,索引建立成功。
~~~~~~~~~~~~~~~~
如果是导入mysql,模板如下:
[deployer@XXX0014 ~]$ curl -XPUT ‘localhost:9200/_river/my_jdbc_river/_meta‘ -d ‘{
> "type":"jdbc",
> "jdbc":{
> "url":"jdbc:mysql://localhost:3306/fastooth",
> "user":"XXX",
> "password":"XXX",
> "sql":"select *,base62Decode(display_name) as name from users"
> }
> }
> ‘
更详细的是:
{
"jdbc" :{
"strategy" : "simple",
"url" : null,
"user" : null,
"password" : null,
"sql" : null,
"schedule" : null,
"poolsize" : 1,
"rounding" : null,
"scale" : 2,
"autocommit" : false,
"fetchsize" : 10, /* Integer.MIN for MySQL */
"max_rows" : 0,
"max_retries" : 3,
"max_retries_wait" : "30s",
"locale" : Locale.getDefault().toLanguageTag(),
"index" : "jdbc",
"type" : "jdbc",
"bulk_size" : 100,
"max_bulk_requests" : 30,
"bulk_flush_interval" : "5s",
"index_settings" : null,
"type_mapping" : null
}
}
对于schedule参数:设置调度时刻的
格式参考:http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger
http://elasticsearch-users.115913.n3.nabble.com/Ann-JDBC-River-Plugin-for-ElasticSearch-td4019418.html
http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger
https://github.com/jprante/elasticsearch-river-jdbc/issues/186
官方文档:
http://elasticsearch-users.115913.n3.nabble.com/Ann-JDBC-River-Plugin-for-ElasticSearch-td4019418.html
https://github.com/jprante/elasticsearch-river-jdbc/wiki/JDBC-River-parameters
https://github.com/jprante/elasticsearch-river-jdbc/wiki/Quickstart(包含如何删除任务)
附录:http://my.oschina.net/wenhaowu/blog/215219#OSC_h2_7
测试过程中,会出现错误:
[7]: index [yyyy], type [rrrr], id [1964986], message [RemoteTransportException[[2sdfsdf][inet[/xxxxxxxxxx:9300]][bulk/shard]]; nested: EsRejectedExecutionException[rejected execution (queue capacity 50) on org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1@3e82ee89]; ]
修改配置文件,在最后增加:
threadpool:
bulk:
type: fixed
size: 60
queue_size: 1000
至于这几个参数是什么意思,还请读者自己去弄明白。
参考:
http://stackoverflow.com/questions/20683440/elasticsearch-gives-error-about-queue-size
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-threadpool.html
~~~~~~~~~~~~~~~
关于客户端,我们使用了Play框架,正如数据库都需要驱动包一样,我们从官方网站上看到了这个
https://github.com/cleverage/play2-elasticsearch
关于中文分词,可以尝试使用Ansj.
~~~~~~~~~~~~~~~~~~~~~
关于创建索引:
curl -i -XPUT ‘XXX:9200/fasth‘ -d ‘
{
"settings" :
{
"number_of_shards" : 3 ,
"number_of_replicas" : 1
}
}
‘
~~~~~~~~~~~
创建映射
curl -i -XPUT ‘http://localhost:9200/fa/users/_mapping‘ -d ‘
{
"properties":
{
"_id":
{
"type":"string",
"index":"not_analyzed"
},
"name":
{
"type":"string"
},
"gender":
{
"type":"string",
"index":"not_analyzed"
},
"primary_avatar":
{
"type":"string",
"index":"not_analyzed"
},
"signature":
{
"type":"string",
"index":"not_analyzed"
}
}
}
‘
全量任务:
curl -XPUT ‘xxx:9200/_river/mysql_users/_meta‘ -d ‘{
"type":"jdbc",
"jdbc":
{
"url":"jdbc:mysql://XXX:3306/fastooth",
"user":"XXX",
"password":"XXX",
"sql":"select distinct _id,base62Decode(display_name) as name,gender,primary_avatar,signature from users",
"index":"XXX",
"type":"XXX"
}
}
‘
http://www.nosqldb.cn/1368777378160.html
ElasticSearch使用