2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 【SAP Hana】X-DOC:SAP Hana CDS 开发简介

【SAP Hana】X-DOC:SAP Hana CDS 开发简介

时间:2022-02-08 17:59:25

相关推荐

【SAP Hana】X-DOC:SAP Hana CDS 开发简介

SAP Hana CDS 开发简介

一、CDS简介二、CDS VIEW 创建模板1、定义单数据源的简单视图2、定义两个数据源的JOIN视图3、定义具有关联关系的视图4、定义父类关联关系的视图5、定义单个入参的视图6、定义简单的投影视图实体7、定义继承的视图8、定义带有入参的表函数9、定义带有入参的抽象实体10、定义父子层次视图11、定义单个入参的客户实体 三、CDS VIEW ENTITY 创建模板12、定义单数据源的简单视图实体13、定义根视图实体14、定义关联父实体的视图实体15、继承投影视图实体16、继承抽象实体17、继承客户实体

一、CDS简介

为了利用SAP HANA进行应用程序开发,SAP引入了一个新的基础数据建模,称为核心数据服务(CDS)。使用CDS,数据模型是在数据库服务器上定义和使用的,而不是在应用程序服务器上。CDS还提供了超越传统数据建模工具的功能,包括对概念建模和关系定义、内置函数和扩展的支持。

最初,CDS仅在SAP HANA的设计时和运行时环境中可用。现在,CDS概念在SAP NetWeaver中为ABAP也得到了充分的实现,使开发人员能够在将代码执行下推到数据库的同时,使用ABAP开发工具在ABAP层工作。

CDS简化和统一了定义和使用数据模型的方式,不管你用的是哪种消费技术。从技术上讲,它是对SQL的增强,为您提供了一种数据定义语言(DDL),用于定义语义丰富的数据库表/视图(CDS实体)和数据库中的用户定义类型。

包括:

用于数据模型中的计算和查询的表达式概念层次上的关联,在查询中使用简单的路径表达式代替连接使用附加的(domain specific特殊域)元数据来丰富数据模型的注解。[元数据是“描述数据的数据”。元数据可以为数据说明其元素或属性(名称、大小、数据类型等),或结构(长度、字段、数据列),或其相关数据(位于何处、如何联系、拥有者)。]

CDS版本变更历史:

二、CDS VIEW 创建模板

1、定义单数据源的简单视图

模板:

/*template 1: Define ViewDefines a simple CDS view with one data source.*/@AbapCatalog.sqlViewName: '${sql_view_name}'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #CHECK@EndUserText.label: '${ddl_source_description}'define view ${ddl_source_name_editable} as select from ${data_source_name} {${cursor}}

实例(CDS标准写法):

@AbapCatalog.sqlViewName: 'YV_DEMO01_SCARR'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'CDS_DEMO01_SCARR'define view YCDS_DEMO01_SCARR as select from scarr{--key mandt,-- CDS会自动添加key carrid,carrname,url};

或者(传统SQL写法):

@AbapCatalog.sqlViewName: 'YV_DEMO01_SCARR'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'CDS_DEMO01_SCARR'define view YCDS_DEMO01_SCARR as select --key mandt,-- CDS会自动添加key carrid,carrname,urlfrom scarr;

ABAP程序调用(Open SQL):

REPORT yz_cds_demo.SELECT *FROM ycds_demo01_scarrINTO TABLE @DATA(itab).cl_demo_output=>display( itab ).

ABAP程序调用(SALV IDA):

REPORT YZ_IDA_DEMO.CL_SALV_GUI_TABLE_IDA=>CREATE_FOR_CDS_VIEW( iv_cds_view_name = 'YCDS_DEMO01_SCARR' )->FULLSCREEN( )->DISPLAY( ).

实例(定义字段别名):

@AbapCatalog.sqlViewName: 'YV_DEMO01_SCARR2'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'CDS_DEMO01_SCARR2'define view YCDS_DEMO02_SCARR (id, name, url) --field aliasas select from scarr{--key mandt,key carrid as id, --aliascarrname,url};

2、定义两个数据源的JOIN视图

模板:

/*template 2: Define View with JoinDefines a CDS view which combines two data sources using a left outer join.The join conditions are specified in the on clause.*/@AbapCatalog.sqlViewName: '${sql_view_name}'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #CHECK@EndUserText.label: '${ddl_source_description}'define view ${ddl_source_name_editable} as select from ${data_source_name}left outer join ${joined_data_source_name}on ${data_source_name}.${element_name} = ${joined_data_source_name}.${joined_element_name} {${cursor}}

实例:

@AbapCatalog.sqlViewName: 'YV_DEMO02_JOIN'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'CDS_DEMO02_JOIN'define view YCDS_DEMO02_JOIN as select from spflileft outer join scarron scarr.carrid = spfli.carrid {key spfli.carrid,key spfli.connid,scarr.carrname};

ABAP调用程序:

REPORT yz_cds_demo.SELECT *FROM ycds_demo02_joinINTO TABLE @DATA(itab).cl_demo_output=>display( itab ).

3、定义具有关联关系的视图

模板:

/*template 3: Define View with AssociationDefines a CDS view with a public association to another data source.The association can be used in the select list as well as by other CDS views which use this CDS view as a data source.*/@AbapCatalog.sqlViewName: '${sql_view_name}'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #CHECK@EndUserText.label: '${ddl_source_description}'define view ${ddl_source_name_editable} as select from ${data_source_name}association [${1}] to ${target_data_source_name} as ${_association_name}on $$projection.${element_name} = ${_association_name}.${target_element_name} {${cursor}${_association_name} // Make association public}

实例:

@AbapCatalog.sqlViewName: 'YV_DEMO03_ASSOCI'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'CDS_DEMO03_ASSOCIATION'define view YCDS_DEMO03_ASSOCIATION as select from spfliassociation to scarr as _scarron $projection.carrid = _scarr.carrid {key spfli.carrid,key spfli.connid,--_scarr.carrname,_scarr // Make association public, joined on demand}

在HanaStudio中预览数据:

HANA访问数据:

select * from YV_DEMO03_ASSOCI;-- 未访问关联

ABAP访问数据:

REPORT yz_cds_demo.PARAMETERS: p_carrid TYPE scarr-carrid.SELECT carrid, connid, \_scarr-carrname AS flightname"访问关联FROM YCDS_DEMO03_ASSOCIATIONwhere carrid = @p_carridINTO TABLE @DATA(itab).cl_demo_output=>display( itab ).

4、定义父类关联关系的视图

模板:

/*template 4: Define View with To-Parent AssociationDefines a CDS view with a specialized association to its parent CDS positions and to-parent associations are used to define the structure of a business object which can be used in the ABAP RESTful Programming Model.*/@AbapCatalog.sqlViewName: '${sql_view_name}'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #CHECK@EndUserText.label: '${ddl_source_description}'define view ${ddl_source_name_editable} as select from ${data_source_name}association to parent ${target_data_source_name} as ${_association_name}on $$projection.${element_name} = ${_association_name}.${target_element_name} {${cursor}${_association_name} // Make association public}

实例:

@AbapCatalog.sqlViewName: 'YV_DEMO04_TOPARE'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'CDS_DEMO04_TOPARENT'define view YCDS_DEMO04_TOPARENT as select from spfliassociation to parent YCDS_DEMO01_SCARR as _scarron $projection.carrid = _scarr.id {key spfli.carrid,key spfli.connid,_scarr.id, // Make association public_scarr}

5、定义单个入参的视图

模板:

/*template 5: Define View with ParametersDefines a CDS view with a single input parameter.The input parameter can be used as an element in the select list or as an operand in conditional or arithmetic expressions.*/@AbapCatalog.sqlViewName: '${sql_view_name}'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #CHECK@EndUserText.label: '${ddl_source_description}'define view ${ddl_source_name_editable}with parameters ${parameter_name} : ${parameter_type}as select from ${data_source_name} {${cursor}}

实例:

@AbapCatalog.sqlViewName: 'YV_DEMO05_PARAM'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'CDS_DEMO05_PARAMETERS'define view YCDS_DEMO05_PARAMETERSwith parameters p_carrid : s_carr_idas select from spfli {key connid,cityfrom,cityto}where carrid = $parameters.p_carrid;

SE11图(不支持查看表数据):

SE16N图(可以执行,需要输入参数):

ABAP程序调用:

REPORT yz_cds_demo.PARAMETERS: p_carrid TYPE scarr-carrid.SELECT *FROM ycds_demo05_parameters( p_carrid = @p_carrid )INTO TABLE @DATA(itab).cl_demo_output=>display( itab ).

HANA 查询数据:

select * from YV_DEMO05_PARAM('AA') where mandt = 200;--或者select * from YV_DEMO05_PARAM( p_carrid => 'AA' ) where mandt = 200;

6、定义简单的投影视图实体

模板:用于屏蔽一些字段(一般用于保护数据时使用)。

/*template 6: Define Projection ViewDefines a simple CDS projection view.*/@EndUserText.label: '${ddl_source_description}'@AccessControl.authorizationCheck: #CHECKdefine view entity ${ddl_source_name_editable} as projection on ${data_source_name} {${cursor}}

实例:

@EndUserText.label: 'CDS_DEMO06_PROJECTION'@AccessControl.authorizationCheck: #NOT_REQUIREDdefine view entity YCDS_DEMO06_PROJECTION as projection on YCDS_DEMO04_TOPARENT {key carrid,key connid}

7、定义继承的视图

/*template 7: Extend ViewExtends an existing CDS view by adding the specified elements to the select list of the CDS view using a view enhancement.*/@AbapCatalog.sqlViewAppendName: '${sql_view_append_name}'@EndUserText.label: '${ddl_source_description}'extend view ${view_name} with ${ddl_source_name_editable} {${data_source_name}.${element_name}}

实例:

@AbapCatalog.sqlViewAppendName: 'YV_DEMO07_EXTEND'@EndUserText.label: 'CDS_DEMO07_EXTEND'extend view YCDS_DEMO02_JOIN //原CDS,有三个字段with YCDS_DEMO07_EXTEND //新CDS,追加一个字段{scarr.currcode}

8、定义带有入参的表函数

模板:AMDP FUNCTION实现。

/*template 8: Define Table Function with ParametersDefines the type signature of a client dependent CDS table function with importing parameters. The CDS table function is implemented in the specified ABAP method.The CDS table function can be used in Open SQL and as a data source in other CDS view definitions.*/@EndUserText.label: '${ddl_source_description}'define table function ${ddl_source_name_editable}with parameters ${parameter_name} : ${parameter_type}returns {${client_element_name} : abap.clnt;${element_name} : ${element_type};${cursor}}implemented by method ${class_name}=>${method_name};

实例:

@EndUserText.label: 'ADMP_DEMO_SCARR'define table function YCDS_ADMP_DEMO_SCARRwith parameters @Environment.systemField: #CLIENTp_clnt : abap.clntreturns {mandt: abap.clnt;carrid : s_carr_id;carrname : s_carrname;url : s_carrurl;}implemented by method ycl_amdp_hdb_demo=>get_scarr_for_cds;

具体参考:X档案:SAP ABAP 中 AMDP 简介及实现方法

9、定义带有入参的抽象实体

模板:仅描述类型属性且未实例化任何数据库对象的 CDS 实体。

/*template 9: Define Abastract Entity with ParametersDefines an abstract CDS entity with a single input parameter.*/@EndUserText.label: '${ddl_source_description}'define abstract entity ${ddl_source_name_editable} with parameters ${parameter_name} : ${parameter_type} {${element_name} : ${element_type};${cursor}}

实例:

@EndUserText.label: 'CDS_DEMO09_ABSTRACT'define abstract entity YCDS_DEMO09_ABSTRACT with parameters p_carrid : s_carr_id {connid : s_conn_id;cityfrom : s_from_cit;cityto : s_to_city;}

10、定义父子层次视图

模板:

/*template 10: Define Parent Child HierarchyDefines a simple CDS parent child hierarchy.*/define hierarchy ${ddl_source_name_editable} as parent child hierarchy (source ${data_source_name}child to parent association ${_association_name}start where ${element_name} = ${value}siblings order by ${order_by_element_name}){${element_name}${cursor}}

定义父子层次表:YTB_DEMO_HIER

插入数据:

CREATE COLUMN TABLE "SAPHANADB"."YTB_DEMO_HIER" ("MANDT" NVARCHAR(3) DEFAULT '000' NOT NULL ,"ID" INTEGER CS_INT DEFAULT 0 NOT NULL ,"PID" INTEGER CS_INT DEFAULT 0 NOT NULL ,"NAME" NVARCHAR(20) DEFAULT '' NOT NULL ,CONSTRAINT "YTB_DEMO_HIER~0" PRIMARY KEY ("MANDT","ID"));insert into YTB_DEMO_HIER values('200', 1, 0, '图书');insert into YTB_DEMO_HIER values('200', 2, 1, '教材类');insert into YTB_DEMO_HIER values('200', 3, 1, '计算机类');insert into YTB_DEMO_HIER values('200', 4, 3, 'Java');insert into YTB_DEMO_HIER values('200', 5, 3, '.Net');insert into YTB_DEMO_HIER values('200', 6, 3, 'SAP');insert into YTB_DEMO_HIER values('200', 7, 1, '文学类');insert into YTB_DEMO_HIER values('200', 8, 1, '科幻类');insert into YTB_DEMO_HIER values('200', 9, 8, '三体');insert into YTB_DEMO_HIER values('200', 10, 8, '流浪地球');

定义父子层次数据源:CDS View

@AbapCatalog.sqlViewName: 'YTB_DEMO_HIER_S'@pareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'CDS_DEMO10_HIERARCHY_SOURCE'define view YCDS_DEMO10_HIERARCHY_SOURCEas select from ytb_demo_hierassociation[1..1] to YCDS_DEMO10_HIERARCHY_SOURCE as _tree on $projection.parent= _tree.id{_tree,key id,pid as parent,name}

或者:CDS View 实体

@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'CDS_DEMO10_HIERARCHY_VIEW'define view entity YCDS_DEMO10_HIERARCHY_VIEW as select from ytb_demo_hierassociation[1..1] to YCDS_DEMO10_HIERARCHY_VIEW as _treeon $projection.parent= _tree.id{_tree,key id,pid as parent,name}

定义父子层次关系视图:

define hierarchy YCDS_DEMO10_HIERARCHYwith parametersp_id : abap.int4as parent child hierarchy (source --YCDS_DEMO10_HIERARCHY_SOURCE--两者都可以YCDS_DEMO10_HIERARCHY_VIEW--两者都可以child to parent association _treestart where id = :p_idsiblings order by id ascending){id, parent, name }

在Hana中预览数据:

在Hana SQL中查询数据:

select * from YTB_DEMO_HIER_S;--sql view,可以访问,有数据select * from YCDS_DEMO10_HIERARCHY_SOURCE;--cds view,无法访问select * from YCDS_DEMO10_HIERARCHY_VIEW;--cds view entity,可以访问,没有数据select * from YCDS_DEMO10_HIERARCHY( p_id => 3 );--可以访问,没有数据

在ABAP中访问:

REPORT YZ_CDS_DEMO.PARAMETERS: p_id type YCDS_DEMO10_HIERARCHY_VIEW-id.SELECT FROM YCDS_DEMO10_HIERARCHY( p_id = @p_id )FIELDS id,parent,name,hierarchy_rank,hierarchy_tree_size,hierarchy_parent_rank,hierarchy_level,hierarchy_is_cycle,hierarchy_is_orphan,node_id,parent_idINTO TABLE @DATA( cds_result ).cl_demo_output=>display( cds_result ).

11、定义单个入参的客户实体

模板:

/*template 11: Define Custom Entity with ParametersDefines a custom CDS entity with a single input parameter.*/@EndUserText.label: '${ddl_source_description}'define custom entity ${ddl_source_name_editable} with parameters ${parameter_name} : ${parameter_type} {key ${key_element_name} : ${key_element_type};${element_name} : ${element_type};${cursor}}

实例:

(1)定义CDS客户实体:YCDS_DEMO11_CUSTOM_ENTITY

@ObjectModel.query.implementedBy : 'ABAP:YCL_CUSTOM_ENTITY'@EndUserText.label: 'CDS_DEMO11_CUSTOM_ENTITY'define custom entity YCDS_DEMO11_CUSTOM_ENTITYwith parameters p_id : abap.char(3){key carrid: abap.char(3); // Returning fields are mentioned between {} just like ordinary CDS carrname : abap.char(20);// Returning field set must contain a key or key combination url : abap.char(255);}

(2)定义ABAP实现类:YCL_CUSTOM_ENTITY

class YCL_CUSTOM_ENTITY definitionpublicfinalcreate public .public section.interfaces IF_RAP_QUERY_PROVIDER .protected section.private section.ENDCLASS.CLASS YCL_CUSTOM_ENTITY IMPLEMENTATION.* <SIGNATURE>---------------------------------------------------------------------------------------+* | Instance Public Method YCL_CUSTOM_ENTITY->IF_RAP_QUERY_PROVIDER~SELECT* +-------------------------------------------------------------------------------------------------+* | [--->] IO_REQUEST TYPE REF TO IF_RAP_QUERY_REQUEST* | [--->] IO_RESPONSETYPE REF TO IF_RAP_QUERY_RESPONSE* | [!CX!] CX_RAP_QUERY_PROV_NOT_IMPL* | [!CX!] CX_RAP_QUERY_PROVIDER* +--------------------------------------------------------------------------------------</SIGNATURE>method IF_RAP_QUERY_PROVIDER~SELECT.data:IT_RESULT type table of YCDS_DEMO11_CUSTOM_ENTITY. "Internal table to be returned , easier to handle return if internal table is as same type of our data definitiondata: LV_PARAM type STRING."Local variable to fetch and save parameter valuetry.try.if IO_REQUEST->IS_DATA_REQUESTED( ). "Fetching incoming dataIO_REQUEST->GET_PAGING( ).data(LT_FILTER_COND) = IO_REQUEST->GET_PARAMETERS( ). "Setting the filter condition, fetching parameter names from data definitionLV_PARAM = value #( LT_FILTER_COND[ PARAMETER_NAME = 'p_id' ]-VALUE optional ). "Fetching the parameter value"Using the parameter we could do whatever we want , like selecting from a table , doing certain calculations etcselect * from scarr where carrID = @LV_PARAM into CORRESPONDING FIELDS OF TABLE @IT_RESULT.IO_RESPONSE->SET_TOTAL_NUMBER_OF_RECORDS( LINES( IT_RESULT ) ). "setting the total number of records which will be sentIO_RESPONSE->SET_DATA( IT_RESULT ). "returning the data as internal tableendif.catch CX_RAP_QUERY_PROVIDER into data(LX_EXC). "CX_A4C_RAP_QUERY_PROVIDER is now deprecated so use CX_RAP_QUERY_PROVIDERendtry.catch CX_RFC_DEST_PROVIDER_ERROR into data(LX_DEST).endtry.endmethod.ENDCLASS.

(3)定义服务:

@EndUserText.label: 'SVS_EXPOSE_CUSTOM_ENTITY'define service YSVS_EXPOSE_CUSTOM_ENTITY {expose YCDS_DEMO11_CUSTOM_ENTITY;}

(4)服务如何使用(暂时还没研究到)

三、CDS VIEW ENTITY 创建模板

说明:

NW 7.55 以上引入。

CDS View Entity 对象,可以在ABAP中访问,但是不能在Hana Sql中访问(无数据)。

12、定义单数据源的简单视图实体

模板:

/*template 12: Defines a simple CDS view entity with one data source.*/@AbapCatalog.viewEnhancementCategory: [#NONE]@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: '${ddl_source_description}'@Metadata.ignorePropagatedAnnotations: true@ObjectModel.usageType:{serviceQuality: #X,sizeCategory: #S,dataClass: #MIXED}define view entity ${ddl_source_name_editable} as select from ${data_source_name}{${data_source_elements}${cursor}}

实例:

@AbapCatalog.viewEnhancementCategory: [#NONE]@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'CDS_DEMO12_VIEW_ENTITY'@Metadata.ignorePropagatedAnnotations: true@ObjectModel.usageType:{serviceQuality: #X,sizeCategory: #S,dataClass: #MIXED}define view entity YCDS_DEMO12_VIEW_ENTITY as select from scarr{key carrid,carrname,url}

13、定义根视图实体

模板:

/*template 13: Defines a root CDS view entity with a specialized association to a child CDS entity.Root nodes, compositions and to-parent associations are used to define the structure of a business object which can be used in the ABAP RESTful programming model.*/@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: '${ddl_source_description}'define root view entity ${ddl_source_name_editable} as select from ${data_source_name}composition of ${target_data_source_name} as ${_association_name}{${data_source_elements}${cursor}${_association_name} // Make association public}

14、定义关联父实体的视图实体

模板:

/*template 14: Defines a CDS view entity with a specialized association to its parent CDS positions and to-parent associations are used to define the structure of a business object which can be used in the ABAP RESTful programming model.*/@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: '${ddl_source_description}'define view entity ${ddl_source_name_editable} as select from ${data_source_name}association to parent ${target_data_source_name} as ${_association_name}on $$projection.${element_name} = ${_association_name}.${target_element_name}{${data_source_elements}${cursor}${_association_name} // Make association public}

15、继承投影视图实体

模板:

/*template 15: Extends an existing CDS projection view entity by adding the specified elements to its select list.*/extend view entity ${view_name} with {${base_data_source_name}.${element_name}}

16、继承抽象实体

模板:

/*template 16: Extends an abstract CDS entity by adding the specified elements to its select list.*/extend abstract entity ${entity_name} with{${element_name} : ${element_type};}

17、继承客户实体

模板:

/*template 17: Extends a custom CDS entity by adding the specified elements to its select list.*/extend custom entity ${entity_name} with{${element_name} : ${element_type};}

created by xlevon on 0206.

原创文章,转载请注明来源-X档案

参考文档:

ABAP CDS - Syntax

Working with Hierarchies in ABAP SQL

Custom Entities – Business Technology Platform (SAP Cloud Platform)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。