org.elasticsearch.client.RestClientBuilder Java Examples
The following examples show how to use
org.elasticsearch.client.RestClientBuilder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JfDemoESPlugin.java From jframe with Apache License 2.0 | 8 votes |
private void startRestClient() { try { HttpHost[] hosts = new HttpHost[] { // new HttpHost("10.132.161.173", 30002, "http") new HttpHost("127.0.0.1", 9200, "http") }; client = RestClient.builder(hosts).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder.setConnectTimeout(2000).setSocketTimeout(10000); } }).setMaxRetryTimeoutMillis(10000).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setMaxConnPerRoute(100).setMaxConnTotal(200); // return httpClientBuilder // .setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build()); } }).build(); } catch (Exception e) { LOG.error(e.getMessage(), e.fillInStackTrace()); } }
Example #2
Source File: ElasticsearchRestClientInstrumentationIT.java From apm-agent-java with Apache License 2.0 | 8 votes |
@BeforeClass public static void startElasticsearchContainerAndClient() throws IOException { container = new ElasticsearchContainer(ELASTICSEARCH_CONTAINER_VERSION); container.start(); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER_NAME, PASSWORD)); RestClientBuilder builder = RestClient.builder(HttpHost.create(container.getHttpHostAddress())) .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); client = new RestHighLevelClient(builder); client.indices().create(new CreateIndexRequest(INDEX), RequestOptions.DEFAULT); reporter.reset(); }
Example #3
Source File: LegacyElasticsearchConfig.java From adaptive-alerting with Apache License 2.0 | 7 votes |
private RestClientBuilder buildRestClientBuilder() { return RestClient.builder( HttpHost.create( elasticSearchProperties.getUrls())) .setRequestConfigCallback(req -> { req.setConnectionRequestTimeout(elasticSearchProperties.getConfig().getConnectionTimeout()); req.setConnectTimeout(elasticSearchProperties.getConfig().getConnectionTimeout()); req.setSocketTimeout(elasticSearchProperties.getConfig().getConnectionTimeout()); return req; }) .setMaxRetryTimeoutMillis(elasticSearchProperties.getConfig().getConnectionRetryTimeout()) .setHttpClientConfigCallback(req -> { req.setMaxConnTotal(elasticSearchProperties.getConfig().getMaxTotalConnections()); req.setMaxConnPerRoute(500); return req; }); }
Example #4
Source File: ElasticsearchConfig.java From adaptive-alerting with Apache License 2.0 | 7 votes |
private RestClientBuilder buildRestClientBuilder() { return RestClient .builder(HttpHost.create(elasticSearchProperties.getUrls())) .setRequestConfigCallback(req -> { req.setConnectionRequestTimeout(elasticSearchProperties.getConfig().getConnectionTimeout()); req.setConnectTimeout(elasticSearchProperties.getConfig().getConnectionTimeout()); req.setSocketTimeout(elasticSearchProperties.getConfig().getConnectionTimeout()); return req; }) .setMaxRetryTimeoutMillis(elasticSearchProperties.getConfig().getConnectionRetryTimeout()) .setHttpClientConfigCallback(req -> { req.setMaxConnTotal(elasticSearchProperties.getConfig().getMaxTotalConnections()); //TODO Move hardcoded value to config req.setMaxConnPerRoute(500); return req; }); }
Example #5
Source File: EsRestClientContainer.java From frostmourne with MIT License | 6 votes |
public void init() { RestClientBuilder restClientBuilder = RestClient.builder(parseHttpHost(esHosts) .toArray(new HttpHost[0])); if (this.settings != null && this.settings.size() > 0 && !Strings.isNullOrEmpty(this.settings.get("username")) && !Strings.isNullOrEmpty(this.settings.get("password"))) { String userName = this.settings.get("username"); String password = this.settings.get("password"); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); restClientBuilder.setHttpClientConfigCallback(httpAsyncClientBuilder -> { httpAsyncClientBuilder.disableAuthCaching(); return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); }); } restHighLevelClient = new RestHighLevelClient(restClientBuilder); this.restLowLevelClient = restHighLevelClient.getLowLevelClient(); if (sniff) { sniffer = Sniffer.builder(restLowLevelClient).setSniffIntervalMillis(5 * 60 * 1000).build(); } }
Example #6
Source File: ElasticsearchConnection.java From components with Apache License 2.0 | 6 votes |
public static RestClient createClient(ElasticsearchDatastoreProperties datastore) throws MalformedURLException { String urlStr = datastore.nodes.getValue(); String[] urls = urlStr.split(","); HttpHost[] hosts = new HttpHost[urls.length]; int i = 0; for (String address : urls) { URL url = new URL("http://" + address); hosts[i] = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); i++; } RestClientBuilder restClientBuilder = RestClient.builder(hosts); if (datastore.auth.useAuth.getValue()) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(datastore.auth.userId.getValue(), datastore.auth.password.getValue())); restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }); } return restClientBuilder.build(); }
Example #7
Source File: AbstractBeyonderTest.java From elasticsearch-beyonder with Apache License 2.0 | 6 votes |
private static void startRestClient() throws IOException { if (client == null) { RestClientBuilder builder = RestClient.builder(HttpHost.create(testCluster)); if (testClusterUser != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(testClusterUser, testClusterPass)); builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder .setDefaultCredentialsProvider(credentialsProvider)); } client = builder.build(); testClusterRunning(); } }
Example #8
Source File: RestAutoConfigure.java From microservices-platform with Apache License 2.0 | 6 votes |
/** * 异步httpclient连接数配置 */ private void setHttpClientConfig(RestClientBuilder builder, RestClientPoolProperties poolProperties, RestClientProperties restProperties){ builder.setHttpClientConfigCallback(httpClientBuilder -> { httpClientBuilder.setMaxConnTotal(poolProperties.getMaxConnectNum()) .setMaxConnPerRoute(poolProperties.getMaxConnectPerRoute()); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); map.from(restProperties::getUsername).to(username -> { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, restProperties.getPassword())); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); }); return httpClientBuilder; }); }
Example #9
Source File: Elasticsearch6ApiCallBridge.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
@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; }
Example #10
Source File: ElasticDatastoreFactoryTest.java From elasticgeo with GNU General Public License v3.0 | 6 votes |
@Before public void setUp() throws IOException { dataStoreFactory = Mockito.spy(new ElasticDataStoreFactory()); clientBuilder = mock(RestClientBuilder.class); hostsCaptor = ArgumentCaptor.forClass(HttpHost[].class); Mockito.doReturn(clientBuilder).when(dataStoreFactory).createClientBuilder(hostsCaptor.capture()); final RestClient restClient = mock(RestClient.class); when(clientBuilder.build()).thenReturn(restClient); final DataStore dataStore = mock(DataStore.class); Mockito.doReturn(dataStore).when(dataStoreFactory).createDataStore(any(RestClient.class), any(), anyMap()); configCallbackCaptor = ArgumentCaptor.forClass(RestClientBuilder.HttpClientConfigCallback.class); when(clientBuilder.setHttpClientConfigCallback(configCallbackCaptor.capture())).thenReturn(clientBuilder); httpClientBuilder = mock(HttpAsyncClientBuilder.class); credentialsProviderCaptor = ArgumentCaptor.forClass(CredentialsProvider.class); when(httpClientBuilder.setDefaultCredentialsProvider(credentialsProviderCaptor.capture())).thenReturn(httpClientBuilder); threadFactoryCaptor = ArgumentCaptor.forClass(ThreadFactory.class); when(httpClientBuilder.setThreadFactory(threadFactoryCaptor.capture())).thenReturn(httpClientBuilder); requestConfigCallbackCaptor = ArgumentCaptor.forClass(RestClientBuilder.RequestConfigCallback.class); when(clientBuilder.setRequestConfigCallback(requestConfigCallbackCaptor.capture())).thenReturn(clientBuilder); requestConfigBuilder = mock(RequestConfig.Builder.class); when(requestConfigBuilder.setAuthenticationEnabled(true)).thenReturn(requestConfigBuilder); params = getParams("localhost", 9200, "admin", "proxy"); ElasticDataStoreFactory.httpThreads.set(1); }
Example #11
Source File: TestImportData.java From jframe with Apache License 2.0 | 6 votes |
@Before public void init() { client = RestClient.builder(new HttpHost("127.0.0.1", 30002, "http"), new HttpHost("127.0.0.1", 30002, "http")) .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder.setConnectTimeout(5000).setSocketTimeout(30000); } }).setMaxRetryTimeoutMillis(30000) // .setHttpClientConfigCallback(new // RestClientBuilder.HttpClientConfigCallback() { // @Override // public HttpAsyncClientBuilder // customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) // { // return httpClientBuilder // .setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build()); // } // }) .build(); }
Example #12
Source File: ElasticsearchClientRest.java From c2mon with GNU Lesser General Public License v3.0 | 6 votes |
@Override public void setup() { RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(properties.getHost(), properties.getPort(), properties.getScheme())); if (StringUtils.isNotEmpty(properties.getUsername()) && StringUtils.isNotEmpty(properties.getPassword())) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(properties.getUsername(), properties.getPassword()); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, credentials); restClientBuilder.setHttpClientConfigCallback( httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); } else { if (StringUtils.isNotEmpty(properties.getUsername()) || StringUtils.isNotEmpty(properties.getPassword())) { log.warn("Both username and password must be configured to setup ES authentication."); } } client = new RestHighLevelClient(restClientBuilder); }
Example #13
Source File: ElasticsearchRestClientInstrumentationIT.java From apm-agent-java with Apache License 2.0 | 6 votes |
@BeforeClass public static void startElasticsearchContainerAndClient() throws IOException { // Start the container container = new ElasticsearchContainer(ELASTICSEARCH_CONTAINER_VERSION); container.start(); // Create the client CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER_NAME, PASSWORD)); RestClientBuilder builder = RestClient.builder(HttpHost.create(container.getHttpHostAddress())) .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); client = new RestHighLevelClient(builder); client.indices().create(new CreateIndexRequest(INDEX), RequestOptions.DEFAULT); reporter.reset(); }
Example #14
Source File: ElasticsearchRestClientInstrumentationIT.java From apm-agent-java with Apache License 2.0 | 6 votes |
@BeforeClass public static void startElasticsearchContainerAndClient() throws IOException { // Start the container container = new ElasticsearchContainer(ELASTICSEARCH_CONTAINER_VERSION); container.start(); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER_NAME, PASSWORD)); RestClientBuilder builder = RestClient.builder(HttpHost.create(container.getHttpHostAddress())) .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); lowLevelClient = builder.build(); client = new RestHighLevelClient(lowLevelClient); lowLevelClient.performRequest("PUT", "/" + INDEX); reporter.reset(); }
Example #15
Source File: ElasticSearchFilter.java From timbuctoo with GNU General Public License v3.0 | 6 votes |
@JsonCreator public ElasticSearchFilter(@JsonProperty("hostname") String hostname, @JsonProperty("port") int port, @JsonProperty("username") Optional<String> username, @JsonProperty("password") Optional<String> password) { Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"), new BasicHeader("Role", "Read")}; final RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(hostname, port)) .setDefaultHeaders(headers); if (username.isPresent() && !username.get().isEmpty() && password.isPresent() && !password.get().isEmpty()) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(username.get(), password.get()) ); restClientBuilder.setHttpClientConfigCallback(b -> b.setDefaultCredentialsProvider(credentialsProvider)); } restClient = restClientBuilder.build(); mapper = new ObjectMapper(); }
Example #16
Source File: XPackBaseDemo.java From elasticsearch-full with Apache License 2.0 | 6 votes |
@Before public void setUp() throws Exception { /** * 如果es集群安装了x-pack插件则以此种方式连接集群 * 1. java客户端的方式是以tcp协议在9300端口上进行通信 * 2. http客户端的方式是以http协议在9200端口上进行通信 */ Settings settings = Settings.builder(). put("xpack.security.user", "elastic:changeme").build(); client = new PreBuiltXPackTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9200)); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme")); restClient = RestClient.builder(new HttpHost("localhost",9200,"http")) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }).build(); }
Example #17
Source File: ElasticsearchAutoConfiguration.java From spring-boot-demo with MIT License | 6 votes |
@Bean @ConditionalOnMissingBean public RestHighLevelClient restHighLevelClient() { List<String> clusterNodes = elasticsearchProperties.getClusterNodes(); clusterNodes.forEach(node -> { try { String[] parts = StringUtils.split(node, ":"); Assert.notNull(parts, "Must defined"); Assert.state(parts.length == 2, "Must be defined as 'host:port'"); httpHosts.add(new HttpHost(parts[0], Integer.parseInt(parts[1]), elasticsearchProperties.getSchema())); } catch (Exception e) { throw new IllegalStateException("Invalid ES nodes " + "property '" + node + "'", e); } }); RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[0])); return getRestHighLevelClient(builder, elasticsearchProperties); }
Example #18
Source File: ElasticsearchAuditLogSink.java From Groza with Apache License 2.0 | 6 votes |
@PostConstruct public void init() { try { log.trace("Adding elastic rest endpoint... host [{}], port [{}], scheme name [{}]", host, port, schemeName); RestClientBuilder builder = RestClient.builder( new HttpHost(host, port, schemeName)); if (StringUtils.isNotEmpty(userName) && StringUtils.isNotEmpty(password)) { log.trace("...using username [{}] and password ***", userName); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); } this.restClient = builder.build(); } catch (Exception e) { log.error("Sink init failed!", e); throw new RuntimeException(e.getMessage(), e); } }
Example #19
Source File: ElasticSearchSinkTester.java From pulsar with Apache License 2.0 | 5 votes |
@Override public void prepareSink() throws Exception { RestClientBuilder builder = RestClient.builder( new HttpHost( "localhost", serviceContainer.getMappedPort(9200), "http")); elasticClient = new RestHighLevelClient(builder); }
Example #20
Source File: ESUtils.java From yuzhouwan with Apache License 2.0 | 5 votes |
public static RestClientBuilder getESHosts() { if (isBlank(ES_HOSTS)) throw new RuntimeException("Cannot get elasticSearch hosts from config! " + "Please check es.hosts config option."); String[] hosts = ES_HOSTS.split(","); int len; if ((len = hosts.length) <= 0) throw new RuntimeException("Cannot get elasticSearch hosts from config! " + "Please check es.hosts config option."); String host; String[] hostAndPort; LinkedList<HttpHost> httpHosts = new LinkedList<>(); for (int i = 0; i < len; i++) { host = hosts[i]; hostAndPort = host.split(":"); if (hostAndPort.length != 2) { LOG.warn("Invalid es host: {}!", host); continue; } httpHosts.add(new HttpHost(hostAndPort[0], Integer.parseInt(hostAndPort[1]), "http")); } int size; HttpHost[] httpHostsArray = new HttpHost[size = httpHosts.size()]; for (int i = 0; i < size; i++) { httpHostsArray[i] = httpHosts.get(i); } return RestClient.builder(httpHostsArray); }
Example #21
Source File: TestElasticSearchRestDAOV6.java From conductor with Apache License 2.0 | 5 votes |
@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"); System.setProperty(ElasticSearchConfiguration.ELASTIC_SEARCH_INDEX_BATCH_SIZE_PROPERTY_NAME, "1"); configuration = new SystemPropertiesElasticSearchConfiguration(); String host = configuration.getEmbeddedHost(); int port = configuration.getEmbeddedPort(); String clusterName = configuration.getEmbeddedClusterName(); embeddedElasticSearch = new EmbeddedElasticSearchV6(clusterName, host, port); embeddedElasticSearch.start(); ElasticSearchRestClientBuilderProvider restClientProvider = new ElasticSearchRestClientBuilderProvider(configuration); RestClientBuilder restClientBuilder = restClientProvider.get(); restClient = restClientBuilder.build(); Map<String, String> params = new HashMap<>(); params.put("wait_for_status", "yellow"); params.put("timeout", "30s"); restClient.performRequest("GET", "/_cluster/health", params); objectMapper = new JsonMapperProvider().get(); indexDAO = new ElasticSearchRestDAOV6(restClientBuilder, configuration, objectMapper); }
Example #22
Source File: ElasticVindClient.java From vind with Apache License 2.0 | 5 votes |
private ElasticVindClient(int port, String scheme, String host, String user, String key) { this.port = port; this.host = host; this.scheme = scheme; this.user = user; this.key = key; final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, key)); final RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host, port, scheme)) .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); this.client = new RestHighLevelClient(restClientBuilder); }
Example #23
Source File: ElasticVindClient.java From vind with Apache License 2.0 | 5 votes |
private ElasticVindClient(String defaultIndex, int port, String scheme, String host, String user, String key) { this.defaultIndex = defaultIndex; this.port = port; this.host = host; this.scheme = scheme; this.user = user; this.key = key; final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); if(Objects.nonNull(this.user) && Objects.nonNull(this.key)) { credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, key)); } this.client = new RestHighLevelClient( RestClient.builder( new HttpHost(host, port, scheme) ).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }) ); }
Example #24
Source File: ElasticSearchHelper.java From testgrid with Apache License 2.0 | 5 votes |
public void open() { String esEndpoint = ConfigurationContext.getProperty(ConfigurationContext.ConfigurationProperties.ES_ENDPOINT_URL); try { String urlString = new URL(esEndpoint).getHost(); RestClientBuilder builder = RestClient.builder(new HttpHost(urlString, 80, "http")); esClient = new RestHighLevelClient(builder); logger.info("Initialized Elastic Stack Helper"); } catch (MalformedURLException e) { logger.error("Could not initialize ElasticSearch Helper cannot talk to Elastic Search"); } }
Example #25
Source File: MetricStoreImplTest.java From griffin with Apache License 2.0 | 5 votes |
@Before public void setup(){ PowerMockito.mockStatic(RestClient.class); restClientMock = PowerMockito.mock(RestClient.class); RestClientBuilder restClientBuilderMock = PowerMockito.mock(RestClientBuilder.class); given(RestClient.builder(anyVararg())).willReturn(restClientBuilderMock); given(restClientBuilderMock.build()).willReturn(restClientMock); }
Example #26
Source File: MetricStoreImpl.java From griffin with Apache License 2.0 | 5 votes |
public MetricStoreImpl(@Value("${elasticsearch.host}") String host, @Value("${elasticsearch.port}") int port, @Value("${elasticsearch.scheme:http}") String scheme, @Value("${elasticsearch.user:}") String user, @Value("${elasticsearch.password:}") String password) { HttpHost httpHost = new HttpHost(host, port, scheme); RestClientBuilder builder = RestClient.builder(httpHost); if (!user.isEmpty() && !password.isEmpty()) { String encodedAuth = buildBasicAuthString(user, password); Header[] requestHeaders = new Header[]{ new BasicHeader(org.apache.http.HttpHeaders.AUTHORIZATION, encodedAuth)}; builder.setDefaultHeaders(requestHeaders); } this.client = builder.build(); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON); this.responseHeaders = responseHeaders; String urlBase = String.format("/%s/%s", INDEX, TYPE); this.urlGet = urlBase.concat("/_search?filter_path=hits.hits._source"); this.urlPost = urlBase.concat("/_bulk"); this.urlDelete = urlBase.concat("/_delete_by_query"); this.indexMetaData = String.format( "{ \"index\" : { \"_index\" : " + "\"%s\",\"_type\" : \"%s\" } }%n", INDEX, TYPE); this.mapper = new ObjectMapper(); }
Example #27
Source File: ElasticsearchMigration.java From elasticsearch-migration with Apache License 2.0 | 5 votes |
private RestHighLevelClient createElasticsearchClient(ElasticsearchConfig elasticsearchConfig) { final RestClientBuilder builder = RestClient.builder( elasticsearchConfig.getUrls().stream().map(e -> new HttpHost(e.getHost(), e.getPort(), e.getProtocol())).collect(Collectors.toSet()).toArray(new HttpHost[0]) ); builder.setDefaultHeaders(elasticsearchConfig.getHeaders().entries().stream().map(e -> new BasicHeader(e.getKey(), e.getValue())).collect(Collectors.toList()).toArray(new Header[0])); if (elasticsearchConfig.getMaxRetryTimeoutMillis() != null) { builder.setMaxRetryTimeoutMillis(elasticsearchConfig.getMaxRetryTimeoutMillis()); } if (elasticsearchConfig.getPathPrefix() != null) { builder.setPathPrefix(elasticsearchConfig.getPathPrefix()); } return new RestHighLevelClient(builder); }
Example #28
Source File: ElasticSearchClientFactory.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public PooledObject<RestHighLevelClient> makeObject() throws Exception { HttpHost[] nodes = new HttpHost[nodesReference.get().size()]; List<HttpHost> nodeList = new ArrayList<HttpHost>(); for(HostAndPort each: nodesReference.get()){ nodeList.add(new HttpHost(each.getHost(),each.getPort(),each.getSchema())); } nodes = nodeList.toArray(nodes); RestClientBuilder clientBuilder = RestClient.builder(nodes); RestHighLevelClient client = new RestHighLevelClient(clientBuilder); return new DefaultPooledObject(client); }
Example #29
Source File: Elasticsearch7ApiCallBridge.java From flink with Apache License 2.0 | 5 votes |
@Override public RestHighLevelClient createClient(Map<String, String> clientConfig) { RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()])); restClientFactory.configureRestClientBuilder(builder); RestHighLevelClient rhlClient = new RestHighLevelClient(builder); return rhlClient; }
Example #30
Source File: ElasticSearchRestTargetRepository.java From ache with Apache License 2.0 | 5 votes |
public RestClient createRestClient(ElasticSearchConfig config) { List<String> esHosts = config.getRestApiHosts(); List<HttpHost> hosts = new ArrayList<>(); for (String host : esHosts) { try { URL url = new URL(host); hosts.add(new HttpHost(url.getHost(), url.getPort())); } catch (MalformedURLException e) { throw new RuntimeException("Failed to initialize Elasticsearch REST client. " + "Invalid host: " + host, e); } } HttpHost[] httpHostsArray = (HttpHost[]) hosts.toArray(new HttpHost[hosts.size()]); client = RestClient.builder(httpHostsArray) .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder .setConnectTimeout(config.getRestConnectTimeout()) .setSocketTimeout(config.getRestSocketTimeout()); } }) .setMaxRetryTimeoutMillis(config.getRestMaxRetryTimeoutMillis()) .build(); logger.info("Initialized Elasticsearch REST client for hosts: "+Arrays.toString(httpHostsArray)); return client; }