Java Code Examples for org.elasticsearch.client.RestClient#builder()
The following examples show how to use
org.elasticsearch.client.RestClient#builder() .
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: 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 2
Source File: ElasticsearchConfig.java From staccato with Apache License 2.0 | 6 votes |
/** * Registers an instance of the high level client for Elasticsearch. * * @return An instance of Elasticsearch's high level rest client */ @Bean public RestHighLevelClient restHighLevelClient() { RestClientBuilder builder = RestClient.builder(new HttpHost(configProps.getHost(), configProps.getPort(), configProps.getScheme())); RestClientBuilder.HttpClientConfigCallback httpClientConfigCallback = httpAsyncClientBuilder -> { httpAsyncClientBuilder .setMaxConnTotal(configProps.getRestClientMaxConnectionsTotal()) .setMaxConnPerRoute(configProps.getRestClientMaxConnectionsPerRoute()); if (null != configProps.getUser() && !configProps.getUser().isEmpty()) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(configProps.getUser(), configProps.getPassword())); httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } return httpAsyncClientBuilder; }; builder.setHttpClientConfigCallback(httpClientConfigCallback); builder.setMaxRetryTimeoutMillis(configProps.getRestClientMaxRetryTimeoutMillis()); //return new RestHighLevelClient(builder.build()); return new RestHighLevelClient(builder); }
Example 3
Source File: CommonService.java From pacbot with Apache License 2.0 | 5 votes |
private RestClient getRestClient() { if (restClient == null) { String esHost = config.getElasticSearch().getDevIngestHost(); int esPort = config.getElasticSearch().getDevIngestPort(); RestClientBuilder builder = RestClient.builder(new HttpHost(esHost, esPort)); RequestConfigCallback requestConfigCallback = requestConfigBuilder -> requestConfigBuilder .setConnectionRequestTimeout(0); builder.setRequestConfigCallback(requestConfigCallback); restClient = builder.build(); } return restClient; }
Example 4
Source File: VulnerabilityTrendGenerator.java From pacbot with Apache License 2.0 | 5 votes |
private RestClient getRestClient() { RestClientBuilder builder = RestClient.builder(new HttpHost(esHost, esPort)); builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder.setConnectionRequestTimeout(0).setSocketTimeout(60000); } }).setMaxRetryTimeoutMillis(60000); return builder.build(); }
Example 5
Source File: Elasticsearch6ApiCallBridge.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 6
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 7
Source File: Select.java From code with Apache License 2.0 | 5 votes |
@Before public void init() { //1.连接rest接口 HttpHost http = new HttpHost("127.0.0.1", 9200, "http"); RestClientBuilder builder = RestClient.builder(http);//rest构建器 restHighLevelClient = new RestHighLevelClient(builder);//高级客户端对象 //2.封装查询请求 searchRequest = new SearchRequest("sku"); searchRequest.types("doc"); //设置查询的类型 }
Example 8
Source File: ElasticsearchCollector.java From karaf-decanter with Apache License 2.0 | 5 votes |
public void activate(Dictionary<String, Object> configuration) { this.configuration = configuration; String addressesString = (configuration.get("addresses") != null) ? configuration.get("addresses").toString() : "http://localhost:9200"; String username = (configuration.get("username") != null) ? configuration.get("username").toString() : null; String password = (configuration.get("password") != null) ? configuration.get("password").toString() : null; Set<String> addresses = new HashSet<>(Arrays.asList(addressesString.split(","))); HttpHost[] hosts = new HttpHost[addresses.size()]; int i = 0; for (String address : addresses) { try { URL url = new URL(address); hosts[i] = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); i++; } catch (Exception e) { LOGGER.warn("Bad elasticsearch address {}", address, e); } } RestClientBuilder restClientBuilder = RestClient.builder(hosts); restClientBuilder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(1000) .setSocketTimeout(10000)); if (username != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); restClientBuilder.setHttpClientConfigCallback( new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } } ); } restClient = new RestHighLevelClient(restClientBuilder); }
Example 9
Source File: ESConfiguration.java From SkaETL with Apache License 2.0 | 5 votes |
@Bean public RestHighLevelClient elasticsearchRestConnection(ESConfiguration esConfiguration) { String auth = esConfiguration.getServiceElasticsearchUsername() + ":" + esConfiguration.getServiceElasticsearchPassword(); String authB64; try { authB64 = Base64.getEncoder().encodeToString(auth.getBytes("utf-8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Impossible encoding for user " + esConfiguration.getServiceElasticsearchUsername() + " and password " + esConfiguration.getServiceElasticsearchPassword() + " msg " + e); } RestClientBuilder builder = RestClient.builder( new HttpHost(esConfiguration.getHost(), Integer.valueOf(esConfiguration.getPort()), "http")); Header[] defaultHeaders = new Header[]{ new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"), new BasicHeader("cluster.name", esConfiguration.getClusterName()), new BasicHeader(HttpHeaders.AUTHORIZATION, "Basic " + authB64) }; builder.setDefaultHeaders(defaultHeaders); builder.setMaxRetryTimeoutMillis(esConfiguration.getSocketTimeout() * 1000); builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder .setConnectionRequestTimeout(connectionRequestTimeout) .setConnectTimeout(esConfiguration.getConnectionTimeout() * 1000) .setSocketTimeout(esConfiguration.getSocketTimeout() * 1000); } }); return new RestHighLevelClient(builder); }
Example 10
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 11
Source File: ElasticSearchImpl.java From core-ng-project with Apache License 2.0 | 5 votes |
public void initialize() { RestClientBuilder builder = RestClient.builder(hosts); builder.setRequestConfigCallback(config -> config.setSocketTimeout((int) timeout.toMillis()) .setConnectionRequestTimeout((int) timeout.toMillis())); // timeout of requesting connection from connection pool builder.setHttpClientConfigCallback(config -> config.setMaxConnTotal(100) .setMaxConnPerRoute(100) .setKeepAliveStrategy((response, context) -> Duration.ofSeconds(30).toMillis())); client = new RestHighLevelClient(builder); }
Example 12
Source File: AwsRestHighLevelClient.java From aws-athena-query-federation with Apache License 2.0 | 5 votes |
/** * A constructor for the client builder. * @param endpoint is the cluster's endpoint and is injected into the builder. */ public Builder(String endpoint) { this.endpoint = endpoint; this.clientBuilder = RestClient.builder(HttpHost.create(this.endpoint)); this.signer = new AWS4Signer(); this.domainSplitter = Splitter.on("."); }
Example 13
Source File: ElasticsearchSchemaFactory.java From calcite with Apache License 2.0 | 5 votes |
/** * Builds elastic rest client from user configuration * @param hosts list of ES HTTP Hosts to connect to * @return newly initialized low-level rest http client for ES */ private static RestClient connect(List<HttpHost> hosts, String pathPrefix) { Objects.requireNonNull(hosts, "hosts or coordinates"); Preconditions.checkArgument(!hosts.isEmpty(), "no ES hosts specified"); RestClientBuilder builder = RestClient.builder(hosts.toArray(new HttpHost[hosts.size()])); if (pathPrefix != null && !pathPrefix.isEmpty()) { builder.setPathPrefix(pathPrefix); } return builder.build(); }
Example 14
Source File: ElasticSearchClient.java From camel-kafka-connector with Apache License 2.0 | 5 votes |
public ElasticSearchClient(String host, int port, String index) { client = new RestHighLevelClient( RestClient.builder( new HttpHost(host, port, "http"))); this.index = index; }
Example 15
Source File: DataServiceImpl.java From java-11-examples with Apache License 2.0 | 5 votes |
public DataServiceImpl(String hostname, int port, String scheme) { client = new RestHighLevelClient( RestClient.builder( new HttpHost(hostname, port, scheme) ) ); }
Example 16
Source File: EsHighLevelRestSearchTest.java From java-study with Apache License 2.0 | 4 votes |
private static void init() { RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(elasticIp, elasticPort)); client = new RestHighLevelClient(restClientBuilder); }
Example 17
Source File: ElasticsearchAppender.java From karaf-decanter with Apache License 2.0 | 4 votes |
public void open(Dictionary<String, Object> config) { this.config = config; String addressesString = getValue(config, ADDRESSES_PROPERTY, ADDRESSES_DEFAULT); String username = getValue(config, USERNAME_PROPERTY, USERNAME_DEFAULT); String password = getValue(config, PASSWORD_PROPERTY, PASSWORD_DEFAULT); Set<String> addresses = new HashSet<String>(Arrays.asList(addressesString.split(","))); HttpHost[] hosts = new HttpHost[addresses.size()]; int i = 0; for (String address : addresses) { try { URL url = new URL(address); hosts[i] = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); i++; } catch (Exception e) { LOGGER.warn("Bad elasticsearch address {}", address, e); } } RestClientBuilder restClientBuilder = RestClient.builder(hosts); restClientBuilder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder.setConnectTimeout(1000) .setSocketTimeout(10000); } }); if (username != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); restClientBuilder.setHttpClientConfigCallback( new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } } ); } client = restClientBuilder.build(); TimeZone tz = TimeZone.getTimeZone( "UTC" ); tsFormat.setTimeZone(tz); indexDateFormat.setTimeZone(tz); }
Example 18
Source File: ElasticSearchRestClientBuilderProvider.java From conductor with Apache License 2.0 | 4 votes |
@Override public RestClientBuilder get() { return RestClient.builder(convertToHttpHosts(configuration.getURIs())); }
Example 19
Source File: ElasticsearchTransactionManagerTestCase.java From jstarcraft-core with Apache License 2.0 | 4 votes |
@Before public void testBefore() { elasticClient = new RestHighLevelClient(RestClient.builder(new HttpHost(EMBEDDED_ELASTIC_HOST, EMBEDDED_ELASTIC_PORT, "http"))); }
Example 20
Source File: EsHighLevelRestTest2.java From java-study with Apache License 2.0 | 2 votes |
private static void init() { client = new RestHighLevelClient(RestClient.builder(new HttpHost(elasticIp, elasticPort, "http"))); }