// create the HttpURLConnection
url = new URL(theurl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", contentType);
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
// read the output from the server
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
result = stringBuilder.toString();
Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.
Or use markdown style like:
```
CODE
```
This is the icon to use if you are not using markdown format:
There's a live preview panel for exactly this reasons.
Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.
Any reason for not using the low level rest client?
I have learned a lot from the documentation as you suggested. I was successful in setting up a Maven environment for Elasticsearch. I tried both the low-level and high-level poms, and successfully created a .jar file to use in my Tomcat application. I can call methods in this jar file to connect to Elastic, but once I get to the RestClient.builder() in either the low or high level client calls, the code simply hangs up.
package com.mycompany.elasticsearch;
import org.apache.log4j.Logger;
import org.apache.http.HttpHost;
import org.elasticsearch.client.*;
import org.json.simple.JSONObject;
public class N2NESElastic {
private Logger logger = null;
public N2NESElastic() {
logger = Logger.getLogger(com.mycompany.elasticsearch.N2NESElastic.class);
logger.info("[SERVER] N2NESElastic(). Inside the constructor...");
}
private static final String HOST = "localhost"; //"127.0.0.1";
private static final int PORT_ONE = 9200;
private static final int PORT_TWO = 9201;
private static final String SCHEME = "http";
private RestClient restClient = null;
private RestHighLevelClient client = null;
@SuppressWarnings("unchecked")
public synchronized JSONObject processRequest(JSONObject jsonReqAsObject, String command) {
logger.info("[N2NESElastic] Command: " + command);
JSONObject json = null;
String message = "command: " + command;
// - - - - - - - - - - - - - - - - - - - -
// O P E N
// - - - - - - - - - - - - - - - - - - - -
logger.info("Calling makeConnection()...");
this.makeConnection();
logger.info("Connection established...");
// - - - - - - - - - - - - - - - - - - - -
// C L O S E
// - - - - - - - - - - - - - - - - - - - -
try {
logger.info("Calling closeConnection()...");
this.closeConnection();
logger.info("Connection closed...");
}
catch (Exception e) {
logger.error("**xERROR> " + e.toString());
}
json.put("message", message);
return json;
}
private synchronized void makeConnection() {
logger.info("Making connection...");
logger.info("Creating hostPort1...");
HttpHost hostPort1 = new HttpHost("localhost", 9200, "http");
logger.info("Creating hostPort2...");
HttpHost hostPort2 = new HttpHost("localhost", 9201, "http");
//logger.info("Calling restClient builder...");
//RestClient.builder(hostPort1, hostPort2).build(); // LOW LEVEL: HANGS
logger.info("Calling high-level-rest builder..."); // HIGH LEVEL: ALSO HANGS
client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
logger.info("Done building..."); // NEVER GET TO THIS LINE
}
private synchronized void closeConnection() {
try {
logger.info("Closing connection...");
restClient.close();
}
catch (Exception e) {
logger.error("**ERROR> " + e.toString());
}
}
}
Any help you can offer will be appreciated.
In my Eclipse project I have the following external .jar files from Elastic:
elasticsearch-7.1.0.jar
elasticsearch-core-7.1.0.jar
elasticsearch-rest-client-7.1.0.jar
elasticsearch-rest-high-level-client-7.1.0.jar
I never used Maven before, but now I've learned, and I have a new java project in Eclipse that I converted to a Maven project. All dependencies in pom.xml are taken care of just as they should be. My java classes look great.
After all that, however, the code still hangs at the exact same point as before.
Here is what I do:
I create a .jar file from my elasticsearch Maven project. I then add this .jar file to my Tomcat application.
My Tomcat application then instantiates the my elasticsearch class, an I call RestClient and RestClient builder, just as in the documentation:
restClient = RestClient.builder(
new HttpHost(HOST, PORT_ONE, SCHEME),
new HttpHost(HOST, PORT_TWO, SCHEME)).build();
I know that the HttpHost calls happen correctly, but the call to RestClient.builder() hangs.
MY QUESTION: do you think this is some timing (synchronous / asynchronous) issue between Tomcat (port 8080) and elastic search?
Really, I need your HELP!! Everything is in place, all the dependencies are good, the code looks like it will work, but it hangs, just like before.
Hmm. RestClient.builder() running from main() does not hang.
I'm putting my thinking hat on, but I would also like to know what your thoughts are. My first thought is that I'm missing elasticsearch dependencies in my Tomcat application, and I'll be checking on this today.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.