当前位置: 首页>>代码示例>>Java>>正文


Java RestClientBuilder类代码示例

本文整理汇总了Java中org.elasticsearch.client.RestClientBuilder的典型用法代码示例。如果您正苦于以下问题:Java RestClientBuilder类的具体用法?Java RestClientBuilder怎么用?Java RestClientBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


RestClientBuilder类属于org.elasticsearch.client包,在下文中一共展示了RestClientBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: ElasticBulkSender

import org.elasticsearch.client.RestClientBuilder; //导入依赖的package包/类
public ElasticBulkSender(String user, String password, HttpHost... hosts) {
	this.user = user;
	this.password = password;
	this.hosts = hosts;
	this.restClient = RestClient.builder(hosts)
			.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
				@Override
				public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
					if (!Strings.isBlank(user)) {
						CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
						credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
		                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
					} else {
						return httpClientBuilder;
					}
				}
	        })
			.build();
}
 
开发者ID:magrossi,项目名称:es-log4j2-appender,代码行数:20,代码来源:ElasticBulkSender.java

示例2: setUp

import org.elasticsearch.client.RestClientBuilder; //导入依赖的package包/类
@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 InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
    elasticsearchTemplate = new ElasticsearchTemplate(client);
    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();
}
 
开发者ID:felayman,项目名称:elasticsearch-full,代码行数:23,代码来源:XPackBaseDemo.java

示例3: createClient

import org.elasticsearch.client.RestClientBuilder; //导入依赖的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

示例4: startRestClient

import org.elasticsearch.client.RestClientBuilder; //导入依赖的package包/类
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());
    }
}
 
开发者ID:dzh,项目名称:jframe,代码行数:23,代码来源:JfDemoESPlugin.java

示例5: init

import org.elasticsearch.client.RestClientBuilder; //导入依赖的package包/类
@Before
public void init() {
    HttpHost host = new HttpHost("127.0.0.1", 30002, "http");
    // HttpHost host = new HttpHost("127.0.0.1", 9002, "http");
    client = RestClient.builder(host).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();
}
 
开发者ID:dzh,项目名称:jframe,代码行数:24,代码来源:TestQuery.java

示例6: init

import org.elasticsearch.client.RestClientBuilder; //导入依赖的package包/类
@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();
}
 
开发者ID:dzh,项目名称:jframe,代码行数:22,代码来源:TestImportData.java

示例7: ElasticSearchFilter

import org.elasticsearch.client.RestClientBuilder; //导入依赖的package包/类
@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();
}
 
开发者ID:HuygensING,项目名称:timbuctoo,代码行数:23,代码来源:ElasticSearchFilter.java

示例8: buildClient

import org.elasticsearch.client.RestClientBuilder; //导入依赖的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

示例9: run

import org.elasticsearch.client.RestClientBuilder; //导入依赖的package包/类
public void run(Configuration configuration, Environment environment) throws Exception {
  final CollectorRegistry collectorRegistry = new CollectorRegistry();
  collectorRegistry.register(new DropwizardExports(environment.metrics()));
  environment.admin()
      .addServlet("metrics", new MetricsServlet(collectorRegistry))
      .addMapping("/metrics");

  final PrometheusMetricsReporter reporter = PrometheusMetricsReporter.newMetricsReporter()
      .withCollectorRegistry(collectorRegistry)
      .withConstLabel("service", getName())
      .build();

  final Tracer tracer = getTracer();
  final Tracer metricsTracer = io.opentracing.contrib.metrics.Metrics.decorate(tracer, reporter);
  GlobalTracer.register(metricsTracer);

  final DynamicFeature tracing = new ServerTracingDynamicFeature.Builder(metricsTracer).build();
  environment.jersey().register(tracing);

  final HttpHost httpHost = new HttpHost("tweets-elasticsearch", 9200);
  final RestClientBuilder restClientBuilder =
      RestClient.builder(httpHost).setHttpClientConfigCallback(new TracingHttpClientConfigCallback(metricsTracer));
  final RestClient restClient = restClientBuilder.build();
  final RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClient);
  final TweetRepository tweetRepository = new ElasticsearchTweetRepository(restHighLevelClient);
  final TweetsResource tweetsResource = new TweetsResource(tweetRepository);
  environment.jersey().register(tweetsResource);
}
 
开发者ID:jeqo,项目名称:talk-observing-distributed-systems,代码行数:29,代码来源:SearchServiceApplication.java

示例10: run

import org.elasticsearch.client.RestClientBuilder; //导入依赖的package包/类
public void run(Configuration configuration, Environment environment) throws Exception {
  // INSTRUMENTATION
  // Metrics Instrumentation
  final CollectorRegistry collectorRegistry = new CollectorRegistry();
  collectorRegistry.register(new DropwizardExports(environment.metrics()));
  environment.admin()
      .addServlet("metrics", new MetricsServlet(collectorRegistry))
      .addMapping("/metrics");

  final PrometheusMetricsReporter reporter = PrometheusMetricsReporter.newMetricsReporter()
      .withCollectorRegistry(collectorRegistry)
      .withConstLabel("service", getName())
      .build();

  // Tracing Instrumentation
  final Tracer tracer = getTracer();
  final Tracer metricsTracer = io.opentracing.contrib.metrics.Metrics.decorate(tracer, reporter);
  GlobalTracer.register(metricsTracer);

  final HttpHost httpHost = new HttpHost("tweets-elasticsearch", 9200);
  final RestClientBuilder restClientBuilder =
      RestClient.builder(httpHost).setHttpClientConfigCallback(new TracingHttpClientConfigCallback(metricsTracer));
  final RestClient restClient = restClientBuilder.build();
  final ElasticsearchTweetRepository elasticsearchRepository = new ElasticsearchTweetRepository(restClient);

  final Properties consumerConfigs = new Properties();
  consumerConfigs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "tweets-kafka:9092");
  consumerConfigs.put(ConsumerConfig.GROUP_ID_CONFIG, getName());
  consumerConfigs.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
  consumerConfigs.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
  final KafkaConsumer<Long, String> kafkaConsumer = new KafkaConsumer<>(consumerConfigs, new LongDeserializer(), new StringDeserializer());
  final TracingKafkaConsumer<Long, String> tracingKafkaConsumer = new TracingKafkaConsumer<>(kafkaConsumer, metricsTracer);
  final Runnable kafkaTweetEventConsumer = new KafkaTweetEventConsumer(tracingKafkaConsumer, elasticsearchRepository);
  final ExecutorService executorService = environment.lifecycle().executorService("kafka-consumer").build();
  executorService.submit(kafkaTweetEventConsumer);
}
 
开发者ID:jeqo,项目名称:talk-observing-distributed-systems,代码行数:37,代码来源:IndexerServiceApplication.java

示例11: createClient

import org.elasticsearch.client.RestClientBuilder; //导入依赖的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

示例12: initESClient

import org.elasticsearch.client.RestClientBuilder; //导入依赖的package包/类
private void initESClient() throws NumberFormatException,
        UnknownHostException {

    List<HttpHost> httpHostList = hosts.stream().map(hostString -> {
        String[] parsedHost = hostString.split(":");
        String host = parsedHost[0];
        int port = parsedHost.length == 2 ? Integer.valueOf(parsedHost[1]) : 9200;
        return new HttpHost(host, port);
    }).collect(toList());
    List<HttpHost> clusterHosts = unmodifiableList(httpHostList);

    if (config.containsKey("user") && config.containsKey("password")) {
        String user = config.get("user").toString();
        String password = config.get("password").toString();
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(user, password));

        restClient = RestClient.builder(clusterHosts.toArray(new HttpHost[clusterHosts.size()]))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                }).build();
    } else {
        restClient = RestClient.builder(clusterHosts.toArray(new HttpHost[clusterHosts.size()]))
                .build();
    }
    if (this.isSniff) {
        sniffer = Sniffer.builder(restClient).build();
    }

}
 
开发者ID:childe,项目名称:hangout,代码行数:35,代码来源:ElasticsearchHTTP.java

示例13: startRestClient

import org.elasticsearch.client.RestClientBuilder; //导入依赖的package包/类
private static void startRestClient() throws IOException {
    if (client == null) {
        client = RestClient.builder(new HttpHost(testClusterHost, testClusterRestPort, testClusterScheme)).build();

        securityInstalled = testClusterRunning(false);
        if (securityInstalled) {
            // We have a secured cluster. So we need to create a secured client
            // But first we need to close the previous client we built
            if (client != null) {
                client.close();
            }

            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(testClusterUser, testClusterPass));

            client = RestClient.builder(new HttpHost(testClusterHost, testClusterRestPort, testClusterScheme))
                    .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                        @Override
                        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                            return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                        }
                    })
                    .build();
            securityInstalled = testClusterRunning(true);
        }
    }
}
 
开发者ID:dadoonet,项目名称:elasticsearch-beyonder,代码行数:29,代码来源:AbstractBeyonderTest.java

示例14: createRestClient

import org.elasticsearch.client.RestClientBuilder; //导入依赖的package包/类
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;
    }
 
开发者ID:ViDA-NYU,项目名称:ache,代码行数:32,代码来源:ElasticSearchRestTargetRepository.java

示例15: getESHosts

import org.elasticsearch.client.RestClientBuilder; //导入依赖的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


注:本文中的org.elasticsearch.client.RestClientBuilder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。