Tiven Wang
Wang Tiven October 21, 2016
425 favorite favorites
bookmark bookmark
share share

关于SAP’s Core Data Services (CDS) 我们先来看一下它的官方解释 “Core data services (CDS) is an infrastructure for defining and consuming semantically rich data models in SAP HANA.” 它诞生于SAP HANA数据库的出现,后来又被应用到ABAP Dictionary上。接下来让我们来看一下他们有什么联系和区别以及一些基础应用。

Series

  1. Introduction to CDS on HANA and ABAP part 1
  2. Introduction to CDS on HANA and ABAP part 2 - Authorization Check
  3. Introduction to CDS on HANA and ABAP part 3 - Unit Test

Background

ABAP Dictionary

众所周知,要在ABAP应用服务器上开发程序,你需要在ABAP Dictionary中定义数据模型,例如表、视图等。ABAP Dictionary是平台独立的,也就是说它不依赖于任何一种数据库。ABAP字典对象在激活时会通过DBI (Database Interface)产生数据定义语言DDL并在数据库中生成运行时数据库对象。而ABAP Dictionary对象在ABAP服务器中是全局性的,可以被ABAP程序中的Open SQL所使用。

HANA CDS

伴随着SAP HANA的诞生,我们可以不再使用其他应用服务器便可以在HANA上开发应用程序,因为HANA自带应用服务器程序(SAP HANA Extended Application Services)。如何在HANA中创建数据模型,我们首先想到的是HANA View Modeling包括Attribute Views,Analytic Views,Calculation Views以及.hdbtable文件类型的表定义等。对于HANA Views的可视化编辑工具来说,它自然是方便使用,但它终究还只是个工具而已,对于繁琐的代码版本管理来说它显得有些捉襟见肘。所以一种真正意义上的DDL应运而生,这就是Core Data Services。Core Data Services提供了一种基于SQL-based DDL并增加了注解(annotations)和关联(associations)等功能的规范,这种规范可以在不同的平台上实现。CDS在HANA上的实现就是HANA CDS,它可以定义数据库表、视图及数据类型。

ABAP CDS

CDS规范在ABAP上的实现自然就需要适应ABAP服务器的功能,比如CDS所以定义的对象可以在ABAP Dictionary里查看,可以被Open SQL使用,它也可以使用ABAP Dictionary对象,源代码可以使用ADT(ABAP Development Tools)编辑等,但远不是如此而已。ABAP CDS提供了更先进的功能,如基于角色定义(DCL)的新的授权概念,还增加了注解的能力。注解不仅能定义ABAP数据字典的语义还能指定各种框架的规范说明,比如CDS Model自动生成OData service(@OData),UI5的自动适配(@UI),Enterprise Search(@Search)等。通过添加注解达到功能地灵活扩展,CDS的强大之处就在于可以灵活地扩展注解来达到功能的扩展。

ABAP CDS vs. HANA CDS

ABAP CDS和HANA CDS是同一种规范的不同平台实现而已。核心功能几乎相同,但不同平台实现不同功能需要不同的注解,所以两种的源代码并不能直接copy迁移。

对于开发者来说该如何选择CDS两种不同的实现,是个需要斟酌的问题。这里给出一些建议

  • 如果你的HANA是独立运行的或者说并不是作为ABAP服务器的主数据库运行的,那么自然使用不了ABAP CDS,必须使用HANA CDS
  • 如果你运行的是ABAP on HANA(也就是说HANA数据库作为AS ABAP的主数据库存在)
    • 如果你想在全局环境或者Open SQL中使用CDS实体对象,或者需要一些具有ABAP关联性的注解,那么必须使用ABAP CDS
    • 如果你需要在ABAP中使用CDS实体对象,但想要像ABAP存储库对象一样地传输和升级它的话,可以使用ABAP CDS
    • 如果以上都不需要,你可以使用HANA CDS,它跟HANA有更好的集成。同时你仍然可以在ABAP中使用Native SQL(ADBC,AMDP)访问HANA CDS

有关CDS一些详细注解技术文档请参考SAP Blog - Annotations in ABAP CDSSAP Help - ABAP CDS - SAP Annotations

ABAP CDS Step by Step

最好的学习方式是拿一个实际的例子来一步步写代码,下面就是一个实际产品中的应用场景

New DDL Source

在ADT中新建一个DDL Source文件Z_MKT_DIGACC,向导中可以选择模板生成不同的CDS基本结构

Create CDS by Templates
Create CDS by Templates

生成的Source code
@AbapCatalog.sqlViewName: 'ZMKT_DIGACC'定义ABAP View Name的注解
@EndUserText.label: 'Marketing Digital Account'定义ABAP View Description的注解

@AbapCatalog.sqlViewName: 'ZMKT_DIGACC'
@AbapCatalog.compiler.compareFilter: true
@EndUserText.label: 'Marketing Digital Account'
define view Z_Mkt_Digacc as select from cuand_da_root
association [0..*] to cuand_ce_mp_root as _MarketingPermission
    on cuand_da_root.comm_cat_key = _MarketingPermission.comm_cat_key {
    key cuand_da_root.db_key as DigitalAccount,
    cuand_da_root.comm_cat_key as CommCatKey,
    _MarketingPermission.contact_key as ContactKey,
    _MarketingPermission // Make association public
}

Check Views

激活后便可以在SAP系统中查看到CDS运行时对象,ABAP中使用SE11 View查看ZMKT_DIGACC。 HANA数据库在相应的Schema中查找View ZMKT_DIGACC

Generate OData Service

想要把一个View暴露成OData service尤其简单。

Generate Service Artifacts From a CDS View

只需要一个注解@OData.publish: true即可生成OData所需Class代码

@AbapCatalog.sqlViewName: 'ZMKT_DIGACC'
...
@OData.publish: true
define view Z_Mkt_Digacc as select from cuand_da_root
association [0..*] to cuand_ce_mp_root as _MarketingPermission
    on cuand_da_root.comm_cat_key = _MarketingPermission.comm_cat_key {
    ...
}

激活则生成以下Gateway所需部件

  • The actual service artifact with the technical name <CDS_VIEW>_CDS. You can find it as SAP Gateway Business Suite Enablement - Service object (object type: R3TR IWSV)
  • An SAP Gateway model (object type: R3TR IWMO) with the name <CDS_VIEW>_CDS
  • An ABAP class CL_<CDS_VIEW> that is used to provide model metadata to the SAP Gateway service.

Activate OData Service in the SAP Gateway Hub

生成的Gateway部件需要手动激活才能产生OData服务。

打开transaction /IWFND/MAINT_SERVICEAdd Service -> Get Services -> 选择Technical Service Name <CDS_VIEW>_CDS 我们的是 Z_MKT_DIGACC_CDS -> Add Selected Services -> Specify the package -> Done

结果会生成SAP Gateway: Service Group Metadata object和An SAP Gateway: Model Metadata object

Test the Activated OData Service

使用SAP Gateway Client或者外部Rest Client工具测试生成的OData service

查看服务的metadata信息

/sap/opu/odata/sap/Z_MKT_DIGACC_CDS/?$format=json

选择Z_Mkt_Digacc的头10条数据

/sap/opu/odata/sap/Z_MKT_DIGACC_CDS/Z_Mkt_Digacc?$format=json&$top=10

详细参考SAP Help - Expose CDS View as an OData Service

References

Similar Posts

  • SAP HANA Database SAP HANA is an in-memory, column-oriented, relational database management system developed and marketed by SAP SE. Its primary function as database server is to store and retrieve data as requested by the applications. In addition, it performs advanced analytics (predictive analytics, spatial data processing, text analytics, text search, streaming analytics, graph data processing) and includes ETL capabilities as well as an application server.
  • SAP ABAP CDS
  • Unit Test by Spring MVC Test Framework Testing is an integral part of enterprise software development. Dependency Injection should make your code less dependent on the container than it would be with traditional Java EE development. This topic introduce how to create Unit Test by Spring MVC Test Framework for Java project on HCP
  • Apply Spring Data JPA to Java Project on HCP Spring Data’s mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store. Spring Data JPA, part of the larger Spring Data family, makes it easy to easily implement JPA based repositories. This module deals with enhanced support for JPA based data access layers. It makes it easier to build Spring-powered applications that use data access technologies.
  • Apply Spring Architecture to Java Project on HCP The Spring Framework is an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform. Although the framework does not impose any specific programming model, it has become popular in the Java community as an alternative to, replacement for, or even addition to the Enterprise JavaBeans (EJB) model. I will show you how to apply Spring Frameworks to Java project on HANA Cloud Platform.
  • Persistence Service for Java Maven Project on HCP The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database. JPA was defined as part of the EJB 3.0 specification as a replacement for the EJB 2 CMP Entity Beans specification. In this topic I will show you how to create persistence service for Java project on HCP using Java Persistence API (JPA) and it's implementation EclipseLink

Comments

comments powered by Disqus
Back to Top