G'Day.
I'm trying to write a Java program that interacts with ElasticSearch and the provided documentation on the Java API has been less than helpful - see https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_maven_repository.html
This has proven to be far more trouble than it should have been, so a little feedback and thanks to the various sites I reviewed trying to work out how to get it going...
The set up is quite simple - a new Windows 10 box with NetBeans and Java freshly installed.
Followed the instructions with NetBeans and the first couple of attempts to set it up failed. It seems to assume quite a bit of knowledge about both Maven and NetBeans. As I was also using this as a chance to learn NetBeans, that knowledge was somewhat lacking on my part...
So what did work:
Create a new NetBeans project - A Maven project for a Java application (the jury's still out on whether a plain Java application is ok or it has to be an Enterprise Java application).
Edit the pom.xml and add some dependencies inside the defined project:
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.5.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.5.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>
As I get further in, there may be more dependencies that are required. I had hoped the blanket elasticsearch dependency would pull all the others in, but apparently not.
Build - it should go off and download get some stuff from Maven. This will pull in the standard Apache logger.
To create the log4j2.properties file, you need to use 'Source Packages' -> 'New' -> 'Folder' and tell it to make one called resources under src/main (default is src/main/java). Once you've done that you need to use 'Resources' -> 'New' -> 'Properties File' to create the actual file. So far the cut and paste example from the doco seems to work ok. If you get this wrong it complains about not being able to find a log4j2 configuration file - the trick is that the resources directory has to be in the class path - which is achieved by adding it as a source folder rather than simply creating it.
Now, some code. Create a new Java class and give it a main method. Should build but do nothing at this point.
I pasted the following in line by line (more or less - the try/catch has to go in first) and let NetBeans add dependencies as it saw fit:
try {
InetSocketTransportAddress cluster = new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300);
System.out.println("Address is :" + cluster);
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY);
System.out.println("Client is :" + client);
} catch (Exception e) {
e.printStackTrace();
Logger.getLogger(Phant1.class.getName()).log(Level.SEVERE, null, e);
}
...and it actually builds and runs. Which is more than my first 3 or 4 attempts at creating the project did. Phant1 in the above is my class name.