- 关于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?
- Code Push-Down: 为了充分发挥 HANA 的优势,有必要将应用层的进程转移到 DB 层。而CDS 视图是Push-Down的主要方式。
PS:
- SAP建议在 CDS 视图中而不是在 ABAP 应用程序服务器上实现逻辑。
- SAP建议创建基本的 CDS 视图并将它们用于许多程序/应用程序。
- ABAP CDS的特点
- CDS 视图完全基于 SQL。 集成“标准 SQL”功能,如连接、联合、内置函数等。
- 可以进行复杂的计算,例如 变量、层次结构和异常聚合等。通过在内部利用 BW OLAP 引擎。
- Analytical Query(CDS 视图的一种)可以像 BW 查询一样使用,例如 在事务 RSRT中运行。
- CDS 视图可以作为 OData 公开,并且可以在 Fiori 中使用。
- CDS 视图可以在ABAP 程序中访问,也可以作为数据源向BW 发送数据。
- 可通过注释在元模型级别上进行扩展。
- 使用 DCL 的 PFCG 授权对象可以进行授权控制。
- ABAP CDS的优点
- 通过Push-Down实现高性能。
- 能使用灵活的 SQL 表达式。
- 可以很容易地被应用程序使用(例如:OData、Fiori UI等)。
- 可重复用于许多流程。
- 使用ADT 创建CDS View
展开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 | |
Data type of arg | ||
INT1, INT2, INT4, INT8, DEC, CURR, QUAN without decimal places. | Data type arg1, where DEC, CURR and QUAN are implemented after INT4 | |
arg1, arg2: INT1, INT2, INT4, INT8, DEC, CURR, QUAN | 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. | |
Data type of arg for the integer types, else DEC without decimal places | ||
Data type of arg1 | ||
arg: INT1, INT2, INT4, INT8, DEC, CURR, QUAN | Data type arg: hereINT1 and INT2 are implemented after INT4. |
CDS 字符串内置函数
Demo CDS: demo_cds_sql_functions_string
Valid Argument Types | Result Type | |
See below | SSTRING if an argument has the type SSTRING, else CHAR with the length of the result. | |
arg1, arg2: see below | SSTRING if an argument has the type SSTRING, else CHAR with the length of the result. | |
arg: see below | ||
arg: see below | SSTRING if arg has the type SSTRING, else CHAR with length len | |
See below | ||
Data type of arg in the length of arg | ||
arg: see below | SSTRING if arg has the type SSTRING, else CHAR with length len | |
arg: see below | SSTRING if arg has the type SSTRING, else CHAR with the length of arg. | |
See below | SSTRING if arg1 or arg3 has the type SSTRING, else CHAR with the maximum possible length of the result. | |
arg: see below | SSTRING if arg has the type SSTRING, else CHAR with length len | |
arg: see below | SSTRING if arg has the type SSTRING, else CHAR with length len | |
arg: see below | SSTRING if arg has the type SSTRING, else CHAR with the length of arg. | |
arg: see below | SSTRING if arg has the type SSTRING, else CHAR or NUMCwith length of at lest len | |
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 | Field of a data source, parameter | |
source_currency | - | Source currency from column WAERS of database table TCURC | Field of a data source, literal, parameter | |
target_currency | - | Target currency from column WAERS of database table TCURC | Field of a data source, literal, parameter | |
exchange_rate_date | - | Exchange rate date for column GDATU of database table TCURR | 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 | |
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 | 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 | ||
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). | ||
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). | ||
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 |
CDS Annotations
对于使用 CDS 视图(如 ODATA、UI等)的应用程序,注释可用于进一步丰富元模型,超越 SQL。CDS Annotations有如下几种:
- Analytics Annotations:分析注解(Fiori Design Studio、Analysis Office)
- AnalyticsDetails Annotations: AnalyticsDetails 注解
- Consumption Annotations: 消费注解
- DefaultAggregation Annotations: 默认聚合注解
- EnterpriseSearch Annotations: EnterpriseSearch 注解(目前仅适用于 SAP 内部项目,而不适用于客户项目)
- Hierarchy Annotations: 层次注解
- ObjectModel Annotations: 对象模型注解
- OData Annotations:OData注解
- Search Annotations: 搜索注解
- Semantics Annotations:语义注解
- UI Annotations:UI注解
- 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
- ABAP CDS Analytical Query
Creating Custom CDS Views and Analytical Query – A step by step guide | SAP Blogs
- Others