您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“ElasticSearch如何使用”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“ElasticSearch如何使用”這篇文章吧。
1)安裝ES
下載ElasticSearch_版本號(hào).tar.gz,官網(wǎng)上有,下載好之后。
tar -zvxf elasticsearch-1.1.0.tar.gz cd elasticsearch-1.1.0
安裝一下插件,也可以不安裝,這個(gè)插件用來(lái)監(jiān)控用的
./bin/plugin -i elasticsearch/marvel/latest
想了解這個(gè)插件可以參考官方文檔
http://www.elasticsearch.org/guide/en/marvel/current/index.html
2)執(zhí)行程序
./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
如果想后臺(tái)運(yùn)行,則執(zhí)行
./elasticsearch -d
想確認(rèn)程序是否運(yùn)行,則運(yùn)行
lsof -i:9200 lsof -i:9300 一個(gè)是節(jié)點(diǎn)對(duì)外服務(wù)端口,一個(gè)是節(jié)點(diǎn)間交互端口(如果有集群的話)。
3)建立集群
配置文件路徑是:
.....(你的實(shí)際路徑)/config/elasticsearch.yml
默認(rèn)是全部配置項(xiàng)都屏蔽的,
我修改后配置項(xiàng)如下:
cluster.name: ctoes ---配置集群的名字 node.name: "QiangZiGeGe"---配置節(jié)點(diǎn)的名字,注意有雙引號(hào)
bootstrap.mlockall: true
沒有提到的配置項(xiàng)都采用默認(rèn)值,具體參數(shù)如何設(shè)置,還需要具體情況具體分析。
修改好后,啟動(dòng)es,可以看到打印的消息里有別的節(jié)點(diǎn)名字,就表示建立集群成功。
注意:es是自動(dòng)探測(cè)局域網(wǎng)內(nèi)的同名集群節(jié)點(diǎn)的。
查看集群的狀態(tài),可以通過:
curl 'http://localhost:9200/_cluster/health?pretty'
響應(yīng)如下:
{ "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 }
接下來(lái)來(lái)使用一下來(lái)得到直觀感受
4)使用數(shù)據(jù)庫(kù)感受一下
創(chuàng)建索引(相當(dāng)于創(chuàng)建數(shù)據(jù)庫(kù))
示例如下:
[deployer@XXXXXXX0013 ~]$ curl -XPUT 'http://localhost:9200/test1?pretty' -d' > { > "settings":{ > "number_of_shards":2, > "number_of_replicas":1 > } > } > ' { "acknowledged" : true }
注意,這里的number_of_shards參數(shù)是一次性設(shè)置,設(shè)置之后永遠(yuǎn)不可以再修改的,但是number_of_replicas是可以隨后可以修改的。
上面的url里的test1其實(shí)就是建立的索引(數(shù)據(jù)庫(kù))的名字,根據(jù)需要自己修改即可。
創(chuàng)建文檔
curl -XPUT 'http://localhost:9200/test1/table1/1' -d ' { "first":"dewmobile", "last":"technology", "age":3000, "about":"hello,world", "interest":["basketball","music"] } ' 響應(yīng)如下: {"_index":"test1","_type":"table1","_id":"1","_version":1,"created":true}
表明創(chuàng)建文檔成功
test1:建立的數(shù)據(jù)庫(kù)名字
table1:建立的type名字,type與關(guān)系數(shù)據(jù)庫(kù)的table對(duì)應(yīng)
1:自己制定的文檔的主鍵,也可以不指定主鍵由數(shù)據(jù)庫(kù)自己分配。
5)安裝數(shù)據(jù)庫(kù)同步插件
由于我們的數(shù)據(jù)源是放在MongoDB中的,所以這里只講MongoDB數(shù)據(jù)源的數(shù)據(jù)同步。
插件源碼:https://github.com/richardwilly98/elasticsearch-river-mongodb/
MongoDB River Plugin (作者 Richard Louapre) 簡(jiǎn)介:mongodb同步插件,mongodb必須搭成副本集的模式,因?yàn)檫@個(gè)插件的原理是通過定期讀取mongodb中的oplog來(lái)同步數(shù)據(jù)。
如何安裝使用呢?需要安裝2個(gè)插件
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插件
具體請(qǐng)參考:
https://github.com/jprante/elasticsearch-river-jdbc可以直接下載二進(jìn)制jar包。
https://github.com/jprante/elasticsearch-river-jdbc
4)安裝mysql驅(qū)動(dòng)jar包(必須?。?/p>
這樣,插件就裝好了。
6)使用插件告知ES添加監(jiān)聽數(shù)據(jù)庫(kù)任務(wù)
模板如下:
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" }}'
如果看到下面的內(nèi)容表示創(chuàng)建成功
{"_index":"_river","_type":"mongodb","_id":"_meta","_version":1,"created":true}
然后,數(shù)據(jù)就導(dǎo)入到了es中了,索引建立成功。
~~~~~~~~~~~~~~~~
如果是導(dǎo)入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" > } > } > '
更詳細(xì)的是:
{ "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 } }
對(duì)于schedule參數(shù):設(shè)置調(diào)度時(shí)刻的
格式參考: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(包含如何刪除任務(wù))
附錄:http://my.oschina.net/wenhaowu/blog/215219#OSC_h3_7
測(cè)試過程中,會(huì)出現(xiàn)錯(cuò)誤:
[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
至于這幾個(gè)參數(shù)是什么意思,還請(qǐng)讀者自己去弄明白。
參考:
http://stackoverflow.com/questions/20683440/elasticsearch-gives-error-about-queue-size
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-threadpool.html
~~~~~~~~~~~~~~~
關(guān)于客戶端,我們使用了Play框架,正如數(shù)據(jù)庫(kù)都需要驅(qū)動(dòng)包一樣,我們從官方網(wǎng)站上看到了這個(gè)
https://github.com/cleverage/play2-elasticsearch
關(guān)于中文分詞,可以嘗試使用Ansj.
~~~~~~~~~~~~~~~~~~~~~
關(guān)于創(chuàng)建索引:
curl -i -XPUT 'XXX:9200/fasth' -d '
{
"settings" :
{
"number_of_shards" : 3 ,
"number_of_replicas" : 1
}
}
'
~~~~~~~~~~~
創(chuàng)建映射
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"
}
}
}
'
全量任務(wù):
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"
}
}
'
以上是“ElasticSearch如何使用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。