spring使用elasticsearch 5.x
2016-12-20 11:45
281 查看
elasticsearch客户端选择
这里使用transport建立elasticsearch客户端
applicationContext.xml配置,属性可以采用读取属性文件的方式。参考类PropertyPlaceholderConfigurer。
transport客户端类
参考:https://github.com/spring-projects/spring-data-elasticsearch
这里使用transport建立elasticsearch客户端
applicationContext.xml配置,属性可以采用读取属性文件的方式。参考类PropertyPlaceholderConfigurer。
<!-- elasticsearch配置 --> <bean id="esTransportClient" class="org.elk.ESTransportClient"> <property name="clusterNodes" value="127.0.0.1:9300" /> <property name="clusterName" value="elasticsearch" /> </bean>
transport客户端类
public class ESTransportClient implements FactoryBean<TransportClient>, InitializingBean, DisposableBean { private static final Logger logger = LoggerFactory.getLogger(ESTransportClient.class); private String clusterNodes = "127.0.0.1:9300"; private String clusterName = "elasticsearch"; private Boolean clientTransportSniff = true; private Boolean clientIgnoreClusterName = Boolean.FALSE; private String clientPingTimeout = "5s"; private String clientNodesSamplerInterval = "5s"; private TransportClient client; private Properties properties; static final String COLON = ":"; static final String COMMA = ","; @Override public void destroy() throws Exception { try { logger.info("Closing elasticSearch client"); if (client != null) { client.close(); } } catch (final Exception e) { logger.error("Error closing ElasticSearch client: ", e); } } @Override public TransportClient getObject() throws Exception { return client; } @Override public Class<TransportClient> getObjectType() { return TransportClient.class; } @Override public boolean isSingleton() { return false; } @Override public void afterPropertiesSet() throws Exception { buildClient(); } protected void buildClient() throws Exception { client = new PreBuiltTransportClient(settings()); Assert.hasText(clusterNodes, "[Assertion failed] clusterNodes settings missing."); for (String clusterNode : split(clusterNodes, COMMA)) { String hostName = substringBeforeLast(clusterNode, COLON); String port = substringAfterLast(clusterNode, COLON); Assert.hasText(hostName, "[Assertion failed] missing host name in 'clusterNodes'"); Assert.hasText(port, "[Assertion failed] missing port in 'clusterNodes'"); logger.info("adding transport node : " + clusterNode); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port))); } client.connectedNodes(); } private Settings settings() { if (properties != null) { return Settings.builder().put(properties).build(); } return Settings.builder() .put("cluster.name", clusterName) .put("client.transport.sniff", clientTransportSniff) .put("client.transport.ignore_cluster_name", clientIgnoreClusterName) .put("client.transport.ping_timeout", clientPingTimeout) .put("client.transport.nodes_sampler_interval", clientNodesSamplerInterval) .build(); } public void setClusterNodes(String clusterNodes) { this.clusterNodes = clusterNodes; } public void setClusterName(String clusterName) { this.clusterName = clusterName; } public void setClientTransportSniff(Boolean clientTransportSniff) { this.clientTransportSniff = clientTransportSniff; } public String getClientNodesSamplerInterval() { return clientNodesSamplerInterval; } public void setClientNodesSamplerInterval(String clientNodesSamplerInterval) { this.clientNodesSamplerInterval = clientNodesSamplerInterval; } public String getClientPingTimeout() { return clientPingTimeout; } public void setClientPingTimeout(String clientPingTimeout) { this.clientPingTimeout = clientPingTimeout; } public Boolean getClientIgnoreClusterName() { return clientIgnoreClusterName; } public void setClientIgnoreClusterName(Boolean clientIgnoreClusterName) { this.clientIgnoreClusterName = clientIgnoreClusterName; } public void setProperties(Properties properties) { this.properties = properties; } }
参考:https://github.com/spring-projects/spring-data-elasticsearch
相关文章推荐
- elasticsearch结合spring springmvc jest 使用做成web架构
- spring中使用Elasticsearch的代码实现
- Elasticsearch 5.x 字段折叠的使用
- 大数据学习[17]--Elasticsearch 5.x 字段折叠的使用[转]
- Elasticsearch 07 使用SpringBoot集成Elasticsearch
- 使用 Spring、Elasticsearch 及 Logstash 构建企业级数据搜索和分析平台
- 005-spring-data-elasticsearch 3.0.0.0使用【三】-spring-data之Spring数据扩展
- Spring集成第三方类库(以简单集成elasticsearch 5.x版本客户端为例)
- spring boot 整合 elasticsearch 5.x
- 003-spring-data-elasticsearch 3.0.0.0使用【一】-spring-data之概述、核心概念、查询方法、定义Repository接口
- spring-data-elasticsearch 初级使用
- 5 在springboot中使用ElasticsearchTemplate 操作ElasticSearch(本机和远程服务器)
- 详解spring中使用Elasticsearch的代码实现
- spring-data-elasticsearch使用笔记
- spring boot项目,Elasticsearch的一种使用方式
- 使用 Spring、Elasticsearch 及 Logstash 构建企业级数据搜索和分析平台
- spring 操作elasticsearch查询使用方法
- spring boot 集成 elasticsearch 5.x
- elasticsearch结合spring springmvc jest 使用做成WEB架构
- 004-spring-data-elasticsearch 3.0.0.0使用【二】-spring-data之定义方法、创建repository实例、从聚合根发布事件