Elasticsearch API for JAVA Learning notes __java

Source: Internet
Author: User
Tags time interval

This Learning note comes from the ES official website's Guidance document: ES JAVA API Client client is a class that enables various operations on the ES cluster: Index, GET, Delete, Search , as well as management tasks for ES clusters. Client constructs need to be based on the transportclient transportclient transportclient can remotely connect ES clusters through a transmission module, but it is not really connected to the cluster, Just get one or more of the initial transport addresses for the cluster and actually connect to the ES cluster each time the action is requested. Settings The settings class mainly configures some property parameters before starting the client, mainly configuring the cluster name Cluster.name, and other parameters: Client.transport.sniff   Whether to add sniffer functionality for transmission client
Client.transport.ignore_cluster_name set to True, ignore cluster name of connection node verify
client.transport.ping_timeout time limit when setting ping node, default 5s
Client.transport.nodes_sampler_interval Set sample/ping nodes listed time interval, default 5s

		Setting property parameters through the settings class settings Settings
		= Settings.settingsbuilder (). Put ("Cluster.name", "Index-name"). Build ();
		
		Start Client client
		client = Transportclient.builder (). settings (Settings). Build ()
				. Addtransportaddress (New Inetsockettransportaddress (Inetaddress.getbyname ("192.168.xxx.xxx"), 9300));
		
		If you do not need to set the parameters, directly below
		/*client Client = Transportclient.builder (). Build ().
				Addtransportaddress (New Inetsockettransportaddress (Inetaddress.getbyname ("192.168.xxx.xxx"), 9300));
		
		Close clinet
		client.close ();
Document APIMainly divided into the following categories: Index API, get API, Delete API, Update APIs, Multi get API, Bulk API modification and deletion of ESThe index API can index a typical JSON document to a specified index, and it can be retrieved. Generate JSONThere are several ways in which JSON can be produced: Manually splicing a JSON string using the map using a third-party library, such as Jackson using the built-in Xcontentfactory.jsonbuilder () each type will be converted to byte[], So if the object is already in this form and can be used directly, Jsonbuilder is a highly optimized JSON generator that constructs byte[] to explain four methods through the following code: Index-api, Get-api, Delete-api, Update-api
/** * Es-api Method Learning: * 1.prepareIndex method: Index data to Elasticsearch * 2.prepareGet method: Get Information * 3.prepareDelete method: Delete Information * 4.UPDA Te method: Update information * 4.1 Upsert: When using the Update method: * A: For the document does not exist, the index data operation, update invalid; * B: If the document exists, the index data operation is invalid. Update valid; */public static void main (string[] args) throws IOException, Interruptedexception, executionexception {//  
		  
		Setting property parameters through the settings class settings settings = Settings.settingsbuilder (). Put ("Cluster.name", "myApp"). Build ();
		Start Client Client client = NULL;  
			        try {client = Transportclient.builder (). settings (Settings). Build ().
		Addtransportaddress (New Inetsockettransportaddress (Inetaddress.getbyname ("101.200.124.27"), 9300);
		catch (Unknownhostexception e) {e.printstacktrace ();
		}//Perform operation SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd"); Xcontentbuilder Jsonbuilder = Xcontentfactory.jsonbuilder (). StartObject (). Field ("User", "Yuchen"). Field ("int Erest "," Reading book "). Field (" INsert_time ", Df.format (New Date ()). EndObject (); 1.prepareIndex method: Index data to elasticsearch indexresponse response = Client.prepareindex ("Index-test", "Weibo", "4").
		
		SetSource (Jsonbuilder). get ();
		String _index = Response.getindex ();
		String _type = Response.gettype ();
		String _id = Response.getid ();
		Long _version = Response.getversion ();
		Boolean created = Response.iscreated ();
		
		System.out.println (_index+ "+_type+" "+_id+" "+_version+" "+created");
		2.prepareGet method: Obtain information GetResponse GetResponse = Client.prepareget ("Index-test", "Weibo", "1"). get ();
		
		System.out.println (Getresponse.getsourceasstring ());
		3.prepareDelete Method: Delete Information Deleteresponse deleteresponse = Client.preparedelete ("Index-test", "Weibo", "4"). get ();
		
		System.out.println (Deleteresponse.isfound ());
		4.update method: Update information Updaterequest updaterequest = new Updaterequest ();
		Updaterequest.index ("Index-test");
		Updaterequest.type ("Weibo");
		Updaterequest.id ("1"); Updaterequest.doc (xcontentfactOry.jsonbuilder (). StartObject (). Field ("Interest", "music"). EndObject ());
		Updateresponse updateresponse = client.update (updaterequest). get ();
		System.out.println (updateresponse.iscreated ()); Update method: You can add a new field to an existing document Updateresponse UpdateResponse2 = client.prepareupdate ("Index-test", "Weibo", "1"). Setdoc (
		Xcontentfactory.jsonbuilder (). StartObject (). Field ("Interest2", "reading"). EndObject ()).
		System.out.println (updateresponse2.iscreated ()); 4.1 Upsert: When using the Update method, a: When the document does not exist, the operation of the index data, update is invalid;//b: If the document exists, then the index data operation is invalid, update is valid;//first build a index
		Request indexrequest indexrequest = new Indexrequest ("Index-test", "Weibo", "14"); Indexrequest.source (Xcontentfactory.jsonbuilder (). StartObject (). Field ("User", "Yuchen2"). Field ("Interest", "E
		Ating "). Field (" Insert_time ", Df.format (New Date ()). EndObject ());
Then constructs a updaterequest and uses Indexrequest association updaterequest UPDATEREQUEST3 = new Updaterequest ("Index-test", "Weibo", "14");		Updaterequest3.doc (Xcontentfactory.jsonbuilder (). StartObject (). Field ("Interest2", "Love"). EndObject ()
		). Upsert (Indexrequest);
		
		Client.update (UPDATEREQUEST3). get ();  
		if (client!= null) {client.close (); }
	}
Bulk OperationsMulti get APIs and Bulk APIs can be used for bulk additions and deletions using the Multi getting API for bulk access:
		1. Muti-get Api
		//can specify a single ID, also specify a id-list under Index,type, or you can specify a different index/type
		multigetresponse = Client.preparemultiget ()
				. Add ("Index-test", "Weibo", "1")//Specify a single ID
				. Add ("Index-test", "Weibo", "11", "13", "" 14 ")//Specifies a id-list
				. Add (" Index-other "," News "," 1 "," 3 "). get ();//Specify a different index/type for
		(multigetitemresponse Item:multigetresponse) {
			GetResponse response = Item.getresponse ();
			System.out.println (Response.getsourceasstring ());
		}
Bulk API Bulk Increase:
//2.bulk Api: can be batch index and bulk Delete operations//2.1 Batch increase Bulkrequestbuilder bulkrequest = Client.preparebulk ();
	                Bulkrequest.add (Client.prepareindex ("Index-test", "Weibo", "a"). SetSource (Xcontentfactory.jsonbuilder () . StartObject (). Field ("User", "Yuchen20"). Field ("Postdate", New Date
          ()). Field ("Message", "trying out Elasticsearch"). EndObject ())
		);
		                Bulkrequest.add ("Index-test", "Weibo", "Client.prepareindex"). SetSource (Xcontentfactory.jsonbuilder () . StartObject (). Field ("User", "yuchen21"). Field ("Postdate", New D
		              Ate ()). Field ("Message", "trying out Elasticsearch"). EndObject ()
		
		)
	          );
		Bulkresponse bulkresponse = Bulkrequest.get (); if (Bulkresponse.hasfailures ()) {//...} 
Bulk API Bulk Deletion:
		2.2 Mass deletion
		bulkrequestbuilder bulkrequest = Client.preparebulk ();
		Bulkrequest.add (Client.preparedelete ("Index-test", "Weibo", ")"
          );
		Bulkrequest.add (Client.preparedelete ("Index-test", "Weibo", ")"
	          );
		
		Bulkresponse bulkresponse = Bulkrequest.get ();
		if (Bulkresponse.hasfailures ()) {
			System.out.println ("Bulk Error:" +bulkresponse.buildfailuremessage ());
		}
Bulk API Batch Update
		2.3 Batch update
		Bulkrequestbuilder bulkrequest = Client.preparebulk ();
		Bulkrequest.add (Client.prepareupdate ("Index-test", "Weibo", "one"). Setdoc (Xcontentfactory
				. Jsonbuilder (). StartObject ()
				. Field ("Country", "a")/new Add fields
				. EndObject ()
				)
          );
		Bulkrequest.add ("Index-test", "Weibo", "Client.prepareupdate"). Setdoc (xcontentfactory. Jsonbuilder
				(). StartObject ()
				. Field ("User", "yuchen13")/update fields
				. EndObject ()
				)
	          );
		
		Bulkresponse bulkresponse = Bulkrequest.get ();
		if (Bulkresponse.hasfailures ()) {
			System.out.println ("Bulk Error:" +bulkresponse.buildfailuremessage ());
		}
Bulkprocessor Set the properties of 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 ())); Bulkprocessor.awaitclose (5, timeunit.minutes);/Free Bulkprocessor resource System.out.println ("Load succeed!");

Default parameters: Sets Bulkactions to 1000 sets Bulksize to 5MB does not set Flushinterval sets Concurrentrequests to 1 sets BACKOFFP Olicy to the exponential backoff with 8 retries and a start delay of 50ms. The total is roughly 5.1 seconds.







Related Article

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.