The best elasticsearch highlevel java rest api-----bboss
快速內建Elasticsearch Restful API案例分享,本案例中代碼相容spring boot 1.x,2.x,相容Elasticserch 1.x,2.x,5.x,6.x,以及後續版本。
本文中講述的方法同樣适用于其他xxx boot類型項目內建bboss es。
1.導入elasticsearch rest booter子產品
maven工程
spring boot maven工程的pom.xml檔案中導入以下maven坐标
<dependency>
<groupId>com.bbossgroups.plugins</groupId>
<artifactId>bboss-elasticsearch-rest-jdbc</artifactId>
<version>5.0.8.2</version>
</dependency>
gradle工程
spring boot gradle工程的build.gradle檔案中導入以下gradle坐标
compile "com.bbossgroups.plugins:bboss-elasticsearch-rest-jdbc:5.0.8.2"
2.配置elasticsearch位址
預設情況下,如果就是本機的elasticsearch伺服器,導入bboss後不需要做任何配置就可以通過bboss rest api通路和操作elasticsearch。
極簡單配置,修改spring boot項目的application.properties檔案,隻需要加入以下内容即可:
elasticsearch.rest.hostNames=10.21.20.168:9200
## 叢集位址用逗号分隔
#elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282
如果需要更多的配置,可以将以下内容複制到spring boot項目的application.properties檔案中:
#x-pack認證賬号和密碼
elasticUser=elastic
elasticPassword=changeme
#es伺服器位址配置
elasticsearch.rest.hostNames=127.0.0.1:9200
#elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282
#動态索引表名稱日期格式配置
elasticsearch.dateFormat=yyyy.MM.dd
elasticsearch.timeZone=Asia/Shanghai
elasticsearch.ttl=2d
#在控制台輸出腳本調試開關showTemplate,false關閉,true打開,同時log4j至少是info級别
elasticsearch.showTemplate=true
#用戶端動态發現es叢集節點控制開關
elasticsearch.discoverHost=true
#http連結池配置
http.timeoutConnection = 400000
http.timeoutSocket = 400000
http.connectionRequestTimeout=400000
http.retryTime = 1
http.maxLineLength = -1
http.maxHeaderCount = 200
http.maxTotal = 400
http.defaultMaxPerRoute = 200
這些配置的含義,可以參考文檔:《
高性能elasticsearch ORM開發庫使用介紹》章節2進行了解。
其他各種boot架構配置的時候,也可自行建立application.properties配置檔案,在其中配置需要的參數。
3.驗證內建是否成功
完成前面兩步工作後,就可以通過以下代碼驗證內建是否成功,如果正确列印elasticssearch叢集狀态,那說明內建成功:
//建立es用戶端工具,驗證環境
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
//驗證環境,擷取es狀态
String response = clientUtil.executeHttp("_cluster/state?pretty",ClientInterface.HTTP_GET);
System.out.println(response);
3.完整的demo執行個體工程
https://github.com/bbossgroups/eshelloword-booter https://gitee.com/bbossgroups/eshelloword-booter4.參考文檔
https://my.oschina.net/bboss/blog/1556866