您好,登錄后才能下訂單哦!
小編給大家分享一下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 集成
@Configuration
@PropertySource("classpath:context-datasource.properties")
public class ElasticSearchElConfig {
@Value("${el.cluster.name}")
private String elClusterName;
@Value("${el.cluster.node1.url}")
private String elClusterNode1Url;
@Value("${el.cluster.node1.port}")
private Integer elClusterNode2Port;
@Bean(name = "elasticSearchClient")
public Client elasticSearchClient() {
Map<String, Object> settimgMap = new HashMap<String, Object>();
settimgMap.put("cluster.name", elClusterName);
Settings settings = Settings.settingsBuilder().put(settimgMap).build();
TransportClient client = TransportClient.builder().settings(settings).build()
.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(elClusterNode1Url, elClusterNode2Port)));
return client;
}
@Bean(name = "elasticTemplate")
public ElasticsearchTemplate elasticTemplate(Client elasticSearchClient) {
return new ElasticsearchTemplate(elasticSearchClient);
}
@Autowired
private Environment environment;
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
return new PropertySourcesPlaceholderConfigurer();
}
}
四:Demo
文檔類
點(diǎn)擊(此處)折疊或打開
@Document(indexName="gmap_system_log_index",type="gmap_system_log")
public class Logs extends ValueObject{
/**
*
*/
private static final long serialVersionUID = 1L;
@Field(type=FieldType.Integer,index=FieldIndex.not_analyzed,store=true)
private Integer userId;
@Field(type=FieldType.String,index=FieldIndex.not_analyzed,store=true)
private String system;
@Field(type=FieldType.String,index=FieldIndex.not_analyzed,store=true)
private String url;
@Field(type=FieldType.String,index=FieldIndex.analyzed,store=true)
private String content;
/**
* @return the userId
*/
public Integer getUserId() {
return userId;
}
/**
* @param userId the userId to set
*/
public void setUserId(Integer userId) {
this.userId = userId;
}
/**
* @return the system
*/
public String getSystem() {
return system;
}
/**
* @param system the system to set
*/
public void setSystem(String system) {
this.system = system;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}
}
服務(wù)類
點(diǎn)擊(此處)折疊或打開
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.stereotype.Component;
/**
* TODO:
*
* @author gengchong
* @date 2017年4月28日 下午4:24:01
*/
@Component("elasticSearchDemo")
public class ElasticSearchDemo {
@Autowired
private ElasticsearchTemplate elasticTemplate;
/**
* 創(chuàng)建索引
*/
public void createGmapLogsIndex() {
System.out.println(elasticTemplate.createIndex(Logs.class));
}
/**
* 批量添加文檔
*
* @param logs
*/
public void createGmapLogs(List<Logs> logs) {
List<IndexQuery> queries = new ArrayList<>();
for (Logs log : logs) {
IndexQuery indexQuery = new IndexQueryBuilder().withObject(log).build();
queries.add(indexQuery);
}
elasticTemplate.bulkIndex(queries);
}
/**
* 添加文檔
*
* @param log
*/
public void createGmapLog(Logs log) {
List<Logs> logs = new ArrayList<>();
logs.add(log);
createGmapLogs(logs);
}
}
以上是“Spring 4+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)容。