Skip to content

Commit

Permalink
A simple app to verify TransportClient access to an Elasticsearch
Browse files Browse the repository at this point in the history
cluster
  • Loading branch information
Jeff Tharp committed Mar 4, 2015
0 parents commit 0cbc530
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## elasticsearch-test-client

Simple application to verify TransportClient connectivity to an Elasticsearch cluster.
Uses Elasticsearch 1.4.3 by default, adjust pom.xml to specify other versions

### Usage

`java -jar esTestTransportClient.jar ClusterName host1:port,host2:port,host3:port`

### Example Output

```
Connecting to eaf1fe49e76ea1b2782ac45879ce5952...
Mar 03, 2015 10:38:53 PM org.elasticsearch.plugins.PluginsService <init>
INFO: [Cybelle] loaded [], sites []
Last status: GREEN
```

This shows that a connection was made and the cluster status was GREEN
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.objectrocket</groupId>
<artifactId>esTestClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Elasticsearch Test Client</name>
<description>A client to test native connectivity</description>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.4.3</version>
</dependency>
</dependencies>
</project>
60 changes: 60 additions & 0 deletions src/main/java/esTestTransportClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.client.transport.TransportClient;


public class esTestTransportClient {
private String cluster_name;
private String[] host_list;

public esTestTransportClient(String cname, String hlist) {
cluster_name = cname;
host_list = hlist.split(",", -1);
}

private void connect() {
System.out.println("Connecting to " + cluster_name + "...");
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", cluster_name).build();

TransportClient client = new TransportClient(settings);
for(String host: host_list) {
int port = 9300;

//Parse port from host string
String[] parsedHost = host.split(":", 2);
if(parsedHost.length == 2) {
host = parsedHost[0];
port = Integer.parseInt(parsedHost[1]);
}

client.addTransportAddress(new InetSocketTransportAddress(host, port));
}

final ClusterHealthStatus status = client.admin().cluster().prepareHealth().get().getStatus();
System.out.println("Last status: " + status.name());
client.close();
}

public static void main(String[] args) {
String cname;
String hlist;

if(args.length != 0) {
cname = args[0];
hlist = args[1];

try {
esTestTransportClient esclient = new esTestTransportClient(cname, hlist);
esclient.connect();
}
catch (Exception e) {
e.printStackTrace();
}

System.exit(0);
}
}
}

0 comments on commit 0cbc530

Please sign in to comment.