Technical Articles
An example to call PAL Apriori via AMDP
Introduction
SAP Predictive Analysis Library (PAL) is delivered with SAP HANA. This application function library (AFL) defines functions that can be called from within SAP HANA SQLScript procedures to perform analytic algorithms.
ABAP-based SAP applications are able call the functions (Classification, Regression, Clustering, Association Rule, Recommender Systems, etc.) provided by PAL. One common way is to do it via AMDP.
ABAP-Managed Database Procedures (AMDP) is one of the recommended patterns for use in ABAP code optimization within the context of ABAP development on SAP HANA.
Step-by-Step Example
Let’s use an example to show how that is done. The PAL function used is Apriori.
Step 1 (Optional). Familiarize yourself with PAL function using SQLScript
If you are already familiar with PAL’s HANA procedure interface and how to call it, you may skip this step.
Connect to the HANA Database via HANA Studio. Run the following script:
SET SCHEMA ZHAOJE; DROP TABLE PAL_APRIORI_PARAMETER_TBL; DROP TABLE PAL_APRIORI_TRANS_TBL; CALL _SYS_AFL.PAL_APRIORI(PAL_APRIORI_TRANS_TBL, PAL_APRIORI_PARAMETER_TBL, ?, ?); |
You’ll see the result of mined rules as below:
Step 2. Write AMDP Code to Call PAL procedure
Below is an example class of AMDP. It calls the PAL Apriori procedure. You could edit your own AMDP code in ADT (ABAP Development Tools)
CLASS zcl_amdp_pal DEFINITION PUBLIC FINAL CREATE PUBLIC .PUBLIC SECTION. INTERFACES if_amdp_marker_hdb.TYPES: BEGIN OF ty_apdata, customer TYPE i, item TYPE C LENGTH 10, END OF ty_apdata, tt_apdata TYPE STANDARD TABLE OF ty_apdata, BEGIN OF ty_apparams, ty_metric TYPE p LENGTH 5 DECIMALS 4, BEGIN OF ty_appmml, METHODS apriori_proc_call PROTECTED SECTION. CLASS zcl_amdp_pal IMPLEMENTATION. METHOD apriori_proc_call BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT. ENDCLASS. |
Step 3
Write an ABAP program to call the AMDP method.
*&———————————————————————* *& Report ZZ_APRIORI_TEST *&———————————————————————* *& *&———————————————————————* REPORT ZZ_APRIORI_TEST.DATA: lt_data TYPE zcl_amdp_pal=>tt_apdata, ls_data LIKE LINE OF lt_data, lt_param TYPE zcl_amdp_pal=>tt_apparams, ls_param LIKE LINE OF lt_param, lt_rules TYPE zcl_amdp_pal=>tt_aprules, lt_pmml TYPE zcl_amdp_pal=>tt_appmml, lr_wrapper TYPE REF TO zcl_amdp_pal. ls_data-customer = 2. ls_data-item = ‘item2’. APPEND ls_data TO lt_data. CLEAR ls_param. ls_param-name = ‘THREAD_NUMBER’. ls_param-intargs = 2. APPEND ls_param TO lt_param. CREATE OBJECT lr_wrapper. TRY. |
After successful execution, you’ll see the following result in ABAP.
The result in ABAP can be used by the corresponding applications.
This is really great , Thanks for sharing this stuff , we are touching ML from any of the end 🙂