Friday, August 5, 2016

Build First SAP HANA Model in 10 minutes

In the article SAP HANA Modeling Introduction we explained the basics of SAP HANA data modeling.
This article describes how to build a simple model using data stored on SAP HANA. 
By the end of this, you will have created tables, attribute views, and analytical views in SAP HANA.

Prerequisite:
You have SAP HANA Studio installed on your machine.

Add HANA System in HANA Studio

In the article Download and Install HANA Studio we explained how to download and install HANA Studio.
In this article we will explain how to add HANA system into HANA Studio

In order to connect to a SAP HANA system we need to know the Server Host ID and the Instance Number. Also we need a Username & Password combination to connect to the instance. The left side Navigator space shows all the HANA system added to the SAP HANA Studio.

Steps to add new HANA system:

  • Right click in the Navigator space and click on Add System 



  • Enter HANA system details, i.e. the Hostname & HANA Database Instance Number and click Next. (The port 3xx15 - xx is instance number - for jdbc is required to open for the connection)



  • Enter the database username & password to connect to the SAP HANA database. Click on Next and then Finish.
  • The SAP HANA system now appears in the Navigator.


  • Create new tables in SAP HANA and fill them with data:


    1. Open HANA Studio and expand the SAP HANA system.
    Go to your schema. Right-click on your schema and select SQL editor.
    SAP HANA Studio
    Note: In this example schema name is "SAP_HANA_TUTORIAL". In case you want to create a new schema use below query.
    create schema "schema_name";


  • Copy and paste the below script in SQL editor and execute. 

  • --REPLACE <YOUR SCHEMA> WITH YOUR SCHEMA NAME

    -- Create Product table
    create column table "<YOUR SCHEMA>"."PRODUCT"(
          "PRODUCT_ID" INTEGER null,
          "PRODUCT_NAME" VARCHAR (100) null default ''
    );
    insert into "<YOUR SCHEMA>"."PRODUCT" values(1,'Shirts');
    insert into "<YOUR SCHEMA>"."PRODUCT" values(2,'Jackets');
    insert into "<YOUR SCHEMA>"."PRODUCT" values(3,'Trousers');
    insert into "<YOUR SCHEMA>"."PRODUCT" values(4,'Coats');
    insert into "<YOUR SCHEMA>"."PRODUCT" values(5,'Purse');
    -- Create Region table
    create column table "<YOUR SCHEMA>"."REGION"(
          "REGION_ID" INTEGER null,
          "REGION_NAME" VARCHAR (100) null default '',
          "SUB_REGION_NAME" VARCHAR (100) null default ''
    );

    insert into "<YOUR SCHEMA>"."REGION" values(1,'Americas','North-America');
    insert into "<YOUR SCHEMA>"."REGION" values(2,'Americas','South-America');
    insert into "<YOUR SCHEMA>"."REGION" values(3,'Asia','India');
    insert into "<YOUR SCHEMA>"."REGION" values(4,'Asia','Japan');
    insert into "<YOUR SCHEMA>"."REGION" values(5,'Europe','Germany');

    -- Create Sales table
    create column table "<YOUR SCHEMA>"."SALES"(
          "REGION_ID" INTEGER null,
          "PRODUCT_ID" INTEGER null,
          "SALES_AMOUNT" DOUBLE null);

    insert into "<YOUR SCHEMA>"."SALES" values(1,1,100);
    insert into "<YOUR SCHEMA>"."SALES" values(1,2,90);
    insert into "<YOUR SCHEMA>"."SALES" values(1,5,85);
    insert into "<YOUR SCHEMA>"."SALES" values(2,2,80);
    insert into "<YOUR SCHEMA>"."SALES" values(2,1,75);
    insert into "<YOUR SCHEMA>"."SALES" values(3,3,85);
    insert into "<YOUR SCHEMA>"."SALES" values(4,4,75);
    insert into "<YOUR SCHEMA>"."SALES" values(5,1,65);
    insert into "<YOUR SCHEMA>"."SALES" values(5,2,65); 


  • After executing the scripts you should have 3 tables created. If there are no tables, try right-clicking on your schema and refreshing.
    SAP HANA Studio



  • Grant schema SELECT rights to _SYS_REPO user:

    Open the SQL editor of your schema and execute the following command line:
    GRANT SELECT ON SCHEMA <YOUR SCHEMA> TO _SYS_REPO WITH GRANT OPTION;
    If you miss this step, an error will occur when you activate your views later.
    Click here to know more about this.


    Create an attribute view:


    1. Open HANA Studio and make sure you are in Modeler perspective

    2. SAP HANA Studio
    3. Create a new package under the content folder.
      Right-click on the content folder and choose "New" -> "Package." Fill the fields "Name" and "Description" and click "OK."
      If you don't see the new package after this, try right-clicking on the content folder and refreshing.
    4. Right click on the package and choose "New Attribute View." Enter a name and a description and click "Finish"

      SAP HANA Attribute View 
    5. The attribute view will be opened in the HANA studio editor. Select "+" sign on "Data Foundation"

      SAP HANA Attribute View 
    6. Search for table "REGION" and select it.

      SAP HANA Attribute View 
    7. Now add the columns from the REGION table to output. Simply right click on the column and select "Add to Output". Add all 3 columns REGION_ID, REGION_NAME, SUB_REGION_NAME to the output.
      Once it is done, you will see the selected column in right side pane.

      SAP HANA Attribute View 
    8. Now select "Semantics". All 3 columns appears under Column pane as attributes.
      SAP HANA Attribute View 
    9. Now you need to define attributes and key attributes. Every attribute view must have at least one key attribute. 
    10. Click on the "Type" to select key attribute for REGION_ID.

      SAP HANA Studio 
    11. Click on the "Save and Activate" button on top right corner to activate the view.

      SAP HANA Studio 
    12. In the "Job log" Pane you will see an activation completed message.

      SAP HANA Studio 
    13. Attribute view is created and activated.
      To see the output of this view click on the "Data Preview" button on top right corner.
      SAP HANA Studio

      Then select "Raw Data" tab.
      SAP HANA Studio 

    Congratulation!! You have successfully created your first modeling view.

    Next step is to create an analytic view.


    Create an Analytic View:


    1. Right click on the package and choose "New Analytic View." Enter a name and a description and click "Finish"

      SAP HANA Analytic View 
    2. Click on "+" sign of "Data Foundation" and add table SALES.

      SAP HANA Analytic View 
    3. Right Click on the columns of table SALES and add REGION_ID and SALES_AMOUNT to output.

      SAP HANA Analytic View 
    4. Click on "+" sign of "Logical Join" and add attribute view "AT_Region" which was created earlier.

      SAP HANA Analytic View 
    5. Click on the REGION_ID from "Data Foundation" and connect it to the REGION_ID of attribute view AT_Region. In the properties pane select join type as left outer join and cardinality as n..1

      SAP HANA Analytic View 
    6. Select "Sementics". In the right side change the column type of SALES_AMOUNT as measure.

      SAP HANA Analytic View 
    7. Activate the analytic view similar to attribute view.
      Right-click on your analytic view and choose "Data Preview." After that, you can browse through the tabs named raw data, distinct values, and analysis.

      SAP HANA Analytic View

      SAP HANA Analytic View 

    Congratulation!! You have successfully created your first Analytic View. 


    Reference at http://saphanatutorial.com/build-your-first-sap-hana-model/

    No comments:

    Post a Comment

    SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

    SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...