Skip to Content
Author's profile photo Arshad Ansary

Creating Odata services out of CDS views

Introduction

The purpose of this blog is to show how HANA Artifact CDS view can exposed as Odata service to consumer.


1 CDS (Core Data Services) Views

CDS(Core Data Service) views  is the newest kid in the block in HANA . It allows you to do much more compared to classical view

  • Push Code to Data  Base layer by allowing queries ,calculations and imperative statements at the DB layer(instead of application service layer)
  • Allows you to add associations with other business entities/tables
  • Annotations to semantically describe your data model

I am not going to get vociferous about CDS because there is enough documentation already available .

Please refer this ABAP on HANA open sap course for more details.

https://open.sap.com/courses/a4h1

For creating CDS views there are some prerequisites

  • Eclipse should be installed
  • ADT(Abap Development Tools) add on should be installed
  • SAP NetWeaver AS ABAP 7.4 SP05 or higher running on a SAP HANA database SPS6 should be available

The ABAP system in which I am trying is NW7.4 SP8 system

Once we are done with prerequisites , we will create a CDS view .

This CDS view will join tables SNWD_SO(Sales Order) and SNWD_BPA (Business Partner) and find out sum of invoices whose gross amount is greater than 10000000.


@AbapCatalog.sqlViewName: 'ZDDLS_CDS_AGGR'
@EndUserText.label: 'CDS view for aggregation'
define view Zars_Cds_Aggr as select from snwd_so as so
inner join snwd_bpa as bpa
on so.buyer_guid = bpa.node_key
{ key bpa.bp_id as BusinessPartner,
  bpa.company_name,
  sum( so.gross_amount ) as  Total_Gross_Amount,
  so.currency_code
}
group by
bpa.bp_id,
    bpa.company_name,
    so.currency_code
    having sum(so.gross_amount) > 100000000








Here you can see there are two views – The View ZDDLS_CDS_AGGR is DDLS(DDL source view) name which can be viewed in SE11 and this object is integrated with the CTS (Change and transportation system) .The other view ZARS_CDS_AGGR is the CDS view name

Now we will check the results of the CDS Views. We can do it in two ways

a Open Data preview of CDS view in Eclipse

Eclipse_dp.jpg

Now the results

Eclipse_dp_1.jpg

b You can see the result in classical se11 transaction also. But this time we should give the DDL SQL view name ZDDLS_CDS_AGGR

ZARS/wp-content/uploads/2015/04/se11_dp_687586.jpg

2 Data Modelling in Gateway Builder

As we are done with CDS view building , we will move to Gateway data modeling part . The good thing here is we don’t need to switch to GUI for Gateway builder transaction. We can run the SEGW transaction from eclipse itself as an embedded GUI. Just press CTRL+SHIFT+A and give SEGW 🙂 .

We Create  data model based on DDLS view ZDDLS_CDS_AGGR

GW_builder_Data_modeling_1.jpg

GW_builder_Data_modeling_2.jpg

Click on Next button

GW_builder_Data_modeling_3.jpg

select the properties we want in the entity( ignore the MANDT field)

GW_builder_Data_modeling_4.jpg

Here we can see the property names of the entity has been automatically adjusted (Camel case) and we need to mark the Business Partner as the key property of the entity

Press finish to complete the data model part

3 Service implementation

Now we are done with Data model , we look into the data provisioning part.

Here we give the data source name as CDS view name ZARS_CDS_AGGR

Right click on the entity set SalesAggregationSet under service implementation and select Map to Data source . The important point here is we are not mapping the GetEntitySet operation of the entityset but the entityset itself.

/wp-content/uploads/2015/04/service_impl_map_1_687969.jpg

We have to select type as Business Entity and use F4 help to select CDS view  ZARS_CDS_AGGR.

/wp-content/uploads/2015/04/service_impl_map_2_688088.jpg

We then do the mapping of Entity properties with the CDS view fields. Strangely propose mapping button seems to be not there


/wp-content/uploads/2015/04/service_impl_map_3_688115.jpg

Generate the model and do the service registration


4 Testing

Now we head to GW client for testing . We test the SalesAggregationSet.

/wp-content/uploads/2015/04/testing_1_688116.jpg



In the above case we have not implemented the query operation but we use the URL of the query operation to get the CDS view results .

One more thing that comes free is the read operation .No separate implementation of READ 🙂 (GET_ENTITY) required.


So if We put the below URL

/sap/opu/odata/SAP/ZARS_CDS_SIMPLE_SRV/SalesAggregationSet(‘100000014’) also, we get the results


/wp-content/uploads/2015/04/testing_2_688136.jpg



Conclusion

ABAP is not only ‘ABAP’ right now . For a complete End to End scenario , now we need to proficient across three layers (ABAP,Gateway,UI) which seems to be close to impossible .  But as  grey haired ‘Abaper’ , it is required that we at least keep up to date with  how ABAP is evolving itself for HANA DB and how its pushing the changes  in Gateway layer. The easiest solution will be to ignore all the changes happening around us and stick to the old classical style. But that will be a gap too big to bridge


Assigned Tags

      27 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Atanu Mallik
      Atanu Mallik

      Hello Arshad,

      good that you have shared this information. May be you can also share the component details/ prerequisites for this feature as this is will not be available by default.

      Atanu

      Author's profile photo Arshad Ansary
      Arshad Ansary
      Blog Post Author

      Thanks Atanu,

      I have mentioned the same in the prerequisites part

      Regards

      Arshad

      Author's profile photo Brian Zhu
      Brian Zhu

      hi Arshad,

      i do not have the Business Entity in the list of map to data source.

      i am using netweaver 740 with sp08

      /wp-content/uploads/2015/12/2015_12_25_9_51_29_858010.jpg

      Do you have any idea?

      regards,

      Yueqiang

      Author's profile photo Arshad Ansary
      Arshad Ansary
      Blog Post Author

      Hi Zhu,

      Are you are using the context menu of the Entity set and not of GetEntitySet

      Regards

      Arshad

      Author's profile photo Brian Zhu
      Brian Zhu

      Hi Arshad,

      thank you very mush. i found this under the context menu of the Entityset.

      and also noted that you have already pointed that out in your blog 🙂

      regards,

      Yueqiang

      Author's profile photo Former Member
      Former Member

      Hi Arshad,

      Nice blog and You opened doors for good exploration in ABAP. I like your comment updating knowledge on technology.

      Thanks,
      Suman

      Author's profile photo Arshad Ansary
      Arshad Ansary
      Blog Post Author

      Thanks Suman

      Author's profile photo Shakul Jugran
      Shakul Jugran

      Hello Arshad,

      I liked the way you explained it all in layman's terms. Also, the last paragraph made for some fun reading as well 🙂

      Regards,

      Shakul.

      Author's profile photo Arshad Ansary
      Arshad Ansary
      Blog Post Author

      Thanks Shakul

      Author's profile photo Syambabu Allu
      Syambabu Allu

      Hello Arshad,

      Sharing the detail with HANA CDS.

      Thanks,

      Syam

      Author's profile photo Former Member
      Former Member

      Hello Arshad,

      I have a strange problem, I can't perform Map to Data Source.

      Every time I try that the system just freezes and I get no error prompt. I have tried different approaches and nothing seems to work. Do you have any idea why?

      Thank you very much,

      Indra

      Author's profile photo Arshad Ansary
      Arshad Ansary
      Blog Post Author

      Hi Indra,

      I also faced this problem while doing it via Eclipse. Can you try logging in via R/3 Gui SEGW tranxn  and try the same?

      Regards
      Arshad

      Author's profile photo Former Member
      Former Member

      Yes Arshad, thank you very much. I tried using my usual SAP Logon and it worked wonders. Thank you for the prompt and the tutorial.

      Regards,

      Indra

      Author's profile photo Prashanth Prabhu
      Prashanth Prabhu

      Hi Arshad,

      you talked about the Get Entity set and Read. what about Create and Update?

      is it possible via the CDS ? can you throw some light what are the steps.

      Prashanth.

      Author's profile photo Andre Fischer
      Andre Fischer

      Hi Prashanth,

      no, this is not possible.

      You can however redefine the CREATE, UPDATE and DELETE method of the data provider extension classes and implement these methods so that your service will support all CRUD-Q methods.

      Best Regards,

      Andre

      Author's profile photo Oliver Merk
      Oliver Merk

      How can associations (and all related entities) from the CDS-View automatically be considered in the service?

      Regards

      Oliver

      Author's profile photo Arshad Ansary
      Arshad Ansary
      Blog Post Author

      Hi Oliver,

      You should declare the association in the projection list of the CDS definition.Then the same will be available for mapping in the GW builder like below

      CDS Mapping.jpg

      You can also refer the below link

      How to build OData Navigation from CDS Associations ?

      Author's profile photo Pablo Costantini
      Pablo Costantini

      Arshad Ansary

      Can you please show us a photo of your segw project?

      • Do you have done an entity "plant" manually?
      • Have you created an association of material to plan by hand?

      This is my project and the error that I am facing right now:

      • Without an association / navigation created by me.

      • With an association/navigation created by me.

      Thanks in advance.

      Author's profile photo Pavan Golesar
      Pavan Golesar

      Thanks, Was looking forward for these OData taking to CDS scenario. Nicely elaborated 😀

      Regards,

      Pavan Golesar

      Author's profile photo Krishna Kishor Kammaje
      Krishna Kishor Kammaje

      Nice blog Arshad. However keep in mind that starting NW 750, CDS can directly be exposed as an oData service without SAP Gateway. Automatically Publish CDS View as OData Service | SCN

      Author's profile photo Former Member
      Former Member

      Nice blog Arshad!

      I followed the steps and my OData is created with a CDS view.

      But for testing I have some problems:

      Capture.PNG

      Does somebody has the same problem of knows the solution?

      Thanks in advance!

      Kind regards,

      Pieter

      Author's profile photo Andre Fischer
      Andre Fischer

      Hi Pieter,

      it would be better if you would post what you find in /n/iwfnd/error_log.

      Best Regards,

      Andre

      Author's profile photo Former Member
      Former Member

      Thanks, it solved with help of the transaction.

      Kind regards,

      Pieter

      Author's profile photo Louis-Arnaud BOUQUIN
      Louis-Arnaud BOUQUIN

      Hello,

      Nice blog but I have a problem. Could you look at this thread please ?

      OData entity association from CDS view : CX_SADL_CONTRACT_VIOLATION

      Author's profile photo Former Member
      Former Member

      Hi Arshad,

      Nice blog. Explained in a very simple way. 🙂

      -Shankar

      Author's profile photo Pramod Singh
      Pramod Singh

      HI Arshad,

      very nice info. Thanks for sharing

      Author's profile photo Babu reddy mudiam
      Babu reddy mudiam

      Hi Arshad,

      Very nice information. I was tried to follow the blog but one error was coming while mapping the data source step.  It shows the following error.

      Can u please explain me the reason.

      Thanks in Advance,

      Babu Reddy.M