# JAVA API and doco for Graph

**URL:** https://discuss.elastic.co/t/java-api-and-doco-for-graph/65926
**Category:** Elasticsearch
**Tags:** elastic-stack-graph
**Created:** [November 14, 2016, 2:37am UTC](https://discuss.elastic.co/t/java-api-and-doco-for-graph/65926 "2016-11-14T02:37:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![David\_she](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_she/32/48792_2.png) [@David\_she](https://discuss.elastic.co/u/David_she)
#### Post date: [November 14, 2016, 2:37am UTC](https://discuss.elastic.co/t/java-api-and-doco-for-graph/65926/1 "2016-11-14T02:37:36Z")

</div>

Hi,  
I have trawled through the elastic site and google looking for  
the Java API to use for ElasticSearch Graph. Also any sample code or doco would be ideal.  
Appreciated

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [November 14, 2016, 10:36am UTC](https://discuss.elastic.co/t/java-api-and-doco-for-graph/65926/2 "2016-11-14T10:36:57Z")

</div>

Hi David,

Installing dependencies is as per the Watcher docs [1].  
In addition (assuming 5.0) you will need the x-pack-5.0.0.jar on your classpath which is found in your plugins directory once you have installed x-pack[2].  
Here is the "Hello World" example of Graph client code:

```
import java.net.InetAddress;
import java.util.Collection;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.xpack.XPackClient;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.elasticsearch.xpack.graph.action.GraphExploreAction;
import org.elasticsearch.xpack.graph.action.GraphExploreRequestBuilder;

import org.elasticsearch.xpack.graph.action.GraphExploreResponse;
import org.elasticsearch.xpack.graph.action.Hop;
import org.elasticsearch.xpack.graph.action.Vertex;

public class GraphDemoClient {

	public static void main(String[] args) throws Exception {

		TransportClient client = new PreBuiltXPackTransportClient(Settings.EMPTY)
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

		XPackClient xpackClient = new XPackClient(client);

		int requiredNumberOfSightings = 1;

		GraphExploreRequestBuilder grb = new GraphExploreRequestBuilder(client, GraphExploreAction.INSTANCE).setIndices("meetuprsvps");
		Hop hop1 = grb.createNextHop(QueryBuilders.termQuery("_all", "elasticsearch"));
		// elasticsearch meetup attendees
		hop1.addVertexRequest("member_id").size(10).minDocCount(requiredNumberOfSightings);
		// groups attended by elastic attendees
		grb.createNextHop(null).addVertexRequest("group_id").size(10).minDocCount(requiredNumberOfSightings);

		GraphExploreResponse response = grb.get();
		Collection<Vertex> vertices = response.getVertices();

		System.out.println("==Members===");
		for (Vertex vertex : vertices) {
			if (vertex.getField().equals("member_id")) {
				System.out.println(vertex.getTerm());
			}
		}

		System.out.println("==Groups===");
		for (Vertex vertex : vertices) {
			if (vertex.getField().equals("group_id")) {
				System.out.println(vertex.getTerm());
			}
		}

		client.close();

	}

}

```

To run this I found I also needed runtime dependencies of log4j and the netty artefacts in the "modules" directory of my elasticsearch installation.

Hope this helps  
Mark

[1] [https://www.elastic.co/guide/en/x-pack/current/api-java.html](https://www.elastic.co/guide/en/x-pack/current/api-java.html)  
[2] [https://www.elastic.co/guide/en/x-pack/current/installing-xpack.html](https://www.elastic.co/guide/en/x-pack/current/installing-xpack.html)

---

<div class="post-metadata">

### Author: ![David\_she](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_she/32/48792_2.png) [@David\_she](https://discuss.elastic.co/u/David_she)
#### Post date: [November 14, 2016, 7:36pm UTC](https://discuss.elastic.co/t/java-api-and-doco-for-graph/65926/3 "2016-11-14T19:36:28Z")

</div>

Cheers

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [December 12, 2016, 7:36pm UTC](https://discuss.elastic.co/t/java-api-and-doco-for-graph/65926/4 "2016-12-12T19:36:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
