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;
}
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);
}
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());
}
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);
}
}
}
}
}
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;
}
Aggregations