Search in sources :

Example 6 with JestClientFactory

use of io.searchbox.client.JestClientFactory in project vind by RBMHTechnology.

the class ElasticSearchClientBuilder method build.

public static JestClient build(String host, String port) {
    final String conn = String.format("http://%s:%s", host, port);
    log.info("Creating ElasticSearch REST Client over {}..,", conn);
    final JestClientFactory factory = new JestClientFactory();
    factory.setHttpClientConfig(new HttpClientConfig.Builder(conn).multiThreaded(true).build());
    return factory.getObject();
}
Also used : HttpClientConfig(io.searchbox.client.config.HttpClientConfig) JestClientFactory(io.searchbox.client.JestClientFactory)

Example 7 with JestClientFactory

use of io.searchbox.client.JestClientFactory in project DataX by alibaba.

the class ESClient method createClient.

public void createClient(String endpoint, String user, String passwd, boolean multiThread, int readTimeout, boolean compression, boolean discovery) {
    JestClientFactory factory = new JestClientFactory();
    Builder httpClientConfig = new HttpClientConfig.Builder(endpoint).setPreemptiveAuth(new HttpHost(endpoint)).multiThreaded(multiThread).connTimeout(30000).readTimeout(readTimeout).maxTotalConnection(200).requestCompressionEnabled(compression).discoveryEnabled(discovery).discoveryFrequency(5l, TimeUnit.MINUTES);
    if (!("".equals(user) || "".equals(passwd))) {
        httpClientConfig.defaultCredentials(user, passwd);
    }
    factory.setHttpClientConfig(httpClientConfig.build());
    jestClient = factory.getObject();
}
Also used : HttpHost(org.apache.http.HttpHost) Builder(io.searchbox.client.config.HttpClientConfig.Builder) JestClientFactory(io.searchbox.client.JestClientFactory)

Example 8 with JestClientFactory

use of io.searchbox.client.JestClientFactory in project spring-boot by spring-projects.

the class JestAutoConfiguration method jestClient.

@Bean(destroyMethod = "shutdownClient")
@ConditionalOnMissingBean
public JestClient jestClient() {
    JestClientFactory factory = new JestClientFactory();
    factory.setHttpClientConfig(createHttpClientConfig());
    return factory.getObject();
}
Also used : JestClientFactory(io.searchbox.client.JestClientFactory) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 9 with JestClientFactory

use of io.searchbox.client.JestClientFactory in project opennms by OpenNMS.

the class ClientRecoveryTest method test.

@Test
public void test() {
    LOG.debug("***************** start of test ClientRecoveryTest");
    try {
        IndexNameFunction indexNameFunction = new IndexNameFunction();
        String rootIndexName = EventToIndex.INDEX_NAMES.get(EventToIndex.Indices.ALARMS);
        String indexName = indexNameFunction.apply(rootIndexName, new Date());
        // Get Jest client
        HttpClientConfig clientConfig = new HttpClientConfig.Builder("http://localhost:9200").multiThreaded(true).build();
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(clientConfig);
        JestClient jestClient = factory.getObject();
        try {
            String query = "{\n" + "\n       \"query\": {" + "\n         \"match\": {" + "\n         \"alarmid\": \"1359\"" + "\n          }" + "\n        }" + "\n     }";
            Search search = new Search.Builder(query).addIndex(indexName).build();
            SearchResult sresult = jestClient.execute(search);
            LOG.debug("received search result: " + sresult.getJsonString() + "\n   response code:" + sresult.getResponseCode() + "\n   error message: " + sresult.getErrorMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // shutdown client
            jestClient.shutdownClient();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    LOG.debug("***************** end of test ClientRecoveryTest");
}
Also used : IndexNameFunction(org.opennms.plugins.elasticsearch.rest.IndexNameFunction) HttpClientConfig(io.searchbox.client.config.HttpClientConfig) Search(io.searchbox.core.Search) SearchResult(io.searchbox.core.SearchResult) JestClient(io.searchbox.client.JestClient) JestClientFactory(io.searchbox.client.JestClientFactory) Date(java.util.Date) Test(org.junit.Test)

Example 10 with JestClientFactory

use of io.searchbox.client.JestClientFactory in project nutch by apache.

the class ElasticRestIndexWriter method open.

@Override
public void open(Configuration conf, String name) throws IOException {
    hosts = conf.getStrings(ElasticRestConstants.HOST);
    port = conf.getInt(ElasticRestConstants.PORT, 9200);
    user = conf.get(ElasticRestConstants.USER);
    password = conf.get(ElasticRestConstants.PASSWORD);
    https = conf.getBoolean(ElasticRestConstants.HTTPS, false);
    trustAllHostnames = conf.getBoolean(ElasticRestConstants.HOSTNAME_TRUST, false);
    languages = conf.getStrings(ElasticRestConstants.LANGUAGES);
    separator = conf.get(ElasticRestConstants.SEPARATOR, DEFAULT_SEPARATOR);
    sink = conf.get(ElasticRestConstants.SINK, DEFAULT_SINK);
    // trust ALL certificates
    SSLContext sslContext = null;
    try {
        sslContext = new SSLContextBuilder().loadTrustMaterial(new TrustStrategy() {

            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                return true;
            }
        }).build();
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        LOG.error("Failed to instantiate sslcontext object: \n{}", ExceptionUtils.getStackTrace(e));
        throw new SecurityException();
    }
    // skip hostname checks
    HostnameVerifier hostnameVerifier = null;
    if (trustAllHostnames) {
        hostnameVerifier = NoopHostnameVerifier.INSTANCE;
    } else {
        hostnameVerifier = new DefaultHostnameVerifier();
    }
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
    SchemeIOSessionStrategy httpsIOSessionStrategy = new SSLIOSessionStrategy(sslContext, hostnameVerifier);
    JestClientFactory jestClientFactory = new JestClientFactory();
    if (hosts == null || hosts.length == 0 || port <= 1) {
        throw new IllegalStateException("No hosts or port specified. Please set the host and port in nutch-site.xml");
    }
    List<String> urlsOfElasticsearchNodes = new ArrayList<String>();
    for (String host : hosts) {
        urlsOfElasticsearchNodes.add(new URL(https ? "https" : "http", host, port, "").toString());
    }
    HttpClientConfig.Builder builder = new HttpClientConfig.Builder(urlsOfElasticsearchNodes).multiThreaded(true).connTimeout(300000).readTimeout(300000);
    if (https) {
        if (user != null && password != null) {
            builder.defaultCredentials(user, password);
        }
        builder.defaultSchemeForDiscoveredNodes("https").sslSocketFactory(// this only affects sync calls
        sslSocketFactory).httpsIOSessionStrategy(// this only affects async calls
        httpsIOSessionStrategy);
    }
    jestClientFactory.setHttpClientConfig(builder.build());
    client = jestClientFactory.getObject();
    defaultIndex = conf.get(ElasticRestConstants.INDEX, "nutch");
    defaultType = conf.get(ElasticRestConstants.TYPE, "doc");
    maxBulkDocs = conf.getInt(ElasticRestConstants.MAX_BULK_DOCS, DEFAULT_MAX_BULK_DOCS);
    maxBulkLength = conf.getInt(ElasticRestConstants.MAX_BULK_LENGTH, DEFAULT_MAX_BULK_LENGTH);
    bulkBuilder = new Bulk.Builder().defaultIndex(defaultIndex).defaultType(defaultType);
}
Also used : TrustStrategy(org.apache.http.ssl.TrustStrategy) ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyManagementException(java.security.KeyManagementException) URL(java.net.URL) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) HttpClientConfig(io.searchbox.client.config.HttpClientConfig) SchemeIOSessionStrategy(org.apache.http.nio.conn.SchemeIOSessionStrategy) SSLContext(javax.net.ssl.SSLContext) KeyStoreException(java.security.KeyStoreException) JestClientFactory(io.searchbox.client.JestClientFactory) Bulk(io.searchbox.core.Bulk) X509Certificate(java.security.cert.X509Certificate) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier)

Aggregations

JestClientFactory (io.searchbox.client.JestClientFactory)14 HttpClientConfig (io.searchbox.client.config.HttpClientConfig)9 JestClient (io.searchbox.client.JestClient)3 Search (io.searchbox.core.Search)3 SearchResult (io.searchbox.core.SearchResult)3 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)3 GsonBuilder (com.google.gson.GsonBuilder)2 Builder (io.searchbox.client.config.HttpClientConfig.Builder)2 Gson (com.google.gson.Gson)1 JestHttpClient (io.searchbox.client.http.JestHttpClient)1 Bulk (io.searchbox.core.Bulk)1 IndicesExists (io.searchbox.indices.IndicesExists)1 URL (java.net.URL)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1