In some business scenarios multiple entity instances need to be handled together as a single logical unit of work.This is where $batch processing come into picture.A $batch request means multiple operations bundled as a single request.Batch processing simple batches several independent OData service call to a single big OData call.
Important thing to remember is 'Do not use batch processing if individual service calls depend on each other(Result of another OData service call)'.Batch calls can combine read and write requests.For write requests we need to define related change-sets in the request.
OData batch request requires setting the Content-Type request header according to the boundary defined.We will see in the tutorial.
We will be batching multiple create operations on our FlightSet.
Pre-requisites
Steps
Note:A sample read request will look like this.See batch boundary is different.Dont forget it when adding header
Important thing to remember is 'Do not use batch processing if individual service calls depend on each other(Result of another OData service call)'.Batch calls can combine read and write requests.For write requests we need to define related change-sets in the request.
OData batch request requires setting the Content-Type request header according to the boundary defined.We will see in the tutorial.
We will be batching multiple create operations on our FlightSet.
Pre-requisites
Steps
- For batching read operations we dont have to do anything in gateway service side.Since we are going to batch write operations we need to redefine CHANGESET_BEGIN and CHANGESET_END methods of our **DPC_EXT class.Either we can implement our own logic or just comment the code and activate it.Right click on both methods and click Redefine.
- Comment the code inside both methods and activate both methods.
- Prepare the batch request.See sample batch request for the FlightSet entity here.Spaces are important in request
- Open SAP Gateway Client().Give the service url and select POST method(Create operation).Url is /sap/opu/odata/SAP/ZTEST_ODATA_SRV_01/$batch
- Add http header Content-Type and give multipart/mixed; boundary=batch as header value.You can see batch is my boundary defined in my request in step 3.If yours is different change it accordingly.(You can add header by selecting '+' button seen in the lefthand side).
- Paste the batch request prepared in step 3 in request section and press execute button.I have pasted screenshots before and after batch processing.
- Go to se11 and check our scarr table for new entries.Batch process success😌
--batch Content-Type: multipart/mixed; boundary=changeset --changeset Content-Type: application/http Content-Transfer-Encoding: binary POST FlightSet HTTP/1.1 Content-Type: application/atom+xml Content-Length: 588 <?xml version="1.0" encoding="utf-8" standalone="yes"?> <atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <atom:content type="application/xml"> <m:properties> <d:Carrid>AA</d:Carrid> <d:Carrname>American Airlines</d:Carrname> <d:Currcode>USD</d:Currcode> <d:Url>http://americanairlines.com</d:Url> </m:properties> </atom:content> </atom:entry> --changeset Content-Type: application/http Content-Transfer-Encoding: binary POST FlightSet HTTP/1.1 Content-Type: application/atom+xml Content-Length: 588 <?xml version="1.0" encoding="utf-8" standalone="yes"?> <atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <atom:content type="application/xml"> <m:properties> <d:Carrid>UA</d:Carrid> <d:Carrname>United Airlines</d:Carrname> <d:Currcode>USD</d:Currcode> <d:Url>http://unitedairlines.com</d:Url> </m:properties> </atom:content> </atom:entry> --changeset-- --batch--
Note:A sample read request will look like this.See batch boundary is different.Dont forget it when adding header
--batch_zmybatch Content-Type: application/http Content-Transfer-Encoding: binary GET ProductCollection('HT-1000') HTTP/1.1 --batch_zmybatch Content-Type: application/http Content-Transfer-Encoding: binary GET ProductCollection('HT-1001') HTTP/1.1 --batch_zmybatch--
If you enjoyed this post, Please Share!!
Thanks for sharing this blog...
ReplyDeletePlease explain when to use(redefine) Update_entity and Changesetend-changesetbegin methods.
ReplyDeleteUpdate_entity is to use when you have to change a single entry. Change set is to use when you need multiple operations in single call like two update operation, update-read operation,multiple delete operation etc.
DeleteGreat blog I enjoyed reading it
ReplyDeleteThhis was a lovely blog post
ReplyDelete