In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
小编给大家分享一下Spring 4+ElasticSearch如何集成,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
一: ElasticSearch 基本概念
INDEX:这是ES存储数据的地方,类似于关系数据库的DATABASE。
Document TYPE:嗯,类似关系数据库的表,主要功能是将完全不同SCHEMA(这个概念以后会讲到,不急)的数据分开,一个INDEX里面可以有若干个Document TYPE。
Document:好吧,这个类似关系数据库的一行,在同一个Document TYPE下面,每一Document都有一个唯一的ID作为区分;
Filed:类似关系数据库的某一列,这是ES数据存储的最小单位。
Cluster和Node:ES可以以单点或者集群方式运行,以一个整体对外提供search服务的所有节点组成cluster,组成这个cluster的各个节点叫做node。
shard:通常叫分片,这是ES提供分布式搜索的基础,其含义为将一个完整的INDEX分成若干部分存储在相同或不同的节点上,这些组成INDEX的部分就叫做shard。
Replica:和REPLICATION通常指的都是一回事,即INDEX的冗余备份,可以用于防止数据丢失,或者用来做负载分担。
二: ElasticSearch TransportClient和NodeClient
如果你使用Java,ElasticSearch 提供Transport CLIENT和Node CLIENT两种连接方式。transport CLIENT充当ES集群和你的应用直接的通信层,它知道API,并且能够在节点间自动轮循。
Node CLIENT,事实上是集群中的一个节点(但是不存储数据,并且不能作为主节点),由于它是一个节点,它知道整个集群的状态(全部节点都在哪,哪些分片在哪些节点上等等),这意味着它执行API时可以少用一个网络跳跃。
但需要完整的Node节点参数配置。
两种CLIENT的使用场景:
1.如果你想让你的应用和集群解耦,transport CLIENT是一个理想的选择。例如,如果你的集群快速创建和销毁连接,那么transport CLIENT比node CLIENT轻很多,因为它不是集群的一部分。
同样,如果你需要创建上千个连接,但是你不希望有上千个node CLIENT加入你的集群,transport CLIENT将是一个更好的选择。
2.在另一方面,如果你只需要几个长连接的,能持久的连接到集群,node CLIENT会更高效一点,因为它知道集群的结构,但是要注意防火墙影响相关通信的问题。
三:与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 settimgMap = new HashMap();
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
文档类
点击(此处)折叠或打开
@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;
}
}
服务类
点击(此处)折叠或打开
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;
/**
* 创建索引
*/
public void createGmapLogsIndex() {
System.out.println(elasticTemplate.createIndex(Logs.class));
}
/**
* 批量添加文档
*
* @param logs
*/
public void createGmapLogs(List logs) {
List 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 = new ArrayList();
logs.add(log);
createGmapLogs(logs);
}
}
以上是"Spring 4+ElasticSearch如何集成"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.