国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > 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管理后好了。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肇州县| 澳门| 抚州市| 庄浪县| 九龙城区| 特克斯县| 韶山市| 泾阳县| 乐清市| 高雄市| 深泽县| 鄂伦春自治旗| 内丘县| 马尔康县| 阜城县| 攀枝花市| 普兰店市| 棋牌| 荆门市| 宝应县| 靖远县| 济南市| 台东县| 宁国市| 府谷县| 年辖:市辖区| 祁阳县| 合山市| 灵武市| 张北县| 岐山县| 田林县| 成武县| 陆丰市| 阿城市| 基隆市| 阿合奇县| 北安市| 余干县| 太和县| 菏泽市|