sap hana self-learning guide

47
SAP HANA Developer Self-Learning Guide Personal Spending Analysis on SAP HANA Author: Dr. Jordan Cao

Upload: edward-huang

Post on 24-Sep-2015

39 views

Category:

Documents


10 download

DESCRIPTION

This guide introduces the SAP HANA development environment and developmenton SAP HANA. It outlines the steps to develop a Personal Spending Analysis tool,whose user interface is implemented by using SAP Dashboard solution.

TRANSCRIPT

  • trial system

    IBM Systems solution forSAP HANA Developer Self-Learning GuidePersonal Spending Analysis on SAP HANA

    Author: Dr. Jordan Cao

  • Learning is finding out what you already know. Doing is demonstrating that you know it. Teaching

    is reminding others that they know it as well as you do. We are all learners, doers, and teachers.

    Richard David Bach

    A technology marketing manager for SAP HANA, Jordan Cao has more than 15 years of experience in computing science, including roles as an SAP senior architect and solution manager. Jordan was part of the first SAP HANA in-memory Proof-of-Concept (PoC) project for the Banking field,

    and has participated in multiple SAP projects focused on scientific programming, software architecture, and web services-related tasks. He has a PhD in cloud computing, service-oriented architecture, and software engineering, as well as an MBA.

    ABOUT THE AUTHOR

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    TABlE Of cOnTEnTsToPIC 1:

    IntroductIon 4

    About this Self-Learning Guide 5

    Before You Start: Pre-requisites and System Requirements 5

    Introducing SAP HANA 6

    SAP HANA Architecture overview 6

    Design principle and advanced features 6

    Benefits of SAP HANA for developers 6

    About the Project 8

    Personal Spending Analysis Tool 8

    Infrastructure 9

    Data sources 9ToPIC 2:

    prepare Sap Hana deVeLopMent enVIronMent 10

    1. Add SAP HANA Instance 11

    2. Load the Data 13

    3. Check the data 17ToPIC 3:

    ModeLIng In Sap Hana 19

    1. Create a Package 21

    2. Create a Time Attribute View 22

    3. Generate Time Data 25

    4. Create an Analytic View 27ToPIC 4:

    ModeLIng uSIng SQLScrIptS 31

    1. Create a Calculation View 33

    2. Create Procedure and the Summary/Average Function 38ToPIC 5:

    referenceS 46

  • TOpic 1: inTROdUcTiOn

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Introduction | 5

    About this Self-Learning Guide

    This guide introduces the SAP HANA development environment and development on SAP HANA. It outlines the steps to develop a Personal Spending Analysis tool, whose user interface is implemented by using SAP Dashboard solution.

    By following the guide, you can learn how to:

    Install an SAP HANA client package and SAP HANA modeler studio Use SAP HANA basic modeling technologies: attribute view, analytic view,

    calculation view, and R language to build application data processing logic Build a Dashboard UI to represent the output values

    Besides those hands-on experiences, after finishing this example, this guide wants to help you to have a better understanding:

    Underlying principles and concepts Application architecture Application development process Multiple models to build one application Benefits for developers

    This self-learning guide focuses on the steps to build one simple application from end to end (from loading data to represent the output in a dashboard). For more details about HANA, please read the documents and books listed in the reference session, such as the free eBook: SAP HANA Essentials, Your First SAP HANA Excel Report, and SAP HANA Modeling Guide. They are all included in the reference. For more documents, please visit SAP HANA session in http://developer.sap.com.

    Before You Start: Pre-requisites and System Requirements

    To successfully build the applications in this guide, you will need:

    Access to an SAP Businessobjects BI Platform Installed SAP BI Platform Clients An installed SAP Businessobjects Dashboard An SCN account Internet Explorer Windows 7 or XP

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Introduction | 6

    Introducing SAP HANA

    SAP HANA is a game-changing, real-time platform for analytics and applications. While simplifying the IT stack, it provides powerful features like: high processing speed, predictive capabilities and text mining capabilities.

    SAP HANA Architecture overview

    SAP HANA is an in-memory database, as well as an in-memory Data platform. to support applications normally with five components: data provision, data loading, data storage, data computation, and data presentation. The SAP HANA platform combines the data storage function and the data computation. The other components are supported by multiple tools seamlessly integrating with HANA, such as data services and BI tools.

    Design principle and advanced features

    The SAP HANA platforms underlying design principle is to push the data computation logic to the data side with providing multiple advanced features, such as scheduling engine, computation engine, security, optimization, and compiler. HANA translates model artifacts into a domain-specific languages, called as a calculation model, which is a directed acyclic graph with arrows representing data flows and nodes representing operations. This approach, along with the exclusion of loops and recursion, enable automatic massively parallel processing. The diagram shows an architecture view of the execution controller.

    Benefits of SAP HANA for developers

    SAP HANA offers numerous benefits for developers, including:

    1. Simplified application logic: Application complexity can come from the complexity of the data model, algorithms, system configuration, and software architecture. By eliminating aggregates, indexes, and the component boundary, this reduces complexity and the efforts to define and maintain metadata:

    Elimination of application boundary: Moving the computation to the data platform means different applications will run on the same platform without a boundary. Each application can focus on its tasks and leave the data management to the in-memory platform. This significantly reduces the need to implement and optimize data management.

    Information Composer & Modeling Studio

    Planning andCalculation Engine

    Real-time replicationservices

    Application Services(e.g. HTML 5 Server)

    Predictive Analysis &Business Function Libraries

    In-memory database

    Text Search

    R & Hadoop Integration

    Data Services

    Real-time analytics Real-time apps

    SAP NetWeaverBusiness Client

    SAP BusinessObjects solution

    MicrosoftExcel Others...(Open)

    SAP Business Suite Third-party systems

    A platform for a new class of real-time analytics and applications

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Introduction | 7

    Elimination of additional indexes: In many cases, columnar storage eliminates the need for additional index structures. Column scanning speed and compression mechanisms especially dictionary compression allow high performance read operations.

    Elimination of materialized aggregates: Materialized aggregates try to increase read performance by storing the aggregates data; read operations do not need to compute them each time they are required. In-memory column stores make it possible to calculate aggregates on large amounts of data on the fly with high performance.

    2. Optimized: Moving less data optimizes system computation. And high-speed in-memory computing makes it possible not to materialize aggregates, which further saves on costs. These optimized benefits come from:

    Optimized data management: HANA supports the representation of application-specific business objects (like OLAP cubes) and logic (domain specific function libraries) directly inside the database engine. This permits the exchange of application semantics with the underlying data management platform, which can be exploited to increase query expressiveness, and reduce the number of individual application-to-database round trips and the amount of data transferred between the database and the application.

    Optimized data accessing: The HANA database is optimized to efficiently communicate between the data management and application layers. For example, the HANA database natively supports SAP application servers data types. Future plans include integrating novel application server technology directly into the SAP HANA database cluster infrastructure, to enable interwoven execution of application logic and database management functionality.

    Calculation model (data ow graph)

    Model optimizer (rule based)

    Model executor

    Calculation engineoperators

    Calculation engine

    Standard SQL Statement

    SQL processor

    Statistics

    SQL Script MDX* query Planning model Other language / model

    SQLScript compiler MDX compiler Planning engine Other compiler

    Intermediate results

    Database optimizer

    Database executer

    Row store Column store

    Script execution runtime

    Intermediate results

    Execute user-denedfunction

    Logicalexecution plan

    Physicalexecution plan

    R*

    R

    R

    R

    RR

    R

    R

    R RR

    *MDX = Mult dimonsional expression; *R = request

    Optimized data processing: The HANA database comprises a multi-engine query processing environment, which supports different data abstraction and different degrees of data structure from well-structured data to unstructured text data. Column-based storage also optimize the parallel computation using multiple processor cores. operations on different columns or patritioned data on the same column can be searched or aggregated in parallel by assigning different processor cores. The HANA database also supports efficient processing of both transactional and analytical workloads on the same physical database.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Introduction | 8

    About the Project

    Personal Spending Analysis Tool

    Most credit card companies provide customers with tools to download their transaction history. Some providers even offer a very useful personal spending analysis tool to help customers analyze their spending. For example, Discover (www.discovercard.com) provides customers with a detailed analysis tool, which includes transactions and a spending history:

    From this diagram, we can find that Discovers personal spending analysis tool provides the following features:

    Summary section: for example, All Categories Spending analysis by category: summarizes transactions from different

    categories Spending analysis by time: shows spending trends, such as average spending,

    YTD, Last 12 months, and Last 24 months summary. Transaction view: show the transaction date, post data, description, amount,

    and category.

    This guide demonstrates how to build a similar personal spending analysis. By following the steps, you will build your own personal spending analysis tool using SAP HANA and an SAP Dashboard. And you can download your spending data and follow the instructions to analyze it.

    The guide provides transaction history sample data. You can expand the scope of the project to include Enterprise financial data, such as banking transaction history analysis, by using data loaded from the SAP ECC core banking component, which has a more complex data structure. You will be able to provide more advanced analyses and deal with a bigger data volume.

    Moreover, you can also build more advanced features to evaluate your own spending. We encourage you to share your work, comments and new features at www.experiencesaphana.com.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Introduction | 9

    Local Machine

    SAP Hana client SAP HANA studio HANA data Loader (only load

    data for this example) csv source data

    Data Source and Data Loading

    AWS HANA Server

    SAP HANA Server R

    Data Storage & Data Computation

    AWS Windows Server

    SAP BusinessObjects BI Platforms SAP BusinessObject BI Client SAP BusinessObjects Dashboard

    Data Representation

    SAPHANA

    SAP In-Memory Computing Engine

    SAP Replication Server RInterface Libraries

    SQL(ODBC/JDBC)

    SQL(ODBC/JDBC)

    Semantic Layer

    Dashboards

    CSV TEXT DATA

    Infrastructure

    Before you build the example application, you need to understand the infrastructure. The right diagram in following diagram shows the system landscape with a three-layer view. This example uses a dashboard to show the data reading through a semantic layer from SAP HANA.

    The left diagram shows the device view. SAP offers special developement environment based on the Amazon Web Service solution. Please contact SAP sales

    or partners to find more details about this environment. Otherwise, you can use the free AWS based HANA server instance and the free HANA modeler studio provided by SAP. But you have to install the BoE environment and Dashboard tool by youself.

    Data sources

    This guide uses a simple data structure with two tables. The example application uses psa_category as the table name.

    Name SQL Sample Data

    PSA_CATEGoRY

    Create column table DATA(ID INTEGER not null,TRAN_DATE DATE null,PoST_DATE DATE null,DESCRIPTIoN VARCHAR (100) null, AMoUNT DoUBLE null, CATEGoRY_TEXT VARCHAR(60) null, primary key (ID))

    This is the core transaction data set.

    The structure can be expanded with more information to support more analyses. For example, an additional column could be added to identify if a transaction is a deposit/credit.

  • TOpic 2: pREpARE sAp HAnA dEVElOpMEnT EnViROnMEnT

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Prepare SAP HANA Development Environment | 11

    STEP 1

    open the SAP HANA modeler studio and choose the Modeler perspective. Please make sure you did not choose Administration perspective.

    STEP 2

    In the Navigator panel, right-click the empty space and choose Add System... from the context menu.

    1. Add SAP HANA Instance

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Prepare SAP HANA Development Environment | 12

    STEP 3

    on the pop-up, enter:

    the host name the System number the description for your HANA server

    And then, click on Next.

    STEP 4on this screen, enter the user name, and the password. Please check with your administrator to get the information. Click on Finish.

    Note: the SAP HANA modeler studio will ask to setup password recovery setting. We can ignore this step.

    Add SAP HANA Instance

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Prepare SAP HANA Development Environment | 13

    STEP 1

    After installing the HANA client tools and modeling studio, you will load the data into the HANA instance to start implementing the example application.

    While data loading can be very complex, SAP provides multiple solutions for different data loading scenario. For this simple example, we will use the basic text data file loading feature provided by SAP HANA modeler studio. It can directly load your local files into the HANA system. Open the HANA modeler studio, and click on File menu and select Import item.

    STEP 2

    The HANA modeler studio shows the Import wizard, please select the Data from Local File

    2. Load the Data

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Prepare SAP HANA Development Environment | 14

    STEP 3

    Select the HANA system created in the previous step, and then click on the Next button.

    STEP 4

    Click on the Browser button in the source file option.

    Load the data:

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Prepare SAP HANA Development Environment | 15

    STEP 5

    Select the transaction.csv file provided to you in the example package.

    STEP 6

    Select the Header row exists option; set the schema to be SYSTEM, and set the table name to be PSA_TRANSACTIoN.

    Load the Data:

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Prepare SAP HANA Development Environment | 16

    STEP 7

    In the Manage Table Definition and Data Mappings diagram, we will define the table structure. Select the check box to set ID to be a key field. Change the data type for TRAN_DATE and PoST_DATE from NVARCHAR to DATE.

    STEP 8

    Review the data and click on Finish button.

    Load the Data:

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Prepare SAP HANA Development Environment | 17

    STEP 1

    After successfully loading the data, you will be able to review the PSA_TRANSACTION data or check the table definition in SAP HANA modeler Studio. Right click on the table name and select Open Definition menu. You can see the table definition in this view.

    To check the data definition, you can select the Open Definition item in the context menu,

    STEP 2

    And then the tool will open a table definition review window to show the table definition.

    3. Check the data

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Prepare SAP HANA Development Environment | 18

    STEP 3

    To preview the data, you can select the open Content item.

    STEP 4

    And then the data will appear in a data preview window.

    Check the Data:

  • ABOUT THE pROjEcTTOpic 3: MOdEling in sAp HAnA

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling in SAP HANA | 20

    Your original data are loaded into SAP HANA as database tables. They can be modeled as multiple different views. Those views define all necessary aggregation, computation, or data mining.

    Tables are tabular data structures, with each row identifying a particular entity, and each column having a unique name. Data fields of one row are called the attributes of the entity. The word attribute has multiple meanings. It may denote a table column, a particular data field of a table row, or the contents of a data field.

    Views are combinations and selections of data from tables modeled to serve a particular purpose. They appear like readable tables, i.e., database operations that read from tables can also be used to read data from views. In SAP HANA, there are three different types of views: attribute, analytic, and calculation.

    Attribute Views

    Attribute views are used to give master data tables context. This context is provided by text tables which give meaning to the master data. For example, in this example, our fact table or analytic view contains some numeric category ID for each transaction record.

    You can link the text description to an attribute, such as category ID, using an attribute view. You can then display the category description in the data representation layer, instead of the IDs, which provides the context for the master data table.

    Attribute views are used to select a subset of columns and rows from a data table. Since it is of little use to sum up attributes from master data tables, there is no need to define measures or aggregates for attribute views. You can also use attribute views to join with master data tables.

    Analytic Views

    Analytic views are used to build a data foundation based on transactional fact tables. You can create a selection of measures (sometimes referred to as key figures), add attributes, and join attribute views. Analytic views leverage the computing power of SAP HANA to calculate aggregate data, e.g., the number of cars sold per country or the maximum power consumption per day.

    These views are defined on at least one fact table, for example, a table containing one row per sold car or one row per power meter reading. Fact tables can be joined to allow access to more detailed data using a single analytic view.

    Analytic views can be defined on a single table, or joined tables. They can contain two types of fields/columns, so-called measures and attributes. Measures are fields for which an aggregation must be defined. If analytic views are used in SQL statements, the measures have to be aggregated using the SQL functions, such as SUM, MIN, or MAX. Attributes can be handled as regular columns, which do not need to be aggregated.

    Calculation Views

    Calculation views are used to provide composites of other views. More details will be covered in the subsequent chapters.

    Start Building Your HANA Project

    In this chapter, we will focus on creating attribute view and analytic view.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling in SAP HANA | 21

    STEP 1

    Choose the Modeling perspective. And on the Navigator panel, you will see the Content item. Right click on this item. And select New/Package from the context menu. The HANA modeler studio wizard screen will appear.

    STEP 2

    Enter the name and description in the window, for example, give the name psa, and the description Personal Spending Analysis. And then click on oK, the new package will be created.

    1. Create a Package

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling in SAP HANA | 22

    STEP 1

    To support time-related computation, this example application need to create a new time attribute view to support date related computation. Right click on the package and select New > Attribute Views.

    2. Create a Time Attribute View

    STEP 2

    Complete the Attribute View screen. Use the ATT_TIME_VIEW:

    Enter a Name and Description.

    For Attribute View Type, select Time.

    For Calendar Type, select Gregorian.

    For Granularity, select Date.

    Select the Auto Create checkbox.

    Click on Finish. The time attribute view will be created.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling in SAP HANA | 23

    Create a Time Attribute View

    STEP 3

    By default, the time attribute view will include multiple predefined attributes. Lets delete these pre-inserted attributes.

    Select all pre-defined attributes. (select the first one and hold the SHIFT key, and then click on the last one. All pre-defined attributes will be selected.)

    Right-click on the selected attributes.

    Select Remove from the context menu.

    STEP 4

    After cleaning the pre-selected attributes, we will add ours. First, we will add DATE_SQL as the key attribute. Right click on the DATE_SQL and select Add as Key Attribute, and then save this view.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling in SAP HANA | 24

    STEP 5

    Select the following items. Right click on the item and select Add as attribute. We will add the following items as attributes: CALQUATER, CALMoNTH, CALWEEK, YEAR_INT, QUARTER_INT, MoNTH_INT, WEEK_INT, WEEK_YEAR_INT, DAY_oF_WEEK, DAY_INT.

    (Hint: those attributes are consecutive attributes. You can click on the first one CALQUATER, hold on SHIFT key and then click on the last one DAY_INT. All attributes will be selected.)

    Create a Time Attribute View

    STEP 6

    Now, we need to activate the new time attribute view. Right-click on the new attribute view and then select Activate from the context menu.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling in SAP HANA | 25

    3. Generate Time Data

    STEP 1

    After creating the time view, we need to generate the time data. Lets use the shortcut link: choose Help > Quick Launch and click on Generate Time Data.

    STEP 2

    Complete the generate time data wizard. For this example, we can use the following setting to generate date data in 2011.

    For Calendar Type, select Gregorian.

    For From Year, enter 2011.

    For To Year, enter 2012.

    For Granularity, select DAY.

    Click on Generate.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling in SAP HANA | 26

    STEP 3

    To review the time data, right-click on the ATT_TIME_VIEW, and choose open Data Preview.

    Link Views and Generate Time Data

    STEP 4

    Add then you can see the data output in this view. And the generated time data will be stored in _SYS_BI.M_TIME_DIMENSIoN. You can also check this generated table.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling in SAP HANA | 27

    4. Create an Analytic View

    STEP 1

    Right click on the psa package. Choose New > Analytic View from the context menu.

    STEP 2

    The analytic wizard screen first asks for the name and description of the analytic view. Use the name ANA_TRANSACTIoN. For this application, you dont need to define the currency conversion schema now. Click on Next.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling in SAP HANA | 28

    STEP 3

    Select the fact table that defines the key measures. In this example, select the PSA_TRANSACTIoN table under SYSTEM schema. Click on the Add button in the middle. This will select the PSA_TRANSACTIoN into the analytic view definition.

    You can also ignore this step and add the fact table later into the Analytic view. And then click on Next.

    Create an Analytic View

    STEP 4

    Choose the pre-defined Time attribute view. Again, the attribute views can be added into analytic model later.

    For this application, choose the ATT_TIME_VIEW attribute view we created just now. Click on Finish. The analytic view will be created.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling in SAP HANA | 29

    Create an Analytic View

    STEP 5

    The analytic view needs to add at least one measure. For this example application, we need to select the Amount as the measure. Right click on this field and then select Add as Measure from the context menu. You can see the Amount field will be added into the Measures group on the output panel.

    STEP 6

    Next, we will add attributes into the Private Attribute group in the right output panel. Define the attributes in analytic view:For this application, hold down the Ctrl key, and left-click on the ID, TRAN_DATE, PoST_DATE, DESCRIPTIoN, and CATEGoRY_TEXT attributes column. Right click on the attributes, and then select Add As Attribute item from the context menu.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling in SAP HANA | 30

    STEP 7

    This step is to define the relationship between the table and attribute view in the analytic view model. To create a link between the table and attribute views, switch the analytic view model from Data foundation tab to Logical View tab. You will see both ATT_TIME_VIEW and the data foundation into the logical view. Drag the TRANS_DATE from Data Foundation to link with DATE_SQL from TIME_VIEW.

    Create an Analytic View

    STEP 8

    After that, you can save the model first. Right click on the ANA_TRANSACTION analytic view, and then click on the Activate menu from the context menu to activate the new analytic view.

    This diagram shows another quick way to active the model. It is the Save and Activate button showed in the screen. You can click on this button to quickly activate the model.

  • ABOUT THE pROjEcTTOpic 4: MOdEling Using sQlscRipTs

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 32

    Calculation views are based on the result of a SQLScript. These scripts can join two or more data flows or invoke built-in or generic SQL functions. They can be defined as either graphical views or scripted views depending on how they are created.

    Graphical views can be modeled using the graphical modeling features of the SAP HANA Information Modeler. Scripted views are created as sequences of SQLScript statements. In essence, they are SQLScript procedures with certain properties.

    Calculation views can be used in the same way as Analytic views, which one key difference: several fact tables can be joined in a Calculation view. Also, calculation views must have at least one measure.

    SQLScript functions composed of SQL queries and function calls can be represented as acyclic data flow graphs. These functions, implemented in SQLScript language, are typically transformed into calculation models that contain only one transformation node of type SQLCcript. In some cases, a more complex graph can be created with SQL nodes for embedded SQL queries and nodes for imperative code sequences.

    Each node has a set of inputs and outputs, and an operation that transforms the inputs into outputs. In addition to their primary operation, each node can also have a filter condition for filtering the result set. The operations inputs and outputs are table-valued operands. Inputs can be connected to SAP HANA tables or node outputs. Calculation models support a variety of node types:

    Nodes for set operations, such as projection, aggregation, join, union, minus, and intersection

    SQL nodes that execute an SQL statement that is an attribute of the node

    Scripting nodes for describing complex operations that cannot be described with a graph of data transformations. The function of such a node is described by a procedural script.

    To enable parallel execution, a calculation model may contain split and join operations. A split operation is used to partition input tables for subsequent processing steps based on partitioning criteria. operations between split and join operations may then be executed in parallel for the different partitions.

    SAP HANA also provides a set of CE functions, which can be used to replace some SQLScripts. They provide better optimization and better performance. This session only shows the basic capability of the calculation view. The CE function will be discussed in the following sections.

    Continue Building Your Project

    This example will implement one special feature to demonstrate the Calculation view: the trend of the difference of the average amount comparing to previous month in a time line. You will calculate the average amount, and use the average amount of the successive month minus the average amount of the current month. A derived table will be used to implement this feature.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 33

    STEP 1

    open SAP HANA Modeler Studio. Right click on your psa project, and select New > Calculation View.

    1. Create a Calculation View

    STEP 2

    Enter a Name (CAL_AVG_DIFF_TREND), choose SQL Script as the View Type, and click on Finish.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 34

    Create a Calculation View

    STEP 3

    The calculation view is created. The left panel in calculation view editor shows a simple graphic with two boxes: Script_View and output. Script_View is connected to Output through a line and is the place to define the SQLScript logic. Output is the place to define the output field of the Calculation view.

    STEP 4

    Click on the Script_View box. The SQLScript editor will appear.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 35

    STEP 5

    Copy and paste the code into the editor between BEGIN and END. This code will generate the average difference between current month and previous month.

    Create a Calculation View

    STEP 6

    Right-click on the Output of Script_View panel to define output from the Script_View.

    var_out = SELECT SUM(T2.AMoUNT-T1.AMoUNT) AS AMoUNT, T1.MoNTH_INT AS MoNTH_INTFRoM(SELECT AVG(AMoUNT) AS AMoUNT, MoNTH_INT FRoM _SYS_BIC.psa/ANA_TRANSACTIoN GRoUP BY MoNTH_INT) AS T1,(SELECT AVG(AMoUNT) AS AMoUNT, MoNTH_INT FRoM _SYS_BIC.psa/ANA_TRANSACTIoN GRoUP BY MoNTH_INT) AS T2WHERE T1.MoNTH_INT = T2.MoNTH_INT - 1GRoUP BY T1.MoNTH_INT;

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 36

    Create a Calculation View

    STEP 7

    This output must have the same name, type, and order as the SQLScript statement used to assign a value to var_out. The var_out is the special variable in the calculation view. It represents the only output parameter from the calculation view.

    For this case, we will define the AMOUNT (Double) and MONTH_INT (TINYINT) for the var_out.

    STEP 8

    To define the Calculation view output, select the Output box. The Script_View output box will appear.

    Right-click on AMOUNT, and select Add As Measure to define AMOUNT as a measure.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 37

    Create a Calculation View

    STEP 9

    Right-click on MoNTH_INT and select Add as Attribute to set this as an attribute.To activate the calculation view, right-click on the calculation view, and select Activate.

    STEP 10

    Select the Data Preview menu to preview the output data. The output data from the activated calculation view will be shown in a data preview windows, similar as the steps we did in the previous section.

    We strongly suggest you preview the data output for each step to make sure each atomic step is correct. So, you will have less problems later.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 38

    STEP 1

    Before you define the summary/average function, we will create a date_range table to define how many different ranges to calculate. Open the SQL Editor. Select the Catalog/SYSTEM in Navigator panel.

    2. Create Procedure and the Summary/Average Function

    STEP 2

    The SQL Editor button will be enabled and click on it to open the SQL Editor. Copy the codes and execute it.

    CREATE CoLUMN TABLE DATE_RANGE(RANGE INT PRIMARY KEY);INSERT INTo DATE_RANGE VALUES(1);INSERT INTo DATE_RANGE VALUES(3);INSERT INTo DATE_RANGE VALUES(6);INSERT INTo DATE_RANGE VALUES(12);

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 39

    Create Procedure and the Summary/Average function

    STEP 3

    Now, we can define a procedure to calculate the time range by using this table we just created. We want to find the correct end time for our transaction records for the 1 month, 3 month, 6 month, and 12 month ranges. Right-click on the project name, and choose New > Procedure.

    STEP 4

    Enter a name (PRo_GET_TIME_RANGE), and click Finish.

    The procedure will be created.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 40

    Create Procedure and the Summary/Average function

    STEP 5

    Copy and paste the above code into the right-side editor between BEGIN and END.

    STEP 6

    Read the code and think about its logic. This code will generate a time range for the four periods (1, 3, 6, 12 months). It uses the latest date in the transaction data and calculates each time periods date as the start time.

    This step demonstrates creating the procedure which can be reused in different calculation view.

    TIME_RANGE_oUT = SELECT L.DATE_SQL AS DATE_FRoM, D.LAST_DATE AS DATE_To, R.RANGE AS RANGE FRoM _SYS_BIC.psa/ATT_TIMEVIEW AS L, (SELECT MAX(TRAN_DATE) AS LAST_DATE FRoM SYSTEM.PSA_TRANSACTIoN) AS D, SYSTEM.DATE_RANGE AS R WHERE DAYS_BETWEEN(L.DATE_SQL, D.LAST_DATE) = R.RANGE * 30;

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 41

    Create Procedure and the Summary/Average function

    STEP 7

    Right click on the Output Parameter and select New to define the output parameters: enter the same name as the variable used in SQLScript. Unlike the Calculation view, this procedure can have multiple outputs.

    STEP 8

    For this project, the output variable name is TIME_RANGE_oUT. The output structure needs to be the same as the output defined in the SQLScript statement, which has DATE_FRoM (Date), DATE_To (Date), RANGE (TINYINT).

    After creating the procedure, we can right click on the procedure and select the Activate menu to activate this new procedure.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 42

    STEP 9

    Right click on the calculation view folder in the navigator panel and then select the New menu to create a new calculation view to generate the average outputs.

    Create Procedure and the Summary/Average function

    STEP 10

    Give the new Calculation view a name CAL_GET_AMoUNT_REPoRT.

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 43

    STEP 11

    Copy and paste the sample codes into the editor between BEGIN and END. Read the code and think about its logic.

    The first line is calling the procedure we created in the previous steps. The next two SELECT statements calculated the average and summary for the transactions in a specific time frame. The last step combine the output and return union results to the caller.

    Create Procedure and the Summary/Average function

    STEP 12

    Right-click on output of Script_View/output Parameter, and select Edit. The output definition should be the same as the SQLScript, which includes the oUTPUT_TYPE ( CHAR(3) ), RANGE(TINYINT) and the AMoUNT (Double).

    CALL _SYS_BIC.psa/PRo_GET_TIME_RANGE(TIME_RANGE); AVG_oUTPUT = SELECT R.RANGE AS RANGE, AVG(AMoUNT) AS AMoUNT, AVG AS oUTPUT_TYPEFRoM _SYS_BIC.psa/ANA_TRANSACTIoN AS D, :TIME_RANGE AS RWHERE D.DATE_SQL >= R.DATE_FRoM AND D.DATE_SQL = R.DATE_FRoM AND D.DATE_SQL

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 44

    Create Procedure and the Summary/Average function

    STEP 13

    Click on the Output box to define the output of the calculation view. Right-click on AMoUNT, and select Add As Measure.

    STEP 14

    Hold the control key and select RANGE and oUTPUT_TYPE. Right-click on them and select Add as Attribute to define them as attributes,

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling using SQLScripts | 45

    Create Procedure and the Summary/Average function

    STEP 15

    Click on the Save and Activate button to activate the new calculation view.

    STEP 16

    Select the Data Preview button. You can review the output data from the activated calculation view.

  • ABOUT THE pROjEcTTOpic 5: REfEREncEs

  • SAP HANA Developer Self-Learning Guide: Personal Spending Analysis on SAP HANA

    Topic 1: Introduction Topic 2: Prepare SAP HANA Development Environment

    Topic 3: Modeling in SAP HANA

    Topic 4: Modeling using SQLScripts

    Topic 5: Reference

    Modeling with R | 47

    1. How to use the SAP HANA studio and how to administrate the SAP HANA database: SAP HANA Database Administration Guide (PDF)

    2. Your First SAP HANA Excel Report: SAP HANA Pocket Book for Developers (HTML)

    3. Free SAP HANA eBook: SAP HANA Essential eBook (HTML)

    4. How to use the HANA Modeler to create oLAP views based on SQLScript programs: SAP HANA Modeling Guide (PDF)

    5. SQL Reference Guide: SAP HANA Database SQL Reference Guide (PDF)

    6. Full reference to the query language used in SAP HANA: SAP HANA Database SQL Reference Guide (HTML)

    7. A tutorial about how to use SQLScript to program and use stored procedures in SAP HANA: SAP HANA Database SQLScript Guide (PDF)

    8. BI Semantic Layer Discussion: Semantic Layer Forum on SDN (HTML)

    9. Universe Best Practice: Universe best practices on SAP HANA (HTML)

    10. IDT Tutorials: Information design tool tutorials (HTML)

    11. SAP HANA Integrate with R: SAP HANA Database Integration with R programming language (PDF)

    12. Blog of Tejada Galindo, Alvaro: When SAP HANA Met R First Kiss (HTML)

    13. HANA Academy: http://academy.saphana.com/

    Topic 1:IntroductionAbout this Self-Learning GuideBefore You Start: Pre-requisites and System Requirements

    Introducing SAP HANASAP HANA Architecture OverviewDesign principle and advanced featuresBenefits of SAP HANA for developers

    About the ProjectPersonal Spending Analysis ToolInfrastructureData sources

    Topic 2:prepare SAP HANA DEVELOPMENT environment1. Add SAP HANA Instance2. Load the Data3. Check the data

    Topic 3:Modeling in SAP HANA1. Create a Package2. Create a Time Attribute View3. Generate Time Data4. Create an Analytic View

    Topic 4:Modeling using SQLScripts1. Create a Calculation View2. Create Procedure and the Summary/Average Function

    Topic 5:References