ABAP CDS(Core Data Service)介绍

  • 关于Core Data Service(CDS)
  • 什么是CDS?

CDS:Core Data Service-核心数据服务。CDS 是使用基于 SQL 的数据定义语言 (DDL) 定义的,该语言基于标准 SQL 并带有一些附加概念。使用类似 SQL 的灵活表达式可以进行复杂的数据建模。有两种类型的 CDS:ABAP CDS 和 HANA CDS。 S/4 HANA 使用 ABAP CDS,它是定义的 ABAP 对象,在 ABAP 层进行管理。不像 HANA CDS 是本地 HANA 对象。使用 CDS 创建的视图称为 CDS 视图,CDS是S/4 HANA企业管理的主要关键技术之一。CDS 视图通常用于 Fiori 和 SAP Business Objects 分析。

  • 为什么需要CDS?
    1. Code Push-Down: 为了充分发挥 HANA 的优势,有必要将应用层的进程转移到 DB 层。而CDS 视图是Push-Down的主要方式。 

PS:

  1. SAP建议在 CDS 视图中而不是在 ABAP 应用程序服务器上实现逻辑。
  2. SAP建议创建基本的 CDS 视图并将它们用于许多程序/应用程序。
  • ABAP CDS的特点
    1. CDS 视图完全基于 SQL。 集成“标准 SQL”功能,如连接、联合、内置函数等。
    2. 可以进行复杂的计算,例如 变量、层次结构和异常聚合等。通过在内部利用 BW OLAP 引擎。
    3. Analytical Query(CDS 视图的一种)可以像 BW 查询一样使用,例如 在事务 RSRT中运行。
    4. CDS 视图可以作为 OData 公开,并且可以在 Fiori 中使用。
    5. CDS 视图可以在ABAP 程序中访问,也可以作为数据源向BW 发送数据。
    6. 可通过注释在元模型级别上进行扩展。
    7. 使用 DCL 的 PFCG 授权对象可以进行授权控制。
  • ABAP CDS的优点
    1. 通过Push-Down实现高性能。
    2. 能使用灵活的 SQL 表达式。
    3. 可以很容易地被应用程序使用(例如:OData、Fiori UI等)。
    4. 可重复用于许多流程。
  • 使用ADT 创建CDS View

点击File,选择New->Others

展开ABAP

展开Core Data Service,选择Data Define,点击Next。

填写视图名称及描述,点击Finish。

选择Define View,点击Finish。

填写SQL View Name,一般将CDS View Name中的符号(此处为下划线)去掉作为SQL View Name。

     

  • 创建CDS View(With Parameters)

点击File,选择New->Others

展开ABAP

展开Core Data Service,选择Data Define,点击Next。

填写视图名称及描述,点击Finish。

选择Define View With Parameters,点击Finish。

填写SQL View Name,一般将CDS View Name中的符号(此处为下划线)去掉作为SQL View Name。

CDS 系统变量-$session.vname

Demo CDS: demo_cds_session_variables

vname

Valued when accessed using ABAP SQL

user

Current user name, nominal value of the ABAP system field sy-uname

client

Current client. The default value is the nominal value of the ABAP system field sy-mandt. In reads with an ABAP SQL statement (with the addition USING CLIENT) and in calls of an AMDP method from ABAP (in whose declaration the addition AMDP OPTIONS CDS SESSION CLIENT is specified), the value specified here.

system_language

Text environment language of the current internal session, nominal value of the ABAP system field sy-langu

system_date

Current system date of the AS ABAP, nominal value of the ABAP system field sy-datum

CDS 数值函数

Demo CDS: demo_cds_sql_functions_num

Valid Argument Types

Result Type

ABS(arg)

INT1, INT2, INT4, INT8, DEC, CURR, QUAN, FLTP

Data type of arg

CEIL(arg)

INT1, INT2, INT4, INT8,DEC, CURR, QUAN, FLTP

INT4, INT8 (if arg is of type INT8)

DIV(arg1, arg2)

INT1, INT2, INT4, INT8, DEC, CURR, QUAN without decimal places.

Data type arg1, where DEC, CURR and QUAN are implemented after INT4

DIVISION(arg1, arg2, dec)

arg1, arg2: INT1, INT2, INT4, INT8, DEC, CURR, QUAN

dec: integer numeric literal greater than or equal to 0 and not greater than the maximum value of 6 and the length of arg2 plus the number of decimal places of arg1 plus 1

DEC with dec decimal places. The length of the result is the length of arg1 minus the decimal places in arg1 plus the decimal places in arg2 plus dec. This value must not be greater than 31.

FLOOR(arg)

INT1, INT2, INT4, INT8, DEC, CURR, QUAN

Data type of arg for the integer types, else DEC without decimal places

MOD(arg1, arg2)

INT1, INT2, INT4, INT8

Data type of arg1

ROUND(arg, pos)

arg: INT1, INT2, INT4, INT8, DEC, CURR, QUAN

pos: Literal, field of a data source or input parameter of type INT1, INT2, or INT4

Data type arg: hereINT1 and INT2 are implemented after INT4.

CDS 字符串内置函数

Demo CDS: demo_cds_sql_functions_string

Valid Argument Types

Result Type

CONCAT(arg1, arg2)

See below

SSTRING if an argument has the type SSTRING, else CHAR with the length of the result.

CONCAT_WITH_SPACE(arg1, arg2, spaces )

arg1, arg2: see below

spaces: positive numeric literal greater than 0 and less than or equal to 1331

SSTRING if an argument has the type SSTRING, else CHAR with the length of the result.

INSTR(arg, sub)

arg: see below

sub: non-empty character literal

INT4

LEFT(arg, len)

arg: see below

len: positive numeric literal greater than 0 and less than or equal to 1333

SSTRING if arg has the type SSTRING, else CHAR with length len

LENGTH(arg)

See below

INT4

LOWER( arg )

See below, with the exception of NUMC, DATS, and TIMS

Data type of arg in the length of arg

LPAD(arg, len, src)

arg: see below

len: positive numeric literal greater than 0 and less than or equal to 1333

src: Character literal

SSTRING if arg has the type SSTRING, else CHAR with length len

LTRIM(arg, char)

arg: see below

char: Character literal with length 1

SSTRING if arg has the type SSTRING, else CHAR with the length of arg.

REPLACE(arg1, arg2, arg3)

See below

SSTRING if arg1 or arg3 has the type SSTRING, else CHAR with the maximum possible length of the result.

RIGHT(arg,len)

arg: see below

len: positive numeric literal greater than 0 and less than or equal to 1333

SSTRING if arg has the type SSTRING, else CHAR with length len

RPAD(arg, len, src)

arg: see below

len: positive numeric literal greater than 0 and less than or equal to 1333

src: Character literal

SSTRING if arg has the type SSTRING, else CHAR with length len

RTRIM(arg, char)

arg: see below

char: Character literal with length 1

SSTRING if arg has the type SSTRING, else CHAR with the length of arg.

SUBSTRING(arg, pos, len)

arg: see below

pos and len: positive numeric literals not equal to zero

SSTRING if arg has the type SSTRING, else CHAR or NUMCwith length of at lest len

UPPER( arg )

See below, with the exception of NUMC, DATS, and TIMS

Data type of arg in the length of arg

CDS 日期函数

Demo CDS: demo_cds_date_functions

dats_is_valid:DATS_IS_VALID 确定日期(如果指定)是否包含格式为 YYYYMMDD 的有效日期。实际参数必须具有内置数据类型 DATS。结果的数据类型为 INT4。有效日期产生值 1,所有其他输入值(包括空值)产生值 0。

dats_days_between:DATS_DAYS_BETWEEN 计算两个指定日期 date1 和 date2 之间的差异,以天为单位。实际参数必须具有内置数据类型 DATS,并且应包含格式为 YYYYMMDD 的有效日期。任何指定的无效日期都会在计算之前初始化或设置为值“00010101”。结果的数据类型为 INT4。如果 date2 大于 date1,则结果为正。在相反的情况下,则为负数。

dats_add_days:将天数添加到指定的日期。

dats_add_months:将月份添加到指定的日期日期。

CDS 货币换算

Demo CDS: demo_cds_curr_conv

Formal Parameter

Optional

Meaning

Data Type

Actual Parameter

amount

-

Initial value

CURR

Field of a data source, parameter

source_currency

-

Source currency from column WAERS of database table TCURC

CUKY

Field of a data source, literal, parameter

target_currency

-

Target currency from column WAERS of database table TCURC

CUKY

Field of a data source, literal, parameter

exchange_rate_date

-

Exchange rate date for column GDATU of database table TCURR

DATS

Field of a data source, literal, parameter

exchange_rate_type

X

Exchange rate type from column KURST of database table TCURR, default value: "M"

CHAR with length 4

Literal, parameter

client

X, -

Client whose rules are used to perform the currency conversion. Optional (if the current data source is client-specific). Default: Content of the client column of the current row

CLNT

Field of a data source, literal, parameter

round

X

If "X" (default value), the intermediate result of the conversion is rounded to the end result using commercial rounding; else it is truncated

CHAR

Literal

decimal_shift

X

If "X" (default value), the decimal places of the source value are moved as specified by the decimal places of the source currency (see below).

CHAR

Literal

decimal_shift_back

X

If "X" (default value), the decimal places of the result are moved as specified by the decimal places of the target currency (see below).

CHAR

Literal

error_handling

X

Error handling. If "FAIL_ON_ERROR" (default value), an error raises an exception; if "SET_TO_NULL", the result is reset to the null value; if "KEEP_UNCONVERTED", the source value is not changed.

CHAR with length 20

Literal

CDS Annotations

对于使用 CDS 视图(如 ODATA、UI等)的应用程序,注释可用于进一步丰富元模型,超越 SQL。CDS Annotations有如下几种:

  1. Analytics Annotations:分析注解(Fiori Design Studio、Analysis Office)
  2. AnalyticsDetails Annotations: AnalyticsDetails 注解
  3. Consumption Annotations: 消费注解
  4. DefaultAggregation Annotations: 默认聚合注解
  5. EnterpriseSearch Annotations: EnterpriseSearch 注解(目前仅适用于 SAP 内部项目,而不适用于客户项目
  6. Hierarchy Annotations: 层次注解
  7. ObjectModel Annotations: 对象模型注解
  8. OData Annotations:OData注解
  9. Search Annotations: 搜索注解
  10. Semantics Annotations:语义注解
  11. UI Annotations:UI注解
  12. VDM Annotations:VDM注解

常用的Annotations:

@AbapCatalog.preserveKey: true

@Semantics.unitOfMeasure: true

@Semantics.quantity.unitOfMeasure: 'meins'

@Semantics.currencyCode: true

@Semantics.amount.currencyCode: 'waers'

@DefaultAggregation: #SUM

@Analytics.query: true

@OData.publish: true

  • 使用CDS View
  • Analytics Apps/BO:OData and BW connection

OData: CDS 视图可以通过@OData.publish: trueAnnotation将CDS View 公开为OData Service并由 Fiori APPs、Fiori KPI 、APF磁贴使用。

BW Connection:CDS视图可以设置为Analytical Query(@Analytics.query: true),基于 CDS 视图的Analytical Query的使用方式与 SAP BusinessObjects 应用程序中的 BW 查询相同(RSRT)。

在BI平台中,在OLAP连接中将S/4HANA系统设置为BW系统。

PS

1、不建议在升级到 Netweaver (NW)  Release 750 Support Package(SP) 4 之前启CDS for Analytics。(SAP Note:2361926)。

2、为了理解和使用CDS 视图,可以使用以下 2 个 Fiori 工具(Query Browser、View Browser)需要分配对应角色,并且在应用服务器配置对应的服务才能使用。

ABAP Program

附录1:名词解释

  • VDM

VDM:Virtual Data Models-虚拟数据模型。VDM是作为分析基础的预定义业务视图,由核心数据服务 (CDS) 视图创建。S/4 HANA OP 1610 提供超过 6,000 个 VDM,包括超过 150 个Analytical queries。

  • OData

OData: 开放数据协议(Open Data Protocol)是一个用于查询和更新数据的开放的互联网协议。该协议允许消费者经由HTTP协议向数据源查询,并以Atom、JSON或者plain XML等格式返回数据,可以对数据编码、排序或者过滤。

  • DDL

DDL:Data Definition Language-数据定义语言。如:CREATE、ALTER、DROP等。DDL主要是用在定义或改变表的结构,数据类型,表之间的链接和约束等初始化工作上

  • DML

DML:Data Manipulation Language-数据操作语言。如:SELECT、UPDATE、INSERT、DELETE。主要用来对数据库的数据进行一些操作。

  • DCL

DCL:Data Control Language-数据控制语言。如grant、deny、revoke、commit等。用来设置或更改数据库用户或角色权限的语句。

附录2:CDS相关URL

  • ABAP CDS Introduction

ABAP Core Data Services – Introduction (ABAP CDS view) | SAP Blogs

ABAP Core Data Services – Introduction (ABAP CDS view) | SAP Blogs

  • ABAP CDS Development User Guide

SAP Help Portal

  • ABAP CDS Analytical Query

Creating Custom CDS Views and Analytical Query – A step by step guide | SAP Blogs

  • Others

SAP Help Portal

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值