當前位置: 首頁>>代碼示例>>Java>>正文


Java RestClient.builder方法代碼示例

本文整理匯總了Java中org.elasticsearch.client.RestClient.builder方法的典型用法代碼示例。如果您正苦於以下問題:Java RestClient.builder方法的具體用法?Java RestClient.builder怎麽用?Java RestClient.builder使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.elasticsearch.client.RestClient的用法示例。


在下文中一共展示了RestClient.builder方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createClient

import org.elasticsearch.client.RestClient; //導入方法依賴的package包/類
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();
}
 
開發者ID:Talend,項目名稱:components,代碼行數:25,代碼來源:ElasticsearchConnection.java

示例2: buildClient

import org.elasticsearch.client.RestClient; //導入方法依賴的package包/類
protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOException {
    RestClientBuilder builder = RestClient.builder(hosts);
    String keystorePath = settings.get(TRUSTSTORE_PATH);
    if (keystorePath != null) {
        final String keystorePass = settings.get(TRUSTSTORE_PASSWORD);
        if (keystorePass == null) {
            throw new IllegalStateException(TRUSTSTORE_PATH + " is provided but not " + TRUSTSTORE_PASSWORD);
        }
        Path path = PathUtils.get(keystorePath);
        if (!Files.exists(path)) {
            throw new IllegalStateException(TRUSTSTORE_PATH + " is set but points to a non-existing file");
        }
        try {
            KeyStore keyStore = KeyStore.getInstance("jks");
            try (InputStream is = Files.newInputStream(path)) {
                keyStore.load(is, keystorePass.toCharArray());
            }
            SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(keyStore, null).build();
            SSLIOSessionStrategy sessionStrategy = new SSLIOSessionStrategy(sslcontext);
            builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setSSLStrategy(sessionStrategy));
        } catch (KeyStoreException|NoSuchAlgorithmException|KeyManagementException|CertificateException e) {
            throw new RuntimeException("Error setting up ssl", e);
        }
    }

    try (ThreadContext threadContext = new ThreadContext(settings)) {
        Header[] defaultHeaders = new Header[threadContext.getHeaders().size()];
        int i = 0;
        for (Map.Entry<String, String> entry : threadContext.getHeaders().entrySet()) {
            defaultHeaders[i++] = new BasicHeader(entry.getKey(), entry.getValue());
        }
        builder.setDefaultHeaders(defaultHeaders);
    }
    return builder.build();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:36,代碼來源:ESRestTestCase.java

示例3: createClient

import org.elasticsearch.client.RestClient; //導入方法依賴的package包/類
@VisibleForTesting
RestClient createClient() throws IOException {
  HttpHost[] hosts = new HttpHost[getAddresses().size()];
  int i = 0;
  for (String address : getAddresses()) {
    URL url = new URL(address);
    hosts[i] = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
    i++;
  }
  RestClientBuilder restClientBuilder = RestClient.builder(hosts);
  if (getUsername() != null) {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(
        AuthScope.ANY, new UsernamePasswordCredentials(getUsername(), getPassword()));
    restClientBuilder.setHttpClientConfigCallback(
        new RestClientBuilder.HttpClientConfigCallback() {
          public HttpAsyncClientBuilder customizeHttpClient(
              HttpAsyncClientBuilder httpAsyncClientBuilder) {
            return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
          }
        });
  }
  if (getKeystorePath() != null && !getKeystorePath().isEmpty()) {
    try {
      KeyStore keyStore = KeyStore.getInstance("jks");
      try (InputStream is = new FileInputStream(new File(getKeystorePath()))) {
        String keystorePassword = getKeystorePassword();
        keyStore.load(is, (keystorePassword == null) ? null : keystorePassword.toCharArray());
      }
      final SSLContext sslContext = SSLContexts.custom()
          .loadTrustMaterial(keyStore, new TrustSelfSignedStrategy()).build();
      final SSLIOSessionStrategy sessionStrategy = new SSLIOSessionStrategy(sslContext);
      restClientBuilder.setHttpClientConfigCallback(
          new RestClientBuilder.HttpClientConfigCallback() {
        @Override
        public HttpAsyncClientBuilder customizeHttpClient(
            HttpAsyncClientBuilder httpClientBuilder) {
          return httpClientBuilder.setSSLContext(sslContext).setSSLStrategy(sessionStrategy);
        }
      });
    } catch (Exception e) {
      throw new IOException("Can't load the client certificate from the keystore", e);
    }
  }
  return restClientBuilder.build();
}
 
開發者ID:apache,項目名稱:beam,代碼行數:47,代碼來源:ElasticsearchIO.java

示例4: getESHosts

import org.elasticsearch.client.RestClient; //導入方法依賴的package包/類
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.valueOf(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);
}
 
開發者ID:asdf2014,項目名稱:yuzhouwan,代碼行數:29,代碼來源:ESUtils.java

示例5: createClient

import org.elasticsearch.client.RestClient; //導入方法依賴的package包/類
private ElasticSearchRestClient createClient(final DataContextProperties properties) throws MalformedURLException {
    final URL url = new URL(properties.getUrl());
    final RestClientBuilder builder = RestClient.builder(new HttpHost(url.getHost(), url.getPort()));
    
    if (properties.getUsername() != null) {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(properties.getUsername(),
                properties.getPassword()));

        builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(
                credentialsProvider));
    }

    return new ElasticSearchRestClient(builder.build());
}
 
開發者ID:apache,項目名稱:metamodel,代碼行數:16,代碼來源:ElasticSearchRestDataContextFactory.java

示例6: createDataStore

import org.elasticsearch.client.RestClient; //導入方法依賴的package包/類
@Override
public DataStore createDataStore(Map<String, Serializable> params) throws IOException {
    final String searchHost = (String) getValue(HOSTNAME, params);
    final Integer hostPort = (Integer) getValue(HOSTPORT, params);
    final String indexName = (String) INDEX_NAME.lookUp(params);
    final String arrayEncoding = (String) getValue(ARRAY_ENCODING, params);
    final Boolean sslEnabled = (Boolean) getValue(SSL_ENABLED, params);
    final Boolean sslRejectUnauthorized = (Boolean) getValue(SSL_REJECT_UNAUTHORIZED, params);

    final String scheme = sslEnabled ? "https" : "http";
    final RestClientBuilder builder = RestClient.builder(new HttpHost(searchHost, hostPort, scheme));

    if (sslEnabled) {
        builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.useSystemProperties();
                if (!sslRejectUnauthorized) {
                    httpClientBuilder.setSSLHostnameVerifier((host,session) -> true);
                    try {
                        httpClientBuilder.setSSLContext(SSLContextBuilder.create().loadTrustMaterial((chain,authType) -> true).build());
                    } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
                        throw new UncheckedIOException(new IOException("Unable to create SSLContext", e));
                    }
                }
                return httpClientBuilder;
            }
        });
    }

    final ElasticDataStore dataStore = new ElasticDataStore(builder.build(), indexName);
    dataStore.setDefaultMaxFeatures((Integer) getValue(DEFAULT_MAX_FEATURES, params));
    dataStore.setSourceFilteringEnabled((Boolean) getValue(SOURCE_FILTERING_ENABLED, params));
    dataStore.setScrollEnabled((Boolean)getValue(SCROLL_ENABLED, params));
    dataStore.setScrollSize(((Number)getValue(SCROLL_SIZE, params)).longValue());
    dataStore.setScrollTime((Integer)getValue(SCROLL_TIME_SECONDS, params));
    dataStore.setArrayEncoding(ArrayEncoding.valueOf(arrayEncoding.toUpperCase()));
    dataStore.setGridSize((Long) GRID_SIZE.lookUp(params));
    dataStore.setGridThreshold((Double) GRID_THRESHOLD.lookUp(params));
    return dataStore;
}
 
開發者ID:ngageoint,項目名稱:elasticgeo,代碼行數:42,代碼來源:ElasticDataStoreFactory.java


注:本文中的org.elasticsearch.client.RestClient.builder方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。