通过AMDP调用HANA的PAL函数

SAP预测分析库(SAP Predictive Analysis Library,PAL)是SAP HANA中的一项功能,它允许我们在SAP HANA SQLScript过程中执行分析算法。

基于ABAP的SAP应用可以调用PAL提供的功能,包含分类,回归,聚类,关联规则,社交网络分析,推荐系统等。通常使用AMDP来实现调用。

AMDP(ABAP-Managed Database Procedures)是一种在SAP HANA中进行ABAP开发时可以使用的代码优化模式,简而言之,它可以让开发者在ABAP中写HANA数据库存储过程。

 

本文链接:https://www.cnblogs.com/hhelibeb/p/12610644.html

英文原文:An example to call PAL Apriori via AMDP

示例

接下来用一个例子来展示如何使用PAL。这里用到的PAL函数是Apriori。

步骤一(可选) 熟悉使用SQLScript调用PAL函数

如果你已经熟悉PAL的HANA存储过程接口和它的调用,可以跳过这步。

通过HANA Studio连接HANA数据库,运行下面的脚本:

SET SCHEMA ZHAOJE;

DROP TABLE PAL_APRIORI_PARAMETER_TBL;
CREATE COLUMN TABLE PAL_APRIORI_PARAMETER_TBL (
“PARAM_NAME ” VARCHAR(100),
“INT_VALUE” INTEGER,
“DOUBLE_VALUE” DOUBLE,
“STRING_VALUE” VARCHAR (100)
);
INSERT INTO PAL_APRIORI_PARAMETER_TBL VALUES (‘MIN_SUPPORT’, null, 0.1, null);
INSERT INTO PAL_APRIORI_PARAMETER_TBL VALUES (‘MIN_CONFIDENCE’, null, 0.3, null);
INSERT INTO PAL_APRIORI_PARAMETER_TBL VALUES (‘MIN_LIFT’, null, 1.1, null);
INSERT INTO PAL_APRIORI_PARAMETER_TBL VALUES (‘MAX_CONSEQUENT’, 1, null, null);
INSERT INTO PAL_APRIORI_PARAMETER_TBL VALUES (‘PMML_EXPORT’, 1, null, null);

DROP TABLE PAL_APRIORI_TRANS_TBL;
CREATE COLUMN TABLE PAL_APRIORI_TRANS_TBL (
“CUSTOMER” INTEGER,
“ITEM” VARCHAR(20)
);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (2, ‘item2’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (2, ‘item3’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (3, ‘item1’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (3, ‘item2’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (3, ‘item4’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (4, ‘item1’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (4, ‘item3’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (5, ‘item2’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (5, ‘item3’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (6, ‘item1’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (6, ‘item3’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (0, ‘item1’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (0, ‘item2’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (0, ‘item5’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (1, ‘item2’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (1, ‘item4’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (7, ‘item1’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (7, ‘item2’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (7, ‘item3’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (7, ‘item5’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (8, ‘item1’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (8, ‘item2’);
INSERT INTO PAL_APRIORI_TRANS_TBL VALUES (8, ‘item3’);

CALL _SYS_AFL.PAL_APRIORI(PAL_APRIORI_TRANS_TBL, PAL_APRIORI_PARAMETER_TBL, ?, ?);

你会看到下面的挖掘结果,

通过AMDP调用HANA的PAL函数_第1张图片

 步骤二 写AMDP代码,调用PAL过程

下面是一个AMDP类例子,它调用了PAL Apriori过程。你可以在Eclipse里编辑自己的AMDP代码。

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,
           name       TYPE c LENGTH 60,
           intargs    TYPE i,
           doubleargs TYPE float,
           stringargs TYPE c LENGTH 100,
         END OF ty_apparams,
         tt_apparams TYPE STANDARD TABLE OF ty_apparams,

         ty_metric   TYPE p LENGTH 5 DECIMALS 4,
         BEGIN OF ty_aprules,
           antecedent TYPE c LENGTH 20,
           consequent TYPE c LENGTH 10,
           support    TYPE ty_metric,
           confidence TYPE ty_metric,
           lift       TYPE ty_metric,
         END OF ty_aprules,
         tt_aprules TYPE STANDARD TABLE OF ty_aprules,

         BEGIN OF ty_appmml,
           row_index     TYPE i,
           model_content TYPE c LENGTH 500,
         END OF ty_appmml,
         tt_appmml TYPE STANDARD TABLE OF ty_appmml.

  METHODS apriori_proc_call
    IMPORTING
      VALUE(it_vapdata)  TYPE tt_apdata
      VALUE(it_apparams) TYPE tt_apparams
    EXPORTING
      VALUE(et_ap_rules) TYPE tt_aprules
      VALUE(et_ap_pmml)  TYPE tt_appmml.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS zcl_amdp_pal IMPLEMENTATION.

  METHOD apriori_proc_call BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT.
    CALL _SYS_AFL.PAL_APRIORI(:it_vapdata, :it_apparams, et_ap_rules, et_ap_pmml);
  ENDMETHOD.

ENDCLASS.

步骤三 在ABAP程序中调用AMDP方法

示例报表代码如下,

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.
ls_data-customer = 2. ls_data-item = 'item3'. APPEND ls_data TO lt_data.
ls_data-customer = 3. ls_data-item = 'item1'. APPEND ls_data TO lt_data.
ls_data-customer = 3. ls_data-item = 'item2'. APPEND ls_data TO lt_data.
ls_data-customer = 3. ls_data-item = 'item4'. APPEND ls_data TO lt_data.
ls_data-customer = 4. ls_data-item = 'item1'. APPEND ls_data TO lt_data.
ls_data-customer = 4. ls_data-item = 'item3'. APPEND ls_data TO lt_data.
ls_data-customer = 5. ls_data-item = 'item2'. APPEND ls_data TO lt_data.
ls_data-customer = 5. ls_data-item = 'item3'. APPEND ls_data TO lt_data.
ls_data-customer = 6. ls_data-item = 'item1'. APPEND ls_data TO lt_data.
ls_data-customer = 6. ls_data-item = 'item3'. APPEND ls_data TO lt_data.
ls_data-customer = 0. ls_data-item = 'item1'. APPEND ls_data TO lt_data.
ls_data-customer = 0. ls_data-item = 'item2'. APPEND ls_data TO lt_data.
ls_data-customer = 0. ls_data-item = 'item5'. APPEND ls_data TO lt_data.
ls_data-customer = 1. ls_data-item = 'item2'. APPEND ls_data TO lt_data.
ls_data-customer = 1. ls_data-item = 'item4'. APPEND ls_data TO lt_data.
ls_data-customer = 7. ls_data-item = 'item1'. APPEND ls_data TO lt_data.
ls_data-customer = 7. ls_data-item = 'item2'. APPEND ls_data TO lt_data.
ls_data-customer = 7. ls_data-item = 'item3'. APPEND ls_data TO lt_data.
ls_data-customer = 7. ls_data-item = 'item5'. APPEND ls_data TO lt_data.
ls_data-customer = 8. ls_data-item = 'item1'. APPEND ls_data TO lt_data.
ls_data-customer = 8. ls_data-item = 'item2'. APPEND ls_data TO lt_data.
ls_data-customer = 8. ls_data-item = 'item3'. APPEND ls_data TO lt_data.

CLEAR ls_param. ls_param-name = 'THREAD_NUMBER'. ls_param-intargs = 2. APPEND ls_param TO lt_param.
CLEAR ls_param. ls_param-name = 'MIN_SUPPORT'. ls_param-doubleargs = '0.1'. APPEND ls_param TO lt_param.
CLEAR ls_param. ls_param-name = 'MIN_CONFIDENCE'. ls_param-doubleargs = '0.3'. APPEND ls_param TO lt_param.
CLEAR ls_param. ls_param-name = 'MIN_LIFT'. ls_param-doubleargs = '1.1'. APPEND ls_param TO lt_param.
CLEAR ls_param. ls_param-name = 'MAX_CONSEQUENT'. ls_param-intargs = 1. APPEND ls_param TO lt_param.

CREATE OBJECT lr_wrapper.
CALL METHOD lr_wrapper->apriori_proc_call
  EXPORTING
    it_vapdata  = lt_data
    it_apparams = lt_param
  IMPORTING
    et_ap_rules = lt_rules
    et_ap_pmml  = lt_pmml.

TRY.
    cl_salv_table=>factory(
    IMPORTING
    r_salv_table = DATA(lr_table)
    CHANGING
    t_table = lt_rules ).
    DATA(lr_functions) = lr_table->get_functions( ).
    lr_functions->set_default( abap_true ).
    DATA(lr_columns) = lr_table->get_columns( ).
    DATA(lr_column_1) = lr_columns->get_column('ANTECEDENT').
    lr_column_1->set_long_text('ANTECEDENT'). lr_column_1->set_medium_text('ANTECEDENT'). lr_column_1->set_short_text('ANTECEDENT').
    DATA(lr_column_2) = lr_columns->get_column('CONSEQUENT').
    lr_column_2->set_long_text('CONSEQUENT'). lr_column_2->set_medium_text('CONSEQUENT'). lr_column_2->set_short_text('CONSEQUENT').
    DATA(lr_column_3) = lr_columns->get_column('SUPPORT').
    lr_column_3->set_long_text('SUPPORT' ). lr_column_3->set_medium_text('SUPPORT'). lr_column_3->set_short_text('SUPPORT').
    DATA(lr_column_4) = lr_columns->get_column('CONFIDENCE').
    lr_column_4->set_long_text('CONFIDENCE'). lr_column_4->set_medium_text('CONFIDENCE'). lr_column_4->set_short_text('CONFIDENCE').
    DATA(lr_column_5) = lr_columns->get_column('LIFT').
    lr_column_5->set_long_text('LIFT'). lr_column_5->set_medium_text('LIFT'). lr_column_5->set_short_text('LIFT').
    lr_table->display( ).
  CATCH cx_salv_msg.                                    "#EC NO_HANDLER
  CATCH cx_salv_not_found.                              "#EC NO_HANDLER
ENDTRY.

成功执行后,可以看到如下的执行结果:

通过AMDP调用HANA的PAL函数_第2张图片

 

 可以在相关的应用中使用这些结果。

 
 参考资料: SAP HANA Predictive Analysis Library (PAL)
 
 
 

你可能感兴趣的:(通过AMDP调用HANA的PAL函数)