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