This repository shows basic connection between JavaAPI and Elasticsearch server.
- Java client
ElasticsearchConnectionCheckHTTP
that establishes a connection to an Elasticsearch cluster and checks if the connection is successful using HTTP.
- Variables:
- The code declares and initializes several variables:
- host: Represents the IP address or hostname of the Elasticsearch cluster.
- port: Represents the port number on which the Elasticsearch cluster is running.
- username: Represents the username for authentication with the Elasticsearch cluster.
- password: Represents the password for authentication with the Elasticsearch cluster.
- Building the Elasticsearch Client:
- The code creates a
RestClientBuilder
object to configure the Elasticsearch client. - It uses the
RestClient.builder()
method and specifies the host, port, and "http" scheme to build an HTTP host. - The
setHttpClientConfigCallback
method is used to configure the HTTP client with authentication credentials. - It creates a
CredentialsProvider
object, sets the AuthScope to ANY, and provides theUsernamePasswordCredentials
. - The httpClientBuilder is then configured with the setDefaultCredentialsProvider method.
- Finally, the RestHighLevelClient is created using the configured RestClientBuilder.
- The code creates a
- Checking the Connection:
- The code attempts to ping the Elasticsearch cluster using the
client.ping
method and passingRequestOptions.DEFAULT
- If the ping is successful, it prints "Connection is SUCCESSFUL."
- If an IOException occurs during the ping, it prints "Connection is FAILED" along with the exception message.
- The code attempts to ping the Elasticsearch cluster using the
- Closing the Client:
- The
client.close()
method is called in a finally block to ensure the client resources are released.
- The
FileReadabilityCheck
checks the readability of a file.
-
Import Statement:
- The code imports the
java.io.File
class, which represents a file on the file system.
- The code imports the
-
File Path:
- The code declares a variable named
filePath
and assigns it a file path string.
- The code declares a variable named
-
Creating a File Object:
- The code creates a
File object
using thefilePath
string. - The File object represents the file specified by the file path.
- The code creates a
-
Checking File Readability:
- The code uses an if statement to check the existence and readability of the file.
- The
exists()
method checks if the file exists. - The
canRead()
method checks if the file is readable. - If both conditions are true, it prints "File is readable".
- If either condition is false then it prints "File is not readable".
-
Demo run screenshot:
- Elasticsearch Java API Client: The Java API client provides strongly typed requests and responses for all Elasticsearch APIs.
- Elasticsearch warning - ignore_throttled parameter is deprecated - Stackoverflow - Elasticsearch warning - ignore_throttled parameter is deprecated issued which has answer
- Do not send default ignore_throttled parameter since it is deprecated - github discussion board about the particular issue
- How to connect to elasticsearch server using rest client with IP address - Stackoverflow: The cluster coordination algorithm has changed in 7.0 234 and in order to be safe it requires some specific configurations. We relax that requirement (that is, we run in a less-safe mode) when you bind to localhost only, but if/when you change
network.host
we enforce that your configure the cluster safely. - Discovery configuration is required in production - production deployments of Elasticsearch now require at least one of the following settings to be specified in the
elasticsearch.yml
configuration file:- discovery.seed_hosts
- discovery.seed_providers
- cluster.initial_master_nodes
- discovery.zen.ping.unicast.hosts
- discovery.zen.hosts_provider