Quantcast
Channel: SCN : All Content - SAP HANA Cloud Platform Developer Center
Viewing all articles
Browse latest Browse all 3285

Using Predictive Analysis Library (PAL) in SAP HANA Cloud Platform

$
0
0

Disclaimer:This document relates to Beta functionality available on SAP HANA Cloud Platform trial landscape.


Predictive Analysis is "... the process of discovering meaningful new correlations, patterns and trends by sifting through large amounts of data stored in repositories, using pattern recognition technologies as well as statistical and mathematical techniques."Gartner Group
SAP Predictive Analysis Library (PAL) is an add-on set of application functions that implement a wide range of analysis algorithms in the areas of clustering, classification, association, etc. It comes natively with very high performance because the complex and heavy analytic computations are executed directly into the DB instead of being brought up to the application server.

 

The PAL functionality is now available for free for TRIAL users of SAP HANA Cloud Platform. You just need an account and an Eclipse IDE in order to get a direct hands-on of how to embed predictive analytics in your business application.

 

In this document we will illustrate how PAL can be used in SAP HANA Cloud Platform based on the popular Enterprise Procurement Model reference application. To see the specifics of using PAL in SAP HANA Cloud Platform environment compared to the standard usage scenario described in the PAL Documentation, check the SAP HANA Cloud Platform Help.

 

Prerequisites

 

Example

In this example we apply the ABC Analysis function from PAL to classify our partner companies based on their importance in terms of gross amount of sales orders. As input we use the sample data from the Enterprise Procurement Model application, which comes pre-delivered on SAP HANA Cloud Platform. The algorithm will put the companies into three categories:

  • "A" partners - 20% of the companies accountable for 70% of the sales orders amount
  • "B" partners - 30% of the companies accountable for 20% of the sales orders amount
  • "C" partners - 50% of the companies accountable for 10% of the sales orders amount

 

Note: In the example is used a sample account named: p123456trial and a sample trial instance named: sample. Having this setup <your_package> is replaced with p123456trial.sample.

 

Step 1: Create an XS Project

project.png

Step 2: Prepare the Data

The ABC Analysis example in the PAL Documentation uses a manually created and filled data table, which has just the right format to be used as input. Business data, however, typically comes scattered across various DB tables and needs to be properly aggregated before being passed to the analytic function. In our case the necessary data is stored in SNWD_SO and SNWD_BPA tables from the EPMSAMPLEDATA schema -  the former is the raw sales orders data and the latter is master data containing company details.

  • Create a calculation view SO_CV in <your_package>.paldemo. Example: p123456trial.sample.paldemo
  • Join SNWD_SO and SNWD_BPA (cardinality n:1) on ON SNWD_SO. "BUYER_GUID" = SNWD_BPA."NODE_KEY":

SO_CV_Join.png

  • Add columns GROSS_AMOUNT and COMPANY_NAME to the output

CO_CV_add_to _output.png

  • Remove client filtering (choose Cross Client), and analytic privileges

CO_CV_cross_client.png

  • Activate the SO_CV calculation view
  • In order to be able to preview the activated view you shall execute the following call in the SQL editor
CALL "HCP"."HCP_GRANT_SELECT_ON_ACTIVATED_OBJECTS";
  • Refresh your Catalog folder and now you should see the _SYS_BIC schema and your calculation view inside. You can make a data preview to check that everything is OK

SO_CV_data_preview.png

 

Step 3: Generate the PAL wrapper procedure

Once we have the input data in appropriate format we should generate a wrapper procedure for the ABC Analysis function. In the next few lines we define the metadata of this wrapper procedure - its input format (PAL_ABC_DATA_VIEW_T), control table format (TYPE PAL_CONTROL_T) and its resulting table format (PAL_ABC_RESULT_T), and put this metadata in PAL_ABC_PDATA_TBL. Finally we generate the wrapper procedure by calling "HCP"."HCP_AFL_WRAPPER_GENERATOR" with the desired name of the wrapper, the AFLPAL area and the metadata table.

As a result we have "_SYS_AFL"."<your NEO_xxx schema>_PAL_ABC" wrapper procedure for the ABC Analysis PAL function.

  • To find <your NEO_xxx schema>, in SQL editor execute
SELECT SCHEMA_NAME FROM "HCP"."HCP_DEV_METADATA"
  • To generate the PAL wrapper procedure, in SQL editor execute
SET SCHEMA "<your NEO_xxx schema>";
CREATE TYPE PAL_ABC_DATA_VIEW_T AS TABLE ("COMPANY_NAME" VARCHAR(100),
"GROSS_AMOUNT" DOUBLE);
CREATE TYPE PAL_CONTROL_T AS TABLE("Name" VARCHAR(100), "intArgs" INT,
"doubleArgs" DOUBLE, "strArgs" VARCHAR(100));
CREATE TYPE PAL_ABC_RESULT_T AS TABLE("ABC" VARCHAR(10),
"COMPANY_NAME" VARCHAR(100));
CREATE COLUMN TABLE PAL_ABC_PDATA_TBL("ID" INT, "TYPENAME" VARCHAR(100),
"DIRECTION" VARCHAR(100));
INSERT INTO PAL_ABC_PDATA_TBL VALUES (1,'<your NEO_xxx schema>.PAL_ABC_DATA_VIEW_T', 'in');
INSERT INTO PAL_ABC_PDATA_TBL VALUES (2,'<your NEO_xxx schema>.PAL_CONTROL_T', 'in');
INSERT INTO PAL_ABC_PDATA_TBL VALUES (3,'<your NEO_xxx schema>.PAL_ABC_RESULT_T','out');
CALL "HCP"."HCP_AFL_WRAPPER_GENERATOR"('PAL_ABC','AFLPAL', 'ABC', PAL_ABC_PDATA_TBL);


Step 4: Prepare configuration data of the generated PAL wrapper procedure

We create a control table PAL_CONTROL_TBL with the format specified in the previous step and fill it with the specific configuration for the ABC Analysis function. Then we also create the result table PAL_ABC_RESULT_TBL which will hold the results.

  • Create folder paldemo\data and file PAL_DEMO_TABLES.hdbdd with the following contents

namespace <your_package>.paldemo.data;

 

 

@Schema: '_SYS_BIC'

context PAL_DEMO_TABLES {

 

  @Catalog.tableType : #COLUMN

  Entity PAL_ABC_RESULT_TBL {

      key ABC: String(10);

      key COMPANY_NAME: String(100);

  };

  @Catalog.tableType : #COLUMN

  Entity PAL_CONTROL_TBL {

      key Name: String(100);

      intArgs: Integer null;

      doubleArgs: Decimal(15,2) null;

      strArgs: String(100) null;

  };

 

  type ABC_RESULTS {

      ABC: String(10);

      COMPANY_NAME: String(100);

      GROSS_AMOUNT: Decimal(15,2);

  };

};

  • Create folder paldemo\data\loads and file PAL_DEMO_TABLES.hdbti with the following contents

import = [{

  schema = "_SYS_BIC";

  cdstable = "<your_package>.paldemo.data::PAL_DEMO_TABLES.PAL_CONTROL_TBL";

  file = "<your_package>.paldemo.data.loads:pal_control.csv";

  header = false;

}];

  • In folder paldemo\data\loads create file pal_control.csv with the following contents

THREAD_NUMBER,1,,

PERCENT_A,,0.7,

PERCENT_B,,0.2,

PERCENT_C,,0.1,

  • Create folder paldemo\procedures and file GET_PAL_ABC.hdbprocedure with the following contents


PROCEDURE "_SYS_BIC"."<your_package>.paldemo.procedures::GET_PAL_ABC" (
OUT result "_SYS_BIC"."<your_package>.paldemo.data::PAL_DEMO_TABLES.ABC_RESULTS")  LANGUAGE SQLSCRIPT  SQL SECURITY INVOKER  DEFAULT SCHEMA "_SYS_BIC"  AS
BEGIN
so_cv = select * from "_SYS_BIC"."<your_package>.paldemo/SO_CV";
control_tbl = select * from
"_SYS_BIC"."<your_package>.paldemo.data::PAL_DEMO_TABLES.PAL_CONTROL_TBL";
CALL "_SYS_AFL"."<your NEO_xxx schema>_PAL_ABC" (:so_cv, :control_tbl, abc_result );
result = SELECT R.ABC, R."COMPANY_NAME", T."GROSS_AMOUNT"  FROM "_SYS_BIC"."<your_package>.paldemo/SO_CV" AS T  FULL JOIN  :abc_result AS R
ON T."COMPANY_NAME" = R."COMPANY_NAME"
ORDER BY T."GROSS_AMOUNT" DESC;
END;
  • To apply the changes use the Team -> Activate menu over the paldemo project

hdbdd_save.png

  • In order to be able to preview the activated artifacts in the catalog you shall execute again HCP_GRANT_SELECT_ON_ACTIVATED_OBJECTS procedure
CALL "HCP"."HCP_GRANT_SELECT_ON_ACTIVATED_OBJECTS";


Step 5: Create XSJS service using the generated PAL wrapper procedure

In our XSJS service we want to call the generated PAL wrapper procedure. It will use the configured ABC analysis to split the information from SO_CV view into three categories A,B,and C. From the PAL_ABC_RESULT_TBL table we get the results, and construct a JSON that can be visualized by SAPUI5.

  • Create folder paldemo\services and file partner_analysis.xsjs with the following contents
var CALL_GET_ABC_RESULTS = "  call \"_SYS_BIC\".\"<your_package>.paldemo.procedures::GET_PAL_ABC\" (?)";
var connection;
function readData() {  var statement = null;  var queryResultSet = null;  var data = {  "A" : [],  "B" : [],  "C" : []  };  try {      statement = connection.prepareCall(CALL_GET_ABC_RESULTS);      statement.execute();      queryResultSet = statement.getResultSet();      var groupId;      while (queryResultSet.next()) {            groupId = queryResultSet.getString(1);            data[groupId].push({                                          groupId : groupId,                                          companyName : queryResultSet.getString(2),                                          grossAmount : queryResultSet.getDouble(3)            });      }  } finally {
queryResultSet.close();
statement.close();
}  return data;
}
function doGet() {  var result;  try {      connection = $.db.getConnection();      result = readData();  } finally {
connection.close();
}  $.response.contentType = "application/json";  $.response.setBody(JSON.stringify(result));
}
doGet();


Step 6: Create SAPUI5 visualization of ABC analysis

The following SAPUI5 consumes JSON result from the XSJS service, and uses three pie charts for visualization. The index.html simply shows the partner_analysis view.

  • Create folder paldemo\views and file partner_analysis.view.js with the following contents
sap.ui.jsview("views.partner_analysis", {  oBarChart : undefined,  getControllerName : function() {return null;},  createDonutControl : function(height, label, dataset) {  return new sap.viz.ui5.Donut({            width : "100%",            height : height,            title : {              visible : true,              text : label            },            dataset : dataset          });  },  createDataset : function(measureTitle, dataRootPath) {  return new sap.viz.ui5.data.FlattenedDataset({            dimensions : [ {              axis : 1,              name : 'Company Name',              value : "{companyName}"            } ],            measures : [ {              name : measureTitle,              value : '{grossAmount}'            } ],            data : {              path : dataRootPath            }          });  },  buildLayout : function() {  var oLayout = new sap.ui.commons.layout.MatrixLayout({            id : 'matrix3',            layoutFixed : true,            columns : 3,            width : '100%'        });    var oRow = new sap.ui.commons.layout.MatrixLayoutRow({id : 'Row-0', width : '100%' });    oLayout.addRow(oRow);    var oCell1 = new sap.ui.commons.layout.MatrixLayoutCell({id : 'Cell-0-0', width : '33%' });    oCell1.addContent(this.donutA);    oRow.addCell(oCell1);    oCell2 = new sap.ui.commons.layout.MatrixLayoutCell({id : 'Cell-0-2', width : '33%' });    oCell2.addContent(this.donutB);    oRow.addCell(oCell2);    oCell3 = new sap.ui.commons.layout.MatrixLayoutCell({ id : 'Cell-0-3',  width : '33%' });    oCell3.addContent(this.donutC);    oRow.addCell(oCell3);    return oLayout;  },  createContent : function(oController) {  var analModel = new sap.ui.model.json.JSONModel();  analModel.loadData("services/partner_analysis.xsjs");  var datasetA  = this.createDataset('A', '/A');  this.donutA = this.createDonutControl("400px", 'A - 70% revenue customers', datasetA);  this.donutA.setModel(analModel);  var datasetB  = this.createDataset('B', '/B');  this.donutB = this.createDonutControl("300px", 'B - 20% revenue customers', datasetB);  this.donutB.setModel(analModel);  var datasetC  = this.createDataset('C', '/C');  this.donutC = this.createDonutControl("250px", 'C - 10% revenue customers', datasetC);  this.donutC.setModel(analModel);  return this.buildLayout();  }
});
  • In folder paldemo create file index.html with the following contents
<html><head><meta http-equiv="X-UA-Compatible" content="IE=edge"><script src="/sap/ui5/1/resources/sap-ui-core.js" id="sap-ui-bootstrap"  data-sap-ui-libs="sap.ui.ux3,sap.ui.commons,sap.ui.table,sap.viz"  data-sap-ui-theme="sap_goldreflection"></script><script>  sap.ui.localResources("views");  var view = sap.ui.view({      id : "companies",      viewName : "views.partner_analysis",      type : sap.ui.core.mvc.ViewType.JS  });  view.placeAt("content");</script></head><body class="sapUiBody" role="application">      <div id="content"></div></body></html>


Step 7: Authorize users to access project's artifacts

In the paldemo project we created different artifacts like views, tables, and procedures. Business users who will analyse data, e.g. uses these artifacts have to be authorized to access them. This is done with the model_access.hdbrole file, described as follows.

  • Create folder paldemo\roles and file model_access.hdbrole with the following contents

role <your_package>.paldemo.roles::model_access {

  sql object <your_package>.paldemo:SO_CV.calculationview : SELECT;

  sql object <your_package>.paldemo.data::PAL_DEMO_TABLES.PAL_CONTROL_TBL,

                 <your_package>.paldemo.data::PAL_DEMO_TABLES.PAL_ABC_RESULT_TBL :

                       SELECT, INSERT, UPDATE, DELETE;

  catalog sql object "_SYS_AFL"."<your NEO_xxx schema>_PAL_ABC" : EXECUTE;

  sql object <your_package>.paldemo.procedures:GET_PAL_ABC.hdbprocedure: EXECUTE;

}

  • To apply changes use the Team -> Activate menu over the paldemo project. On the screenshot below you may see how the project should looks like.

paldemo_project.png

  • Grant model_access role to business/SCN user. In SQL console execute
CALL "HCP"."HCP_GRANT_ROLE_TO_USER"('<your package>.paldemo.roles::model_access', '<any SCN user>')
Example:
CALL "HCP"."HCP_GRANT_ROLE_TO_USER"('p123456trial.sample.paldemo.roles::model_access', 'p789012')

 

Step 8: Running paldemo application

Now it's time to see PAL ABC analysis as business users. Open theSAP HANA Cloud Platform cockpitand open theHANA XS Applicationsmenu. Open applications ofsample trial instance. To see the actual application and the visualization of the application data open Application URL in your browser. Here is how the application looks like:

paldemo_xs_appl.png


(Optional) Delete the PAL wrapper procedure

Optionally, if you no longer need the wrapper procedure for the ABC Analysis function you can delete it by calling "HCP"."HCP_AFL_WRAPPER_ERASER" with the same name you passed when you were generating this wrapper.

CALL "HCP"."HCP_AFL_WRAPPER_ERASER"('PAL_ABC');

Note that if you recreate (delete/create) the same wrapper procedure, you have to reactivate model_access.hdbrole file, and execute the following procedure:

CALL "HCP"."HCP_SYNCHRONIZE_ROLES";

 

Summary

As a user of SAP HANA Cloud Platform you get immediate access to Predictive Analysis Library (PAL) without any installation and configuration. You can use it just as described in the PAL Documentation with some minor specifics described in the SAP HANA Cloud Platform Help.

 

 

Contributor:

Dobrinka Stefanova

Dimitar Tenev

 

Related Links

 

8 Easy Steps to Develop an XS application on the SAP HANA Cloud Platform

Click and Try Sample XS Applications on the SAP HANA Cloud Platform
Using HANA Modeler in the SAP HANA Cloud

Creating and using HANA native scripted calculation view in SAP HANA Cloud

Enhance Your Cloud Application with HANA Search


Viewing all articles
Browse latest Browse all 3285

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>