# Java code to Elasticsearch

**URL:** <https://discuss.elastic.co/t/java-code-to-elasticsearch/112162>\
**Category:** Elasticsearch\
**Created:** [December 18, 2017, 6:58am UTC](https://discuss.elastic.co/t/java-code-to-elasticsearch/112162 "2017-12-18T06:58:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prateek\_Kshtriya](https://avatars.discourse-cdn.com/v4/letter/p/c37758/32.png) [@Prateek\_Kshtriya](https://discuss.elastic.co/u/Prateek_Kshtriya)\
**Post date:** [December 18, 2017, 6:58am UTC](https://discuss.elastic.co/t/java-code-to-elasticsearch/112162/1 "2017-12-18T06:58:20Z")

</div>

Hi All,

Please help me with the java code to send the data to elasticsearch. I went through the post for same , found below code but not helping with problem statement-  
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;  
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;  
import org.elasticsearch.action.admin.cluster.health.ClusterIndexHealth;  
import org.elasticsearch.client.AdminClient;  
import org.elasticsearch.client.Client;  
import org.elasticsearch.client._;  
import org.elasticsearch._;  
import java.net.InetAddress;  
import java.net.UnknownHostException;  
import org.elasticsearch.action.get.GetResponse;  
import org.elasticsearch.client.transport.TransportClient;  
import org.elasticsearch.common.settings.Settings;  
import org.elasticsearch.common.transport.InetSocketTransportAddress;  
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class Test {  
public static void main(String[] argv) throws UnknownHostException{

```
        //create client !!!Make sure keep settings empty if your cluster is the 
        //same as the one you defined in your elasticsearch.yml file
        //Plus, port here（9300）must be different from your http port(9200)
	 Settings settings = Settings.Builder
			    .put("cluster.name", "ElasticSearchClusterName")
			    .build();
        TransportClient client = new PreBuiltTransportClient(settings)
        		.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        //get data
        GetResponse response = client.prepareGet("twitter", "tweet", "1").execute().actionGet();

       //output
        System.out.println(response.getSourceAsString());

        client.close(); 
        } }
```

---

<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:** [January 15, 2018, 6:58am UTC](https://discuss.elastic.co/t/java-code-to-elasticsearch/112162/2 "2018-01-15T06:58:28Z")

</div>

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