Enhanced ABAP Development with Core Data Services (CDS)

How CDS Brings a Modern Data Modeling Approach to ABAP
前言:
With the advent of SAP HANA, a new data modeling infrastructure called core data services (CDS) has been introduced by SAP to simplify how developers define semantically rich data models. Read on to learn the concepts behind CDS and how it works in ABAP development processes, and walk through a step-by-step guide on using CDS with SAP’s development toolset.
实际案例:转自某网站。

Data models are a cornerstone of application development. They provide a standardized method for defining and formatting database contents consistently across systems, enabling different applications to share the same data — reducing development costs, speeding time to market, and improving quality and performance.

Those familiar with application development in the ABAP world are no strangers to the traditional data modeling tools included with SAP NetWeaver Application Server (SAP NetWeaver AS) ABAP — in particular, the ABAP data dictionary (DDIC), which stores definitions of objects, such as database tables and views, that can be used in ABAP programs. And then along came SAP HANA and the new paradigm of pushing down data-intensive logic to the database layer.

To take advantage of SAP HANA for application development, SAP introduced a new data modeling infrastructure known as core data services (CDS). With CDS, data models are defined and consumed on the database rather than on the server. CDS also offers capabilities beyond the traditional data modeling tools, including support for conceptual modeling and relationship definitions, built-in functions, and extensions. Originally, CDS was available only in the design-time and runtime environment of SAP HANA. Now, the CDS concept is also fully implemented in SAP NetWeaver AS ABAP, enabling developers to work in the ABAP layer with ABAP development tools while the code execution is pushed down to the database.

This article focuses on the ABAP implementation of CDS.1 It looks at the concepts behind CDS, and then walks you through a detailed example that demonstrates how CDS in ABAP works, and how CDS is supported during the ABAP development process by SAP’s ABAP development tools for Eclipse.

What Is CDS?

CDS is an infrastructure layer for defining semantically rich data models, which are represented as CDS views. In a very basic way, CDS allows developers to define entity types (such as orders, business partners, or products) and the semantic relationships between them, which correspond to foreign key relationships in traditional entity relationship (ER) models. CDS is defined using a SQL-based data definition language (DDL) that is based on standard SQL with some additional concepts, such as associations, which define the relationships between CDS views, and annotations, which direct the domain-specific use of CDS artifacts. Another example is expressions, which can be used in scenarios in which certain CDS attributes are considered as measures to be aggregated.

Similar to the role of the DDIC in the traditional ABAP world, data models based on CDS serve as central definitions that can be used in many different domains, such as transactional and analytical applications, to interact with data in the database in a unified way (see the sidebar "CDS in SAP Software" for more on how SAP itself is using CDS in its own applications). However, CDS data models go beyond the capabilities of the DDIC, which were typically limited to a transactional scope (think of traditional online transaction processing functionality). For example, in CDS, you can define views that aggregate and analyze data in a layered fashion, starting with basic views and then adding powerful views that combine the basic views. Another difference is the support for special operators such as UNION, which enables the combination of multiple select statements to return only one result set.

Originally, CDS was designed for native SAP HANA application development. It was introduced with support package stack (SPS) 06 for SAP HANA as part of SAP HANA extended application services (XS), an application server for SAP HANA that includes a runtime environment for native development. With SAP NetWeaver 7.4 SPS 05, the CDS concept was also fully implemented in SAP NetWeaver AS ABAP. While differences have evolved between the two variants — for example, SAP HANA-based CDS obviously operates on SAP HANA, while ABAP-based CDS operates on most major database platforms as well as SAP HANA, and each has a different type of repository for development objects — both variants pursue the same goal: to represent central data definitions as a common basis for application development of all kinds.

Each CDS variant is properly embedded into the particularities of the supported technology environment. With the ABAP variant, the integration of CDS artifacts into the ABAP stack is fairly straightforward. The artifacts are modeled and edited using the DDL editor tool within the Eclipse-based ABAP development tools for SAP NetWeaver (known as ABAP in Eclipse). CDS artifacts, like any other ABAP development object, are subject to the transport system within SAP NetWeaver AS ABAP so that they can easily be transported from a development system to quality assurance and production systems. CDS artifacts are stored in the DDIC and can be accessed in ABAP programs via Open SQL in the same manner as ordinary ABAP tables or views.

CDS in ABAP: A Step-by-Step Approach

Let’s see ABAP-based CDS in action in a real-world development scenario. Here, I’ll show you an example that demonstrates how to use CDS with SAP’s development toolset.

1. Start ABAP in Eclipse

First, you need to start ABAP in Eclipse, which is a plug-in tool for ABAP development that you install in your Eclipse workspace. (If you do not yet have a local installation of Eclipse, simply download the latest version — currently Mars — to your desktop.) SAP’s Eclipse-based plugins, including ABAP in Eclipse, are available from its tools update site (https://tools.hana.ondemand.com).

先安装Eclipse,注意版本问题。

*-----------------------------------------------------------------------------------------------------------------------------------------------

SAP Development Tools for Eclipse - Mars Software Repository

This software repository URL https://tools.hana.ondemand.com/mars provides access to SAP Development Tools for Eclipse Mars (4.5) Release.

It is intended to be used in the Help > Install New Software wizard of Eclipse. For more information about installing software, see the Eclipse Platform Help.

If you have any problems installing the software, please check the document SAP Development Tools for Eclipse Installation: FAQ and Known Issues.

The downloads are provided under the terms of the SAP DEVELOPER LICENSE AGREEMENT.

Back to SAP Development Tools for Eclipse home page.

*-----------------------------------------------------------------------------------------------------------------------------------------------

To start ABAP in Eclipse, open the Eclipse project explorer and switch to the ABAP development perspective. To begin development work, establish a connection to the ABAP back end by creating an ABAP project (in the example, M36_001_d002831_en). With this connection, you can create new ABAP repository artifacts, such as ABAP programs, classes, and, as in our example, CDS artifacts.

You can create these repository artifacts as local objects belonging to the $TMP package, but in this example we use an existing package called ZDEV201 to house the CDS artifact. Simply right-click on the package icon, and from the context menu select New > Other ABAP Repository Object (seeFigure 1).

Figure 1 — Create a new repository object

2. Create a New CDS View

In the New ABAP Repository Object pop-up, search for the DDL source editor and select it to launch the New DDL Source wizard (see Figure 2). First, specify the basic properties of the view. In the example — which is a simple scenario based on SAP’s well-known Enterprise Procurement Model (EPM) demo model — we want to count open invoices that have not yet been paid by business partners. So we specify the technical name (ZDDLS_E2E_INV_COUNT_201) and some short text to describe the view (“CDS view: open invoice count”). The underlying package (ZDEV201) is assigned automatically.

Figure 2 — Specify the basic properties of the view

Note that one of the fundamental differences between CDS in native SAP HANA and CDS in ABAP lies in the view definition. In both the ABAP and SAP HANA scenarios, you begin your development work by creating views on top of existing database tables that are contained in the DDIC. With CDS in native SAP HANA, you must create the basic entity types that correspond to the DDIC tables as part of the CDS view definition. With CDS in ABAP, you can simply refer to any underlying DDIC table, view, or type from within the CDS view definition, avoiding the need to “duplicate” the DDIC table definitions on the CDS layer. In the ABAP scenario, the CDS definitions are considered DDIC artifacts and need to be activated like any other DDIC artifact, and when changes are made, their impact is propagated to dependent artifacts.

3. Assign a Transport Request

On the next screen of the New DDL Source wizard, assign a transport request for the view (see Figure 3). After assigning the view’s DDL source to an existing ABAP package, either select an open transport request or create a new one, which you can use to keep track of the changes that make up your development tasks. This transport request can be bundled with tasks from other developers and later transported to a subsequent system for test and then production.

Figure 3 — Assign a transport request

There is no need to go to the lower levels of the underlying database (such as SAP HANA) to manage the transport of CDS artifacts. All changes and transports are managed in the ABAP layer end to end.

4. Select a Template for the CDS View

The last screen of the New DDL Source wizard (Figure 4) offers a selection of templates for creating a CDS view, including a display of the default syntax provided with each. There is a template for a basic view; templates for building views with more advanced features (such as joins, associations, and parameters); and a template for extending a view (by adding fields, for instance). For the example, we choose the basic view template.

Figure 4 — Select the basic view template

The templates include placeholders for code that you fill in step by step. If you later discover the need to change or extend the nature of your CDS view, you can always change the source code directly in the DDL editor and freely edit all parts of it as needed. This is particularly helpful if you want to copy portions of the source code from example code, for instance.

5. Complete the Coding for the View Definition

After selecting the template and clicking on Finish, you are ready to fill the placeholders provided by the template to complete the definition of our view ZDDLS_E2E_INV_COUNT_201 (see Figure 5). Open the DDL editor — which is a source code editor that offers very rich context-sensitive code completion capabilities — from the project perspective in the Eclipse project explorer via the context menu.

Figure 5 — Define the view in the DDL editor

First, specify an ABAP repository name for the view — ZV_INVCNT_201 in the example. The text label and view name are filled in automatically based on the information entered when specifying the basic properties of the view. Then specify the target data source for the invoice count. The underlying base table to be used as the target data source is named sepm_sddl_so_invoice_header (referenced as “inv”) but with the code completion functionality included with CDS, you don’t have to remember long names like these — you can simply start typing a prefix, such as sepm_sddl_so, and all matching base tables are displayed in a list, where you can view the details of each table (such as fields and their types) and select the table you want.

You can view the details of any of the syntactical elements that make up a CDS view definition on the fly by pressing the F2 key. The tooltip pop-up shows all the attributes, the corresponding data elements and types, and the associations (relationships) between the element and other tables or views, including their cardinality. For instance, in the example, the tooltip pop-up shows that the invoice header table contains a number of associations, including an association with a buyer table. With the tooltip functionality, there is no need to navigate deeply inside the DDIC — all of the relevant information is visible in one place.

6. Add Any Associations

For the view to count the number of open invoices, it needs to collect information about the buyer, such as the customer ID and name. To enable this, navigate along the association between the view’s underlying invoice header table and the buyer table, which is syntactically facilitated by a path expression separated by dots. To add the association to the view definition, you type a reference to the invoice header table (“inv”) followed by a dot (see Figure 6). A list of available attributes, including associations, appears once you have typed the dot.

Figure 6 — Add an association to a table to collect information

Select “buyer” to add the association to sepm_sddl_businesspartner, which is a view that encapsulates the buyer table, to the view definition, and then add another dot to display the columns available in the “buyer” view (see Figure 7). Add the columns you need to count the open invoices: business_partner_id, company_name, and address_key. Figure 8 shows the completed code.

Figure 7 — Define the association


Figure 8 — The completed view definition

7. Execute the Code and View the Results

With CDS, you can activate and test your code right away by pressing the F8 key, which immediately previews the results in the data browser (see Figure 9).

Figure 9 — The result set previewed in the data browser

Additional Options for Refining CDS Views

Let’s look at a few different ways you can fine-tune a CDS view to get the result set that best meets your needs.

Using Aggregation and Grouping

In the example scenario, the result set includes all business partners, not just the ones that have not yet paid their invoices. We can refine the results by manually adding a counter in the source code (see Figure 10).

Figure 10 — Add a counter to the view to refine the result set

The count function requires a group by clause, however, which is missing from our source code. Using the quick-fix capabilities of Eclipse, we can add the missing group by clause with a single click. Click on the highlighted select statement and choose Add Group by Clause, which automatically inserts thegroup by clause containing the elements you defined for the count (see Figure 11). To restrict the result set, we manually add a simple where clause that returns results with a payment status other than “P” for paid (see Figure 12).

双击前方的错误标记,或者使用快捷键填充Group By

Figure 11 — Add a group by clause to the counter


Figure 12 — Add a simple where clause to restrict the results

Now you can rerun the CDS view by pressing the F8 key, which reveals that the result set of the view is still raw — it contains address keys, which are GUIDs pointing to address records, when the actual city and street names would be more useful (see Figure 13).

Figure 13 — The result set returned by the modified view definition

Using View Hierarchies and Associations

To show the city and street names in the result set, instead of the GUIDs pointing to address records, define a second CDS view that declares an association to the addresses of the business partners.

First, create another view called Zddls_E2e_Cust_Classify_201 in the ZDEV201 package, as described previously (see Figure 14). Specify the basic properties of the view in the New DDL Source wizard, such as the technical name (ZDDLS_E2E_CUST_CLASSIFY_201) and some short text to describe the view (“CDS view: customer classification”).

Figure 14 — Create a second view to tailor the result set display

On the transport request screen (Figure 15), assign the newly created view to the open development request, and on the view template screen (Figure 16), choose Define View with Association. The syntax for this is slightly more complicated — the association contains an “on” condition, which is similar to a join operation.

Figure 15 — Assign a transport request for the second view


Figure 16 — Select the view with an association template

After selecting the template, open the DDL editor to complete the view definition. As with the first view, specify an ABAP repository name for the corresponding ABAP view (ZV_CUSTCLSFY_201 in the example). Again, the text label and view name are filled in automatically. Then specify a data source for the view, which is the first view created in this example (Zddls_E2e_Inv_Count_201), along with a target data source for the association created by the template. As before, pick a data source with the aid of the code completion functionality by typing the prefix sepm_sddl and selecting sepm_sddl_address (referenced as “_address”) from the matching items (seeFigure 17).

Figure 17 — Define the view and its association in the DDL editor

Next, fill out the “on” condition defining the source and target columns that are connected by the association. For the source, specify the first view Zddls_E2e_Inv_Count_201 and select address_key from the displayed list of available columns (see Figure 18). For the target, specify the target data source (referenced as “_address”) for the association and select the corresponding address_key from the displayed list of available columns (seeFigure 19).

Figure 18 — Define the source for the association


Figure 19 — Define the target for the association

Using View Fields and Expressions

The next step is to define the view fields. The system tells you that the address_key column needs to be part of the view’s signature (because it is the link between the two views), so quickly add it by typing the prefix “ad” and using the code completion functionality (see Figure 20). Likewise, add customer ID and customer name. Figure 21 shows the completed definition of the view fields for the association.

Figure 20 — Define the view fields for the association


Figure 21 — The completed definition of the view fields for the association

Finally, to demonstrate the versatility of CDS, let’s add a calculated field that is based on conditions that need to be met. To define this field, manually add a case expression (see Figure 22). The completed case expression for the example (Figure 23) delivers a category field that assigns a certain value depending on the number of open invoices: “C” for less than 5,000; “D” for between 5,000 and 10,000; and “S” for more than 10,000.

Figure 22— Add a case expression to enable a calculated field

Figure 23 — The completed case expression

Lastly, add view fields that contain address information by specifying the target data source — “_address” (see Figure 24) — and then selecting country, street, city, and postal code from the list of available columns (see Figure 25).

Figure 24 — Specify the data source for the view fields

Figure 25 — Specify the view fields to display in the result set

Figure 26 shows the completed code.

Figure 26 — The refined view definition

Viewing the Refined Results

Activate and run the view, which displays the results shown in Figure 27. As you can see, all of the refinements are reflected in the data browser.

Figure 27 — The result set returned by the refined view definition

The association details defined in the DDL editor are also integrated into the data browser. As in the DDL editor, you can navigate along the various associations, meaning that you can select a row and view a list of associations, such as address (see Figure 28). Selecting an association will display the resulting record that is referred to by the association (see Figure 29), which can be used for testing and analysis.

Figure 28 — Navigating along an association in the data browser


Figure 29 — Displaying the record referred to by the association in the data browser

Figure 30 shows our completed CDS artifacts in the Eclipse project explorer.

Figure 30 — The completed CDS artifacts in the Eclipse project explorer

Summary

SAP NetWeaver 7.4 brings the CDS concept introduced for native SAP HANA development to the ABAP development world. Powerful CDS functions such as associations and annotations are fully integrated into ABAP in Eclipse, and assist developers at each step of the definition process. And since CDS is executed at runtime directly on the database layer, it facilitates the code pushdown paradigm pioneered by SAP HANA right from the beginning. With these features on hand — along with the CDS enhancements included in the upcoming 7.5 release of SAP NetWeaver2 — developers will have the tools they need to succeed in a modern business landscape.

Shortcut Key


Create By :Xiuxianhai

Enhanced ABAP Development with Core Data Services (CDS)相关推荐

  1. 在CDS(Core Data Services)中使用DCL(Data Control Language)

    最近,我在玩ABAP CDS视图,并且遇到了一些权限方面的挑战.我在网上没看到有多少有关CDS开发的文档,因为它是个相当新的东西.因此,我决定写下这篇博客,也许我的想法可以帮助到一些人. 和你已经意识 ...

  2. 关于Core Data的一些整理(一)

    关于Core Data的一些整理(一) 在Xcode7.2中只有Mast-Debug和Single View中可以勾选Use Core Data 如果勾选了Use Core Data,Xcode会自动 ...

  3. IBM中国开发中心吉燕勇: 通过Cloud Data Services打造新型认知计算数据分析云平台...

    在今年初IBM首席执行官Ginni Rometty公开表示将向认知计算与云计算平台方向转型之后,尽管外界一致认为这一过程并不会轻松,但经过近半年时间的努力,IBM确实已取得了一些进展,尤其是在中国国内 ...

  4. SAP ABAP Development Tool 提高开发效率的十个小技巧

    这是 Jerry 2021 年的第 46 篇文章,也是汪子熙公众号总共第 323 篇原创文章. Jerry 已经有很长一阵子没有打开工作电脑上的 SAP ABAP Development Tool 了 ...

  5. ABAP Development Tool 代码模板和其他一些实用技巧汇总

    这是 Jerry 2021 年的第 35 篇文章,也是汪子熙公众号总共第 311 篇原创文章. Jerry 已经告别 ABAP 开发很长一段时间了,最近有朋友问我一个关于 ABAP Developme ...

  6. 集群环境下,你不得不注意的ASP.NET Core Data Protection 机制

    引言 最近线上环境遇到一个问题,就是ASP.NET Core Web应用在单个容器使用正常,扩展多个容器无法访问的问题.查看容器日志,发现以下异常: System.Security.Cryptogra ...

  7. Core Data 学习笔记(二)被管理对象模型

    为什么80%的码农都做不了架构师?>>>    目录 Core Data 学习笔记(一)框架简介 Core Data 学习笔记(二)被管理对象模型 Core Data 学习笔记(三) ...

  8. core data使用教程

    core data使用教程 从印象中记得还是在学校的时候老师讲过的时候用过,那时觉得好难,以至于工作2年多了一直没敢去看core data,前几天想了下,不去看不行,得都会用才行,于是那天6点下班后就 ...

  9. iOS Core Data

    基本概念 当前,各类应用开发中只要牵扯到数据库操作通常都会用到一个概念"对象关系映射(ORM)".例如在Java平台使用Hibernate,在.NET平台使用Entity Fram ...

最新文章

  1. Linux下的Shell编程(2)环境变量和局部变量
  2. Redis以及Redis的php扩展安装无错版
  3. matlab产生一列相同的数据,读取excel中的数据把第一列相同的所有行数据输出成一个excel...
  4. 03-对图像进行数值计算和加权融合
  5. RedHat Linux 7.3基础环境搭建
  6. python自动化运维书籍推荐_《Python 自动化运维:技术与最佳实践》
  7. [转载] 【Java】将一个字符串的字符排序,按ASCII表的顺序从小到大
  8. qt调用外部程序(exe)
  9. 初识:如何实现利用C语言代码封装成可以安装的windows软件?
  10. java cache system_JCS(Java Cache System)基本结构分析和使用
  11. 库存收藏-各种设备默认用户名和密码
  12. lumion材质系统室内渲染6.3
  13. IPhone 日历提醒
  14. 新浪云python开发_python,flask,SAE(新浪云),搭建开发微信公众账号
  15. mysql grant proxy on_mysql grant配置授权
  16. JAVA中的各种引用
  17. ADS1258使用START注意的事项
  18. Magisk工具使用指南
  19. Group BOM 和BOM group
  20. POJ-1190 生日蛋糕(dfs+剪枝)

热门文章

  1. 2022年初,给5年内还想做产品经理的提个醒!
  2. 中国互联网的抑郁:抄与被抄都很痛
  3. 如何快速高效读懂1本书
  4. 将数据转化为API,OpenDataSoft获540万美元A轮融资
  5. 更合理的 setState()
  6. JS 变量的数据类型转换
  7. tomcat下类加载顺序
  8. 修改MySQL字段为首字母大写
  9. C#遍历文件读取Word内容以及使用BackgroundWorker对象打造平滑进度条
  10. 春节英语祝福【中英文对照】