First on the code, follow-up analysis.
The jar package is directly under the ES package, implemented by the following functions:
1. Create an index
2. Query specific Documents
Import Java.util.Date;
Import Java.util.HashMap;
Import Java.util.Map;
Import Org.elasticsearch.action.get.GetResponse;
Import Org.elasticsearch.action.index.IndexResponse;
Import org.elasticsearch.client.Client;
Import org.elasticsearch.client.transport.TransportClient;
Import org.elasticsearch.common.settings.ImmutableSettings;
Import org.elasticsearch.common.settings.Settings;
Import org.elasticsearch.common.transport.InetSocketTransportAddress;
public class Esapitest {private Client client; Public Esapitest () {///first Configure the ES cluster name to the Settings object settings settings = Immutablesettings.settingsbuilder (). Put ("
Cluster.name "," Inde-test "). Build ();
Secondly, the Clinet object is created based on the Settings object, and the connection 9300 this.client = new Transportclient (settings).
Addtransportaddress (New Inetsockettransportaddress ("101.200.xxx.xxx", 9300));
public static void Main (string[] args) {esapitest esapitest = new Esapitest ();
Esapitest.createindex ();
Esapitest.getindex (); }//CREATE INDEX + type + document public void CreateindeX () {//Generate index, use MAP to store map<string,object> json = new hashmap<> ();
Json.put ("User", "cas");
Json.put ("Postdate", New Date ());
Json.put ("message", "Try Es Java API");
Indexresponse response = This.client.prepareIndex ("Es-api", "Test", "2"). SetSource (JSON). Execute (). Actionget ();
System.out.println (response.iscreated ());
Success is true; false; If the document is already in the inside, it returns false Client.close () once it is created. ////According to index + type + document get information public void GetIndex () {GetResponse GetResponse = client.prepareget ("Es-api", "Test", "1"). exe
Cute (). Actionget ();
map<string,object> res = Getresponse.getsource ();
If the query is not available, it returns an empty pointer if (res = = null) {System.out.println ("empty");
}else{System.out.println (res.tostring ());
{message=try Es Java API, postdate=2016-08-30t01:56:32.131z, User=cas}} client.close ();
}
}
Bulkprocessor set properties for a bulk request
Bulkprocessor bulkprocessor bulkprocessor = bulkprocessor.builder (client, new Bulkprocessor.listener () {@Overrid e public void Beforebulk (long arg0, bulkrequest arg1) {//do things before mass execution System.out.println ("Bulk API action Startin
G. ... "); @Override public void Afterbulk (long arg0, bulkrequest arg1, Throwable arg2) {System.out.println ("exception:b
UKL API Action Ending: "+arg2.getmessage ());
@Override public void Afterbulk (long arg0, bulkrequest arg1, Bulkresponse arg2) {//after normal execution ...
SYSTEM.OUT.PRINTLN ("Normal:bukl API action ending ..."); })//Set up a variety of conditions to limit bulk operations to any one of the constraints that trigger the request. Setbulkactions (1000)/Set the number of actions that a batch operation executes at a single amount, submitting the//.setbulksize in batches based on the number of requests ( New Bytesizevalue (1,BYTESIZEUNIT.KB)//sets the maximum allowable size of the bulk commit request//.setflushinterval (Timevalue.timevaluemillis (100))// Set the number of concurrent requests (1)//, based on the time period batch commit request//.setconcurrentrequests (+)//Set the compensation measure when the request failed, repeat request 3 times//.setbackoffpolicy ( Backoffpolicy.exponentialbackoff (Timevalue.timevaluemillis (100), 3)). build (); for (int i =0;i<100000;i++) {Bulkprocessor.add (New indexrequest ("Index-test", "Weibo2", "" +i). Source (xcontentfact Ory. Jsonbuilder (). StartObject (). Field ("Name", "Yuchen" +i). Field ("Interest", "Love" +i). Endobjec
T ())); } System.out.println ("Load succeed!");