SAP HanaXFILE:SAP Hana CDS 开发简介

Posted X档案库

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SAP HanaXFILE:SAP Hana CDS 开发简介相关的知识,希望对你有一定的参考价值。

SAP Hana CDS 开发简介

一、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 View
  Defines a simple CDS view with one data source.
*/
@AbapCatalog.sqlViewName: '$sql_view_name'
@AbapCatalog.compiler.compareFilter: 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'
@AbapCatalog.compiler.compareFilter: 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'
@AbapCatalog.compiler.compareFilter: 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,
    url
from scarr;


ABAP程序调用(Open SQL):

REPORT yz_cds_demo.
SELECT *
FROM ycds_demo01_scarr
INTO 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'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS_DEMO01_SCARR2'
define view YCDS_DEMO02_SCARR 
(id, name, url) --field alias
as 
select from scarr

    --key mandt,
    key carrid as id,   --alias
        carrname,
        url
;

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

模板:

/*
template 2: Define View with Join
  Defines 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'
@AbapCatalog.compiler.compareFilter: 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'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS_DEMO02_JOIN'
define view YCDS_DEMO02_JOIN as 
select from spfli
left outer join scarr
    on scarr.carrid = spfli.carrid 

    key spfli.carrid,
    key spfli.connid,
        scarr.carrname
;


ABAP调用程序:

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

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

模板:

/*
template 3: Define View with Association
  Defines 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'
@AbapCatalog.compiler.compareFilter: 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'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS_DEMO03_ASSOCIATION'
define view YCDS_DEMO03_ASSOCIATION as 
select from spfli
association to scarr as _scarr
    on $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_ASSOCIATION
where carrid = @p_carrid
INTO TABLE @DATA(itab).
cl_demo_output=>display( itab ).

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

模板:

/*
template 4: Define View with To-Parent Association
  Defines a CDS view with a specialized association to its parent CDS entity.
  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.
*/
@AbapCatalog.sqlViewName: '$sql_view_name'
@AbapCatalog.compiler.compareFilter: 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'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS_DEMO04_TOPARENT'
define view YCDS_DEMO04_TOPARENT as 
select from spfli
association to parent YCDS_DEMO01_SCARR as _scarr
    on $projection.carrid = _scarr.id 
    key spfli.carrid,
    key spfli.connid,
    _scarr.id, // Make association public
    _scarr

5、定义单个入参的视图

模板:

/*
template 5: Define View with Parameters
  Defines 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'
@AbapCatalog.compiler.compareFilter: 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'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS_DEMO05_PARAMETERS'
define view YCDS_DEMO05_PARAMETERS
    with parameters p_carrid : s_carr_id
as 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 View
  Defines a simple CDS projection view.
*/
@EndUserText.label: '$ddl_source_description'
@AccessControl.authorizationCheck: #CHECK
define view entity $ddl_source_name_editable as projection on $data_source_name 
	$cursor

实例:

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

7、定义继承的视图

/*
template 7: Extend View
  Extends 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 Parameters
  Defines 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_SCARR
with parameters 
    @Environment.systemField: #CLIENT
    p_clnt  : abap.clnt
returns 
  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 Parameters
  Defines 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 Hierarchy
  Defines 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'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS_DEMO10_HIERARCHY_SOURCE'
define view YCDS_DEMO10_HIERARCHY_SOURCE
as select from ytb_demo_hier
association[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_hier
association[1..1] to YCDS_DEMO10_HIERARCHY_VIEW as _tree
    on $projection.parent= _tree.id

  _tree,
  key id,
  pid as parent,
  name

定义父子层次关系视图:

define hierarchy YCDS_DEMO10_HIERARCHY
  with parameters
    p_id : abap.int4
  as parent child hierarchy (
    source 
        --YCDS_DEMO10_HIERARCHY_SOURCE		--两者都可以
        YCDS_DEMO10_HIERARCHY_VIEW			--两者都可以
    child to parent association _tree
    start where 
        id = :p_id
    siblings 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_id
      INTO TABLE @DATA( cds_result ).
cl_demo_output=>display( cds_result ).


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

模板:

/*
template 11: Define Custom Entity with Parameters
  Defines 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_ENTITY
 with 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 definition
  public
  final
  create 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_RESPONSE                    TYPE 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 definition
    data: LV_PARAM    type STRING."Local variable to fetch and save parameter value
    try.
        try.
            if IO_REQUEST->IS_DATA_REQUESTED( ). "Fetching incoming data
              IO_REQUEST->GET_PAGING( ).

              data(LT_FILTER_COND) = IO_REQUEST->GET_PARAMETERS( ). "Setting the filter condition, fetching parameter names from data definition

              LV_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 etc
              select * 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 sent
                IO_RESPONSE->SET_DATA( IT_RESULT  ). "returning the data as internal table
              endif.
            catch CX_RAP_QUERY_PROVIDER into data(LX_EXC). "CX_A4C_RAP_QUERY_PROVIDER is now deprecated so use CX_RAP_QUERY_PROVIDER
          endtry.
        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: Define

以上是关于SAP HanaXFILE:SAP Hana CDS 开发简介的主要内容,如果未能解决你的问题,请参考以下文章

什么是SAP S4 HANA

HANA系列SAP HANA DB 和SAP HANA studio version查看

sap hana 中的 SLT 服务器是啥?

HANA系列SAP 第一篇EXCEL连接SAP HANA的方法(ODBC)

HANA系列SAP 第二篇EXCEL连接SAP HANA的方法(ODBC)

SAP HANA问题自动诊断专家 | SAP HANA dump analyzer