protected void buildClient() throws Exception { Assert.hasText(hosts, "[Assertion Failed] At least one host must be set."); ArrayList<HttpHost> httpHosts = new ArrayList<HttpHost>(); for (String host : hosts.split(COMMA)) { URL hostUrl = new URL(host); httpHosts.add(new HttpHost(hostUrl.getHost(), hostUrl.getPort(), hostUrl.getProtocol())); } client = new RestHighLevelClient(RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()]))); }
@Override public RestHighLevelClient createClient(Map<String, String> clientConfig) throws IOException { RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()])); restClientFactory.configureRestClientBuilder(builder); RestHighLevelClient rhlClient = new RestHighLevelClient(builder); if (LOG.isInfoEnabled()) { LOG.info("Pinging Elasticsearch cluster via hosts {} ...", httpHosts); } if (!rhlClient.ping()) { throw new RuntimeException("There are no reachable Elasticsearch nodes!"); } if (LOG.isInfoEnabled()) { LOG.info("Created Elasticsearch RestHighLevelClient connected to {}", httpHosts.toString()); } return rhlClient; }
@Inject public ElasticSearchRestDAOV5(RestClient lowLevelRestClient, ElasticSearchConfiguration config, ObjectMapper objectMapper) { this.objectMapper = objectMapper; this.elasticSearchAdminClient = lowLevelRestClient; this.elasticSearchClient = new RestHighLevelClient(lowLevelRestClient); this.indexName = config.getIndexName(); this.logIndexPrefix = config.getTasklogIndexName(); this.clusterHealthColor = config.getClusterHealthColor(); // Set up a workerpool for performing async operations. int corePoolSize = 6; int maximumPoolSize = 12; long keepAliveTime = 1L; this.executorService = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MINUTES, new LinkedBlockingQueue<>()); }
}); RestHighLevelClient client = new RestHighLevelClient(builder); return () -> client;
this.lowLevelClient = buildRestClient(this.hostAddresses, threadCount); client = new RestHighLevelClient(this.lowLevelClient);
this.highLevelClient = new RestHighLevelClient(client);
@BeforeClass public static void startServer() throws Exception { System.setProperty(ElasticSearchConfiguration.EMBEDDED_PORT_PROPERTY_NAME, "9204"); System.setProperty(ElasticSearchConfiguration.ELASTIC_SEARCH_URL_PROPERTY_NAME, "http://localhost:9204"); configuration = new SystemPropertiesElasticSearchConfiguration(); String host = configuration.getEmbeddedHost(); int port = configuration.getEmbeddedPort(); String clusterName = configuration.getEmbeddedClusterName(); embeddedElasticSearch = new EmbeddedElasticSearchV5(clusterName, host, port); embeddedElasticSearch.start(); ElasticSearchRestClientProvider restClientProvider = new ElasticSearchRestClientProvider(configuration); restClient = restClientProvider.get(); elasticSearchClient = new RestHighLevelClient(restClient); Map<String, String> params = new HashMap<>(); params.put("wait_for_status", "yellow"); params.put("timeout", "30s"); restClient.performRequest("GET", "/_cluster/health", params); objectMapper = new ObjectMapper(); indexDAO = new ElasticSearchRestDAOV5(restClient, configuration, objectMapper); }
@Override public RestHighLevelClient instance() { if (this.client != null) return client; try { SystemConfig systemConfigHost = systemConfigService.selectByKey("elasticsearch_host"); String host = systemConfigHost.getValue(); SystemConfig systemConfigPort = systemConfigService.selectByKey("elasticsearch_port"); String port = systemConfigPort.getValue(); SystemConfig systemConfigName = systemConfigService.selectByKey("elasticsearch_index"); name = systemConfigName.getValue(); if (StringUtils.isEmpty(host) || StringUtils.isEmpty(port)) return null; client = new RestHighLevelClient( RestClient.builder( new HttpHost(host, Integer.parseInt(port), "http"))); // 判断索引是否存在,不存在创建 if (!this.existIndex()) this.createIndex("topic", topicMappingBuilder); return client; } catch (NumberFormatException e) { log.error(e.getMessage()); return null; } }
RestHighLevelClientImpl(Vertx vertx, RestClientBuilder restClientBuilder) { this.vertx = vertx; this.restClientBuilder = restClientBuilder; this.delegate = new org.elasticsearch.client.RestHighLevelClient(restClientBuilder); }
try { this.client = new RestHighLevelClient(lowLevelClient); checkVersion(); logger.info("Elasticsearch Client for version {}.x connected to a node running version {}", compatibleVersion(), getVersion());
/** * Creates an Elasticsearch client from settings provided via the global config. * * @return new client */ public static ElasticsearchClient create(Map<String, Object> globalConfig) { ElasticsearchClientConfig esClientConfig = new ElasticsearchClientConfig( getEsSettings(globalConfig)); HttpHost[] httpHosts = getHttpHosts(globalConfig, esClientConfig.getConnectionScheme()); RestClientBuilder builder = RestClient.builder(httpHosts); builder.setRequestConfigCallback(reqConfigBuilder -> { // Modifies request config builder with connection and socket timeouts. // https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.6/_timeouts.html reqConfigBuilder.setConnectTimeout(esClientConfig.getConnectTimeoutMillis()); reqConfigBuilder.setSocketTimeout(esClientConfig.getSocketTimeoutMillis()); return reqConfigBuilder; }); builder.setMaxRetryTimeoutMillis(esClientConfig.getMaxRetryTimeoutMillis()); builder.setHttpClientConfigCallback(clientBuilder -> { clientBuilder.setDefaultIOReactorConfig(getIOReactorConfig(esClientConfig)); clientBuilder.setDefaultCredentialsProvider(getCredentialsProvider(esClientConfig)); clientBuilder.setSSLContext(getSSLContext(esClientConfig)); return clientBuilder; }); RestClient lowLevelClient = builder.build(); RestHighLevelClient client = new RestHighLevelClient(lowLevelClient); return new ElasticsearchClient(lowLevelClient, client); }
@Override public void start() throws IOException { if (client != null) { // The client has already been initialized. Let's skip this again return; } try { // Create an elasticsearch client client = new RestHighLevelClient(buildRestClient(settings.getElasticsearch())); checkVersion(); logger.info("Elasticsearch Client for version {}.x connected to a node running version {}", compatibleVersion(), getVersion()); } catch (Exception e) { logger.warn("failed to create elasticsearch client, disabling crawler..."); throw e; } if (settings.getElasticsearch().getPipeline() != null) { // Check that the pipeline exists if (!isExistingPipeline(settings.getElasticsearch().getPipeline())) { throw new RuntimeException("You defined pipeline:" + settings.getElasticsearch().getPipeline() + ", but it does not exist."); } } BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer = (request, bulkListener) -> client.bulkAsync(request, RequestOptions.DEFAULT, bulkListener); bulkProcessor = BulkProcessor.builder(bulkConsumer, new DebugListener(logger)) .setBulkActions(settings.getElasticsearch().getBulkSize()) .setFlushInterval(TimeValue.timeValueMillis(settings.getElasticsearch().getFlushInterval().millis())) .setBulkSize(new ByteSizeValue(settings.getElasticsearch().getByteSize().getBytes())) .build(); }
@Override public void start() throws IOException { if (client != null) { // The client has already been initialized. Let's skip this again return; } try { // Create an elasticsearch client client = new RestHighLevelClient(buildRestClient(settings.getElasticsearch())); checkVersion(); logger.info("Elasticsearch Client for version {}.x connected to a node running version {}", compatibleVersion(), getVersion()); } catch (Exception e) { logger.warn("failed to create elasticsearch client, disabling crawler..."); throw e; } if (settings.getElasticsearch().getPipeline() != null) { // Check that the pipeline exists if (!isExistingPipeline(settings.getElasticsearch().getPipeline())) { throw new RuntimeException("You defined pipeline:" + settings.getElasticsearch().getPipeline() + ", but it does not exist."); } } BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer = (request, bulkListener) -> client.bulkAsync(request, RequestOptions.DEFAULT, bulkListener); bulkProcessor = BulkProcessor.builder(bulkConsumer, new DebugListener(logger)) .setBulkActions(settings.getElasticsearch().getBulkSize()) .setFlushInterval(TimeValue.timeValueMillis(settings.getElasticsearch().getFlushInterval().millis())) .setBulkSize(new ByteSizeValue(settings.getElasticsearch().getByteSize().getBytes())) .build(); }
public RestHighLevelClient getRestHighLevelClient() { if (restHighLevelClient == null) { restHighLevelClient = new RestHighLevelClient(RestClient.builder(httpHost)); } return restHighLevelClient; }
public static RestHighLevelClient getClient(String address) { List<HttpHost> httpHostList = new ArrayList<>(); String[] addr = address.split(","); for(String add : addr) { String[] pair = add.split(":"); TelnetUtil.telnet(pair[0], Integer.valueOf(pair[1])); httpHostList.add(new HttpHost(pair[0], Integer.valueOf(pair[1]), "http")); } RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(httpHostList.toArray(new HttpHost[httpHostList.size()]))); return client; }
@Produces public RestHighLevelClient create() { return new RestHighLevelClient(RestClient.builder(elasticSearch.getHosts())); }
protected ESClient createLocalRestClient(ElasticSearchEmbeddedServerConfig serverConfig) { if (!serverConfig.httpEnabled()) { throw new IllegalArgumentException( "Embedded configuration has no HTTP port enable, use TransportClient instead of Rest"); } RestClientBuilder lowLevelRestClientBuilder = RestClient.builder( new HttpHost("localhost", Integer.parseInt(serverConfig.getHttpPort()))); RestHighLevelClient client = new RestHighLevelClient(lowLevelRestClientBuilder); // NOSONAR (factory) // checkConnection(client); return new ESRestClient(client.getLowLevelClient(), client); }
/** * Instantiates a new elastic search data publisher. */ public ElasticSearchDataPublisher() { restClient = RestClient.builder(new HttpHost(ESUtils.getESHost(), ESUtils.getESPort())).build(); client = new RestHighLevelClient(restClient); }
@Profile({"production", "docker"}) @Bean(destroyMethod = "close") public RestHighLevelClient getRestClient() { return new RestHighLevelClient(RestClient.builder(new HttpHost(props.getClients().getHostname(), props.getClients().getHttpPort(), props.getClients().getScheme()))); }
private RestHighLevelClient createClient() { final HttpHost httpHost = urlToHttpHost(configuration.url); // use single thread for rest client final RestClientBuilder builder = RestClient.builder(httpHost) .setHttpClientConfigCallback( httpClientBuilder -> httpClientBuilder.setDefaultIOReactorConfig( IOReactorConfig.custom().setIoThreadCount(1).build())); return new RestHighLevelClient(builder); }