溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring 4+ElasticSearch如何集成

發(fā)布時(shí)間:2021-12-16 11:49:08 來(lái)源:億速云 閱讀:229 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下Spring 4+ElasticSearch如何集成,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

一: ElasticSearch 基本概念

INDEX:這是ES存儲(chǔ)數(shù)據(jù)的地方,類似于關(guān)系數(shù)據(jù)庫(kù)的DATABASE。


Document TYPE:嗯,類似關(guān)系數(shù)據(jù)庫(kù)的表,主要功能是將完全不同SCHEMA(這個(gè)概念以后會(huì)講到,不急)的數(shù)據(jù)分開,一個(gè)INDEX里面可以有若干個(gè)Document TYPE。


Document:好吧,這個(gè)類似關(guān)系數(shù)據(jù)庫(kù)的一行,在同一個(gè)Document TYPE下面,每一Document都有一個(gè)唯一的ID作為區(qū)分;


Filed:類似關(guān)系數(shù)據(jù)庫(kù)的某一列,這是ES數(shù)據(jù)存儲(chǔ)的最小單位。


Cluster和Node:ES可以以單點(diǎn)或者集群方式運(yùn)行,以一個(gè)整體對(duì)外提供search服務(wù)的所有節(jié)點(diǎn)組成cluster,組成這個(gè)cluster的各個(gè)節(jié)點(diǎn)叫做node。


shard:通常叫分片,這是ES提供分布式搜索的基礎(chǔ),其含義為將一個(gè)完整的INDEX分成若干部分存儲(chǔ)在相同或不同的節(jié)點(diǎn)上,這些組成INDEX的部分就叫做shard。


Replica:和REPLICATION通常指的都是一回事,即INDEX的冗余備份,可以用于防止數(shù)據(jù)丟失,或者用來(lái)做負(fù)載分擔(dān)。


二: ElasticSearch TransportClient和NodeClient

如果你使用Java,ElasticSearch 提供Transport CLIENT和Node CLIENT兩種連接方式。transport CLIENT充當(dāng)ES集群和你的應(yīng)用直接的通信層,它知道API,并且能夠在節(jié)點(diǎn)間自動(dòng)輪循。


Node CLIENT,事實(shí)上是集群中的一個(gè)節(jié)點(diǎn)(但是不存儲(chǔ)數(shù)據(jù),并且不能作為主節(jié)點(diǎn)),由于它是一個(gè)節(jié)點(diǎn),它知道整個(gè)集群的狀態(tài)(全部節(jié)點(diǎn)都在哪,哪些分片在哪些節(jié)點(diǎn)上等等),這意味著它執(zhí)行API時(shí)可以少用一個(gè)網(wǎng)絡(luò)跳躍。


但需要完整的Node節(jié)點(diǎn)參數(shù)配置。


兩種CLIENT的使用場(chǎng)景:


1.如果你想讓你的應(yīng)用和集群解耦,transport CLIENT是一個(gè)理想的選擇。例如,如果你的集群快速創(chuàng)建和銷毀連接,那么transport CLIENT比node CLIENT輕很多,因?yàn)樗皇羌旱囊徊糠帧?br/>

同樣,如果你需要?jiǎng)?chuàng)建上千個(gè)連接,但是你不希望有上千個(gè)node CLIENT加入你的集群,transport CLIENT將是一個(gè)更好的選擇。


2.在另一方面,如果你只需要幾個(gè)長(zhǎng)連接的,能持久的連接到集群,node CLIENT會(huì)更高效一點(diǎn),因?yàn)樗兰旱慕Y(jié)構(gòu),但是要注意防火墻影響相關(guān)通信的問題。


三:與Spring 集成


  1. @Configuration

  2. @PropertySource("classpath:context-datasource.properties")

  3. public class ElasticSearchElConfig {

  4.     @Value("${el.cluster.name}")

  5.     private String elClusterName;

  6.     @Value("${el.cluster.node1.url}")

  7.     private String elClusterNode1Url;

  8.     @Value("${el.cluster.node1.port}")

  9.     private Integer elClusterNode2Port;


  10.     @Bean(name = "elasticSearchClient")

  11.     public Client elasticSearchClient() {

  12.         Map<String, Object> settimgMap = new HashMap<String, Object>();

  13.         settimgMap.put("cluster.name", elClusterName);


  14.         Settings settings = Settings.settingsBuilder().put(settimgMap).build();


  15.         TransportClient client = TransportClient.builder().settings(settings).build()

  16.                 .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(elClusterNode1Url, elClusterNode2Port)));

  17.         return client;


  18.     }


  19.     @Bean(name = "elasticTemplate")

  20.     public ElasticsearchTemplate elasticTemplate(Client elasticSearchClient) {

  21.         return new ElasticsearchTemplate(elasticSearchClient);

  22.     }


  23.     @Autowired

  24.     private Environment environment;


  25.     @Bean

  26.     public static PropertySourcesPlaceholderConfigurer propertyConfigure() {

  27.         return new PropertySourcesPlaceholderConfigurer();

  28.     }

  29. }


四:Demo

文檔類


點(diǎn)擊(此處)折疊或打開

  1. @Document(indexName="gmap_system_log_index",type="gmap_system_log")

  2. public class Logs extends ValueObject{


  3.     /**

  4.      *

  5.      */

  6.     private static final long serialVersionUID = 1L;

  7.     

  8.     @Field(type=FieldType.Integer,index=FieldIndex.not_analyzed,store=true)

  9.     private Integer userId;

  10.     @Field(type=FieldType.String,index=FieldIndex.not_analyzed,store=true)

  11.     private String system;

  12.     @Field(type=FieldType.String,index=FieldIndex.not_analyzed,store=true)

  13.     private String url;

  14.     @Field(type=FieldType.String,index=FieldIndex.analyzed,store=true)

  15.     private String content;

  16.     /**

  17.      * @return the userId

  18.      */

  19.     public Integer getUserId() {

  20.         return userId;

  21.     }

  22.     /**

  23.      * @param userId the userId to set

  24.      */

  25.     public void setUserId(Integer userId) {

  26.         this.userId = userId;

  27.     }

  28.     /**

  29.      * @return the system

  30.      */

  31.     public String getSystem() {

  32.         return system;

  33.     }

  34.     /**

  35.      * @param system the system to set

  36.      */

  37.     public void setSystem(String system) {

  38.         this.system = system;

  39.     }

  40.     /**

  41.      * @return the url

  42.      */

  43.     public String getUrl() {

  44.         return url;

  45.     }

  46.     /**

  47.      * @param url the url to set

  48.      */

  49.     public void setUrl(String url) {

  50.         this.url = url;

  51.     }

  52.     /**

  53.      * @return the content

  54.      */

  55.     public String getContent() {

  56.         return content;

  57.     }

  58.     /**

  59.      * @param content the content to set

  60.      */

  61.     public void setContent(String content) {

  62.         this.content = content;

  63.     }

  64.     

  65.     

  66. }


服務(wù)類


點(diǎn)擊(此處)折疊或打開

  1. import java.util.ArrayList;

  2. import java.util.List;


  3. import org.springframework.beans.factory.annotation.Autowired;

  4. import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

  5. import org.springframework.data.elasticsearch.core.query.IndexQuery;

  6. import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;

  7. import org.springframework.stereotype.Component;


  8. /**

  9.  * TODO:

  10.  *

  11.  * @author gengchong

  12.  * @date 2017年4月28日 下午4:24:01

  13.  */

  14. @Component("elasticSearchDemo")

  15. public class ElasticSearchDemo {


  16.     @Autowired

  17.     private ElasticsearchTemplate elasticTemplate;


  18.     /**

  19.      * 創(chuàng)建索引

  20.      */

  21.     public void createGmapLogsIndex() {

  22.         System.out.println(elasticTemplate.createIndex(Logs.class));

  23.     }


  24.     /**

  25.      * 批量添加文檔

  26.      *

  27.      * @param logs

  28.      */

  29.     public void createGmapLogs(List<Logs> logs) {

  30.         List<IndexQuery> queries = new ArrayList<>();

  31.         for (Logs log : logs) {

  32.             IndexQuery indexQuery = new IndexQueryBuilder().withObject(log).build();

  33.             queries.add(indexQuery);

  34.         }


  35.         elasticTemplate.bulkIndex(queries);

  36.     }


  37.     /**

  38.      * 添加文檔

  39.      *

  40.      * @param log

  41.      */

  42.     public void createGmapLog(Logs log) {

  43.         List<Logs> logs = new ArrayList<>();

  44.         logs.add(log);

  45.         createGmapLogs(logs);

  46.     }


  47. }

以上是“Spring 4+ElasticSearch如何集成”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(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)容。

AI