ElasticSearch Java API-Singleton mode

Source: Internet
Author: User

Single-Case mode
private static Settings getsettinginstance () {
if (settings==null) {
Synchronized (Settings.class) {
if (settings==null) {
Settings = Immutablesettings.settingsbuilder ()
Client.transport.sniff=true
The client sniffs the entire cluster state, automatically adds the IP addresses of the other machines in the cluster to the client, and automatically discovers the newly added machines to the cluster.
. put ("Client.transport.sniff", true). Put ("client", True)//Connect only as a client
. put ("data", false). Put ("Cluster.name", clustername)//cluster name
. build ();
}
}
}
return settings;
}
Single-Case mode
private static transportclient client;
private static Transportclient getistance () {
if (client = = null) {
Synchronizing code blocks (when an object is not initialized, use a synchronous block of code to guarantee that the object will not be created again after the first creation of the multi-threaded access)
Synchronized (Transportclient.class) {
if (client = = null) {
Client = new Transportclient (Getsettinginstance ()). Addtransportaddress (new inetsockettransportaddress (hostname, Integer.parseint (PORT1)))//TCP
Connection Address
. addtransportaddress (new Inetsockettransportaddress (hostname, integer.parseint (PORT2)));
}
}
}
return client;
}
/**
* CREATE INDEX Write Elasticsearch
*
* @param jsonlist
* Jsonlist data to create the index
*/
public static void CreateIndex (List<jsonobject> jsonlist) {
Searchrequestbuilder = Getistance (). Preparesearch (index);
try {
Create an index
for (int i = 0; i < jsonlist.size (); i++) {
Indexresponse indexresponse = Client.prepareindex (index, type, Jsonlist.get (i). getString ("id")). SetSource ( Jsonlist.get (i). toString ())
. Execute (). Actionget ();
if (indexresponse.iscreated ()) {
SYSTEM.OUT.PRINTLN ("created successfully!");
} else {
System.out.println ("Create failed! ");
}
}
}
catch (Exception e) {
E.printstacktrace ();
}

}

ElasticSearch Java API-Singleton mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.