Search in sources :

Example 21 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project nutch by apache.

the class ElasticIndexWriter method makeClient.

/**
 * Generates a RestHighLevelClient with the hosts given
 * @param parameters implementation specific {@link org.apache.nutch.indexer.IndexWriterParams}
 * @return an initialized {@link org.elasticsearch.client.RestHighLevelClient}
 * @throws IOException if there is an error reading the
 * {@link org.apache.nutch.indexer.IndexWriterParams}
 */
protected RestHighLevelClient makeClient(IndexWriterParams parameters) throws IOException {
    hosts = parameters.getStrings(ElasticConstants.HOSTS);
    port = parameters.getInt(ElasticConstants.PORT, DEFAULT_PORT);
    scheme = parameters.get(ElasticConstants.SCHEME, HttpHost.DEFAULT_SCHEME_NAME);
    auth = parameters.getBoolean(ElasticConstants.USE_AUTH, false);
    user = parameters.get(ElasticConstants.USER, DEFAULT_USER);
    password = parameters.get(ElasticConstants.PASSWORD, "");
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
    RestHighLevelClient client = null;
    if (hosts != null && port > 1) {
        HttpHost[] hostsList = new HttpHost[hosts.length];
        int i = 0;
        for (String host : hosts) {
            hostsList[i++] = new HttpHost(host, port, scheme);
        }
        RestClientBuilder restClientBuilder = RestClient.builder(hostsList);
        if (auth) {
            restClientBuilder.setHttpClientConfigCallback(new HttpClientConfigCallback() {

                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder arg0) {
                    return arg0.setDefaultCredentialsProvider(credentialsProvider);
                }
            });
        }
        client = new RestHighLevelClient(restClientBuilder);
    } else {
        throw new IOException("ElasticRestClient initialization Failed!!!\\n\\nPlease Provide the hosts");
    }
    return client;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) HttpHost(org.apache.http.HttpHost) HttpClientConfigCallback(org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback)

Example 22 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project metron by apache.

the class ElasticsearchClientFactory method create.

/**
 * Creates an Elasticsearch client from settings provided via the global config.
 *
 * @return new client
 */
public static ElasticsearchClient create(Map<String, Object> globalConfig) {
    ElasticsearchClientConfig esClientConfig = new ElasticsearchClientConfig(getEsSettings(globalConfig));
    HttpHost[] httpHosts = getHttpHosts(globalConfig, esClientConfig.getConnectionScheme());
    RestClientBuilder builder = RestClient.builder(httpHosts);
    builder.setRequestConfigCallback(reqConfigBuilder -> {
        // Modifies request config builder with connection and socket timeouts.
        // https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.6/_timeouts.html
        reqConfigBuilder.setConnectTimeout(esClientConfig.getConnectTimeoutMillis());
        reqConfigBuilder.setSocketTimeout(esClientConfig.getSocketTimeoutMillis());
        return reqConfigBuilder;
    });
    builder.setMaxRetryTimeoutMillis(esClientConfig.getMaxRetryTimeoutMillis());
    builder.setHttpClientConfigCallback(clientBuilder -> {
        clientBuilder.setDefaultIOReactorConfig(getIOReactorConfig(esClientConfig));
        clientBuilder.setDefaultCredentialsProvider(getCredentialsProvider(esClientConfig));
        clientBuilder.setSSLContext(getSSLContext(esClientConfig));
        return clientBuilder;
    });
    RestClient lowLevelClient = builder.build();
    RestHighLevelClient client = new RestHighLevelClient(lowLevelClient);
    return new ElasticsearchClient(lowLevelClient, client);
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ElasticsearchClientConfig(org.apache.metron.elasticsearch.config.ElasticsearchClientConfig)

Example 23 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project warn-report by saaavsaaa.

the class ElasticClient method get.

@Test
public void get() throws IOException {
    RestClientBuilder builder = RestClient.builder(new HttpHost("sl010a-analysisdb1", 9200, "https"), new HttpHost("sl010a-analysisdb2", 9200, "https"), new HttpHost("sl010a-analysisdb3", 9200, "https"));
    Header[] defaultHeaders = new Header[] { new BasicHeader("Authorization", "Basic YWRtaW46YWRtaW4=") };
    builder.setDefaultHeaders(defaultHeaders);
    RestClient restClient = builder.build();
    RestHighLevelClient client = new RestHighLevelClient(restClient);
    GetRequest getRequest = new GetRequest("test-index", "test-all", "26269");
    GetResponse getResponse = client.get(getRequest);
    System.out.println(getResponse.getSourceAsString());
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpHost(org.apache.http.HttpHost) GetRequest(org.elasticsearch.action.get.GetRequest) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) GetResponse(org.elasticsearch.action.get.GetResponse) BasicHeader(org.apache.http.message.BasicHeader) Test(org.junit.Test)

Example 24 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project ranger by apache.

the class ElasticSearchMgr method connect.

synchronized void connect() {
    if (client == null) {
        synchronized (ElasticSearchAuditDestination.class) {
            if (client == null) {
                String urls = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_URLS);
                String protocol = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_PROTOCOL, "http");
                user = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_USER, "");
                password = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_PWRD, "");
                int port = Integer.parseInt(PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_PORT));
                this.index = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_INDEX, "ranger_audits");
                String parameterString = String.format(Locale.ROOT, "User:%s, %s://%s:%s/%s", user, protocol, urls, port, index);
                logger.info("Initializing ElasticSearch " + parameterString);
                if (urls != null) {
                    urls = urls.trim();
                }
                if (StringUtils.isBlank(urls) || "NONE".equalsIgnoreCase(urls.trim())) {
                    logger.info(String.format("Clearing URI config value: %s", urls));
                    urls = null;
                }
                try {
                    if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password) && password.contains("keytab") && new File(password).exists()) {
                        subject = CredentialsProviderUtil.login(user, password);
                    }
                    RestClientBuilder restClientBuilder = getRestClientBuilder(urls, protocol, user, password, port);
                    client = new RestHighLevelClient(restClientBuilder);
                } catch (Throwable t) {
                    logger.error("Can't connect to ElasticSearch: " + parameterString, t);
                }
            }
        }
    }
}
Also used : ElasticSearchAuditDestination(org.apache.ranger.audit.destination.ElasticSearchAuditDestination) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) File(java.io.File)

Example 25 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project ranger by apache.

the class ElasticSearchMgr method getRestClientBuilder.

public static RestClientBuilder getRestClientBuilder(String urls, String protocol, String user, String password, int port) {
    RestClientBuilder restClientBuilder = RestClient.builder(MiscUtil.toArray(urls, ",").stream().map(x -> new HttpHost(x, port, protocol)).<HttpHost>toArray(i -> new HttpHost[i]));
    if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password) && !user.equalsIgnoreCase("NONE") && !password.equalsIgnoreCase("NONE")) {
        if (password.contains("keytab") && new File(password).exists()) {
            final KerberosCredentialsProvider credentialsProvider = CredentialsProviderUtil.getKerberosCredentials(user, password);
            Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()).build();
            restClientBuilder.setHttpClientConfigCallback(clientBuilder -> {
                clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
                return clientBuilder;
            });
        } else {
            final CredentialsProvider credentialsProvider = CredentialsProviderUtil.getBasicCredentials(user, password);
            restClientBuilder.setHttpClientConfigCallback(clientBuilder -> clientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        }
    } else {
        logger.error("ElasticSearch Credentials not provided!!");
        final CredentialsProvider credentialsProvider = null;
        restClientBuilder.setHttpClientConfigCallback(clientBuilder -> clientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    }
    return restClientBuilder;
}
Also used : RestClient(org.elasticsearch.client.RestClient) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) StringUtils(org.apache.commons.lang.StringUtils) RegistryBuilder(org.apache.http.config.RegistryBuilder) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) ElasticSearchAuditDestination(org.apache.ranger.audit.destination.ElasticSearchAuditDestination) AuthSchemes(org.apache.http.client.config.AuthSchemes) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) Locale(java.util.Locale) Lookup(org.apache.http.config.Lookup) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) PrivilegedActionException(java.security.PrivilegedActionException) Logger(org.slf4j.Logger) KerberosTicket(javax.security.auth.kerberos.KerberosTicket) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Subject(javax.security.auth.Subject) File(java.io.File) Component(org.springframework.stereotype.Component) MiscUtil(org.apache.ranger.audit.provider.MiscUtil) PropertiesUtil(org.apache.ranger.common.PropertiesUtil) CredentialsProviderUtil(org.apache.ranger.authorization.credutils.CredentialsProviderUtil) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpHost(org.apache.http.HttpHost) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) File(java.io.File)

Aggregations

RestClientBuilder (org.elasticsearch.client.RestClientBuilder)34 HttpHost (org.apache.http.HttpHost)27 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)23 RestClient (org.elasticsearch.client.RestClient)15 IOException (java.io.IOException)11 CredentialsProvider (org.apache.http.client.CredentialsProvider)11 ArrayList (java.util.ArrayList)7 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 File (java.io.File)6 List (java.util.List)6 Locale (java.util.Locale)6 KeyStore (java.security.KeyStore)5 Map (java.util.Map)5 StringUtils (org.apache.commons.lang.StringUtils)5 AuthSchemeProvider (org.apache.http.auth.AuthSchemeProvider)5 AuthSchemes (org.apache.http.client.config.AuthSchemes)5 Lookup (org.apache.http.config.Lookup)5 RegistryBuilder (org.apache.http.config.RegistryBuilder)5 SPNegoSchemeFactory (org.apache.http.impl.auth.SPNegoSchemeFactory)5