Skip to content

afa-farkhod/Elasticsearch-JavaAPI-connection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 

Repository files navigation

This repository shows basic connection between JavaAPI and Elasticsearch server.

  1. 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 the UsernamePasswordCredentials.
    • The httpClientBuilder is then configured with the setDefaultCredentialsProvider method.
    • Finally, the RestHighLevelClient is created using the configured RestClientBuilder.
  • Checking the Connection:
    • The code attempts to ping the Elasticsearch cluster using the client.ping method and passing RequestOptions.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.
  • Closing the Client:
    • The client.close() method is called in a finally block to ensure the client resources are released.
  1. 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.
  • File Path:

    • The code declares a variable named filePath and assigns it a file path string.
  • Creating a File Object:

    • The code creates a File object using the filePath string.
    • The File object represents the file specified by the file path.
  • 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:

Image

About

This repository shows basic connection between JavaAPI and Elasticsearch server.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages