I am trying to get started with the Java API, using the excellent tutorial
found here:
http://www.slideshare.net/dadoonet/hands-on-lab-elasticsearch
But I am still having a lot of trouble.
Below is a sample of code that I have written:
package ca.nrc.ElasticSearch;
import org.codehaus.jackson.map.ObjectMapper;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.node.NodeBuilder;
public class ElasticSearchRunner {
static ObjectMapper mapper;
static Client client;
static String indexName = "meal5";
static String typeName = "beer";
static long startTimeMSecs;
public static void main(String[] args) throws Exception {
startTimeMSecs = System.currentTimeMillis();
mapper = new ObjectMapper(); // create once, reuse
echo("Creating the ElasticSearch client...");
client = NodeBuilder.nodeBuilder().node().client(); // Does this create a
brand new cluster?
// client =
NodeBuilder.nodeBuilder().clusterName("handson").client(true).node().client();
// Joins existing cluster called "handson"
echo("DONE creating the ElasticSearch client... Elapsed time =
"+elapsedSecs()+" secs.");
echo("Creating a beer object...");
Beer beer = new Beer("Heineken", Colour.PALE, 0.33, 3);
String jsonString = mapper.writeValueAsString(beer);
echo("DONE Creating a beer object...");
echo("Indexing the beer object...");
IndexResponse ir = null;
ir = client.prepareIndex(indexName, typeName).setSource(jsonString)
.execute().actionGet();
echo("DONE Indexing the beer object...");
echo("Retrieving the beer object...");
GetResponse gr = null;
gr = client.prepareGet(indexName, typeName, ir.getId()).execute()
.actionGet();
echo("DONE Retrieving the beer object...");
}
public static float elapsedSecs() {
float elapsed = (System.currentTimeMillis() - startTimeMSecs)/1000;
return elapsed;
}
public static void echo(String mess) {
mess = mess + " (Elapsed so far: "+elapsedSecs()+" seconds)";
System.out.println(mess);
}
}
It works, "sort of"...
If I use the first method for creating the client:
client = NodeBuilder.nodeBuilder().node().client();
Then it works fin the first time I run it. However:
*** ISSUE 1: If I try to inspect the meal index with Marvel, I don't find
it.
Also,
*** ISSUE 2: If I run the application a second time, I get the following
output:
Creating the ElasticSearch client... (Elapsed so far: 0.0 seconds)
log4j:WARN No appenders could be found for logger (org.elasticsearch.node).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for
more info.
DONE creating the ElasticSearch client... Elapsed time = 9.0 secs. (Elapsed
so far: 9.0 seconds)
Creating a beer object... (Elapsed so far: 9.0 seconds)
DONE Creating a beer object... (Elapsed so far: 9.0 seconds)
Indexing the beer object... (Elapsed so far: 9.0 seconds)
Exception in thread "main"
org.elasticsearch.action.UnavailableShardsException: [meal5][0] [2]
shardIt, [0] active : Timeout waiting for [1m], request: index
{[meal5][beer][B3F5ZEmSTruqdnlxhYviFg],
source[{"brand":"Heineken","colour":"PALE","size":0.33,"price":3.0}]}
at
org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.raiseTimeoutFailure(TransportShardReplicationOperationAction.java:526)
at
org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$3.onTimeout(TransportShardReplicationOperationAction.java:516)
at
org.elasticsearch.cluster.ClusterStateObserver$ObserverClusterStateListener.onTimeout(ClusterStateObserver.java:239)
at
org.elasticsearch.cluster.service.InternalClusterService$NotifyTimeout.run(InternalClusterService.java:494)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
For me to be able to run the application again, I have to change the
indexName variable to a different value (ex: "meal6").
Also:
*** ISSUE 3: If I try using the second method for creating a Client, then
it doesn't work. More specifically, if I use this method:
client =
NodeBuilder.nodeBuilder().clusterName("handson").client(true).node().client();
This yields:
Creating the ElasticSearch client... (Elapsed so far: 0.0 seconds)
log4j:WARN No appenders could be found for logger (org.elasticsearch.node).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for
more info.
DONE creating the ElasticSearch client... Elapsed time = 34.0 secs.
(Elapsed so far: 34.0 seconds)
Creating a beer object... (Elapsed so far: 34.0 seconds)
DONE Creating a beer object... (Elapsed so far: 34.0 seconds)
Indexing the beer object... (Elapsed so far: 34.0 seconds)
Exception in thread "main"
org.elasticsearch.discovery.MasterNotDiscoveredException: waited for [1m]
at
org.elasticsearch.action.support.master.TransportMasterNodeOperationAction$4.onTimeout(TransportMasterNodeOperationAction.java:170)
at
org.elasticsearch.cluster.ClusterStateObserver$ObserverClusterStateListener.onTimeout(ClusterStateObserver.java:239)
at
org.elasticsearch.cluster.service.InternalClusterService$NotifyTimeout.run(InternalClusterService.java:494)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Yet, the "handson" cluster does exist and I can see it in Marvel.
Finally, I am puzzled by the following:
*** ISSUE 4: It seems that it takes ES 8-9 secs to create a Client using
the first method, and >30 secs with the second method. That seems really
high. Is that normal?
Any help you can provide with either of those issues will be greatly
appreciated. Thx.
Alain
--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/ba7e38a6-fb11-4a4d-95b6-4b0f2c904061%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.