首页 > 编程 > Java > 正文

javaAPI连接Elasticsearch

2019-11-06 08:14:06
字体:
来源:转载
供稿:网友

代码

public class ESTransportClient implements FactoryBean, InitializingBean, DisposableBean {

PRivate static final Logger logger = LoggerFactory.getLogger(ESTransportClient.class);private String clusterNodes = "127.0.0.1:9300: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 = ",";@Overridepublic 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); }}@Overridepublic TransportClient getObject() throws Exception { return client;}@Overridepublic Class<TransportClient> getObjectType() { return TransportClient.class;}@Overridepublic boolean isSingleton() { return false;}@Overridepublic 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); // 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("127.0.0.1:9300"), Integer.valueOf(9300))); // } 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;}

}

@Autowired private ESTransportClient client;

SearchRequestBuilder ss = client.getObject().prepareSearch("index1","index2").setSearchType(SearchType.QUERY_THEN_FETCH);

注:之前直接写链接,也没做连接池,也没用spring依赖注入,结果项目运行一阵就内存溢出了。改为spring管理后好了。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表