Sunteți pe pagina 1din 9

Creating an SAP HANA XS Application

This tutorial explains how to create a simple SAP HANA XS application that is written in server-side JavaScript

and displays the "Hello World!" message together with a string extracted from a table in the SAP HANA

database.

Prerequisites

You have installed the tools as described in Installing SAP HANA Tools for Eclipse.

Context

In this tutorial, you complete the following steps:

1. Trial SAP HANA instance only: Create an SAP HANA development package.

2. Use the Eclipse IDE to access an SAP HANA instance.

3. Create a subpackage.

4. Create a repository workspace.

5. Create an XS project.

6. Create the following files:

o Application descriptor (.xsapp): Marks the root point in the package hierarchy from which content

can be served. It is a prerequisite to develop and deploy an application on SAP HANA Extended

Application Services (SAP HANA XS).

o Application access file (.xsaccess): Determines whether or not package content can be exposed

and specifies the authentication method to be used to grant access.

o Application privileges file (.xsprivileges): Defines the privileges required to access an application.

o Role (.hdbrole): Contains the defined application privileges.

7. Create the application JavaScript file (file extension XSJS)

8. Grant the user the role required to access the application

9. Open the application from the cockpit

The following example data is used:

 Account: p1234567890trial
 User: p1234567890

 SAP HANA Cloud Platform landscape: hanatrial.ondemand.com

Replace the above with your own account and user names. If you have a productive SAP HANA instance,

replace hanatrial.ondemand.com with the appropriate landscape host, for example, hana.ondemand.com.

(For Trial Landscape Only) 1. Create a SAP HANA


Instance
This step is required if you use a trial SAP HANA instance.

Context

You can create one trial SAP HANA instance per account. A trial instance is equivalent to a database schema

with the database property HANA XS. Trial instances are also listed under Databases & Schemas in the

cockpit.

Note This section explains working with trial SAP HANA instances that provide you with a shared database
instance, enabling you to work with SAP HANA in a managed environment, using schemas instead of tenant

databases.

For more information about working with trial SAP HANA databases with multitenant database container

support enabled, see Databases and Creating SAP HANA MDC Databases.

Procedure

1. Log on to the cockpit on the trial landscape (https://account.hanatrial.ondemand.com/cockpit) and

select an account.

2. Choose Databases & Schemas in the navigation area.

All databases available in the selected account are listed with their ID, type, version, and related

database system.
Tip To view the details of a database, for example, its state and the number of existing bindings,
select a database in the list and click the link on its name. On the overview of the database, you can

perform further actions, for example, delete the database.

3. To create a trial instance, choose New on the Databases & Schemas page.

The New Database/Schema screen is displayed.

4. Enter the following details:

o Schema ID. A schema ID is freely definable but must start with a letter and contain only uppercase

and lowercase letters ('a' - 'z', 'A' - 'Z'), numbers ('0' - '9'), and the special characters '.' and '-'. Note

that the actual schema ID assigned in the database will be different to this version.

o Database System: Select an available database ('MaxDB' or 'HANA') from the dropdown box.

To create schemas on your productive HANA instances, you have to use the HANA-specific tools.

5. Save your entries.

2. Connect to an SAP HANA Instance

Context

See the following links, depending on your use case (productive or trial):

 (For the productive landscape) Connecting to SAP HANA Databases via the Eclipse IDE

 (For the trial landscape) Connecting to SAP HANA Schemas via the Eclipse IDE

3. Create a Subpackage

Procedure

1. In the Eclipse IDE, open the SAP HANA Development perspective.

2. In the Systems view, select the Content/p1234567890trial/myhanaxs node.

3. From the context menu, choose New Package .

The New Package dialog box appears.


4. In the Name field, p1234567890rial.myhanaxs. is already entered. Add the new package name (for

example, "hello") to the end of the full package path: p1234567890trial.myhanaxs.hello

5. Click in the Description field to automatically copy across the package name.

6. Choose OK to confirm.

The new subpackage hello is added to the package hierarchy below the myhanaxs package.

4. Create a Repository Workspace

Procedure

1. In the Repositories view, choose the Create Repository Workspace button.

2. If your SAP HANA system is not listed in the SAP HANA System list, choose the Add System button

and add it to the list. If you have more than one SAP HANA system, make sure that the applicable

system is selected under SAP HANA System.


3. Enter the workspace name myhanaxs. In this example, the workspace name is the same as the root

package name.

4. In the Workspace Root field, browse to select the folder that will contain the workspace you create in

this step. The tutorial uses C:\HANAXSws.

5. Choose Finish.

The workspace now appears in the Repositories view. A folder has also been added to your local file

system: C:\HANAXSws\myhanaxs. It will contain all your development files.

5. Create an XS Project

Procedure

1. In the Project Explorer view, choose File New Project SAP HANA Application

Development XS Project and then choose Next.

Note

After installing the SAP HANA tools for SAP HANA Cloud Platform, you may not see the XS Project

wizard in the list of wizards. To fix this:


a. Close the Eclpse IDE.
b. Add a line -clean to the eclipse.ini file, or use -clean at the command line to start Eclipse.

c. Start the Eclipse IDE again. The XS project wizard will now show up.
d. Remove the -clean option again.

2. Enter hello as project name, and choose Next (keeping all default settings).

3. Select the repository workspace you created. As Repository Package, choose Browse and navigate to

the p1234567890trial.myhanaxs.hello package. Choose Next.

Restriction

You cannot use the root repository package here. You need to use the correct subpackage instead

(such as p1234567890trial.myhanaxs.hello). Otherwise, you will get errors during activation.

4. Deselect the default file creation options (XS Application Access (.xsaccess) and XS Application

Descriptor (.xsapp)) and choose Finish.

Results
Note

If you experience project errors due to unsupported encoding, use one of the following options to fix the

problem:

 Quick fix that automatically changes the encoding of individual files to UTF-8

 Project settings that change the default encoding of the entire XS project to UTF-8 ( context

menu Properties Resource Text file encoding Other UTF-8 )

6. Create the Application Descriptors and Role

Procedure

 Application descriptor

1. In the Project Explorer view, select the hello project and choose File New File .
2. Enter the file name .xsapp and choose Finish.
Note that the file name is just a file extension and that the file does not have any content.

 Application access file

1. In the Project Explorer view, select the hello project and choose File New File .
2. Enter the file name .xsaccess and choose Finish.

Note that the file name is just a file extension.

3. Copy the following code into the .xsaccess file and save:
4. {
5. "exposed" : true,
6. "default_file": "hello.xsjs"
}

This configuration determines that the application will be exposed in the Internet via HTTPS.

Although no authentication is specified, all SAP HANA XS applications on a trial SAP HANA

instance use SAML2. For the productive SAP HANA instance, SAML will be enabled in a later

step. Application users will then be authenticated by the SAP ID service.

 Application privileges file

1. In the Project Explorer view, select the hello project and choose File New File .
2. Enter the file name .xsprivileges and choose Finish.

Note that the file name is just a file extension.

3. Copy the following code into the .xsprivileges file and save:
4. { "privileges" :
[ { "name" : "Basic", "description" : "Basic usage privilege"
} ]}

 Role

1. In the Project Explorer view, select the hello project and choose File New File .
2. Enter the file name model_access.hdbrole and choose Finish.

3. Copy the following code into the file:


4. role <package name>::model_access {
5. application privilege: <package name>::Basic;
}
6. Replace <package name> with the SAP HANA package where the project is shared. For the trial

SAP HANA instance, it should look something like this: p1234567890trial.myhanaxs.hello. Then

save the file.

7. Create the JavaScript File

Procedure

1. In the Project Explorer view, select the hello project and choose File New File .
2. Enter the file name hello.xsjs and choose Finish.

3. Copy the following JavaScript code into the file and save:
4. $.response.contentType = "text/html";
5. var output = "Hello, " + $.session.getUsername() + " <br><br>";
6. var conn = $.db.getConnection();
7. var pstmt = conn.prepareStatement( "SELECT CURRENT_USER FROM DUMMY" );
8. var rs = pstmt.executeQuery();
9. if (!rs.next()) {
10. $.response.setBody( "Failed to retrieve data" );
11. $.response.status = $.net.http.INTERNAL_SERVER_ERROR;
12. }
13. else {
14. output = output + "This is the response from my SQL. The
current user is: " + rs.getString(1);
15. }
16. rs.close();
17. pstmt.close();
18. conn.close();
$.response.setBody(output);

The code does the following:

o Opens a connection to the database


o Prepares and runs an SQL statement on the system table DUMMY, which returns as a response

the current user.

o Adds the results to the response so that it can be displayed in the Web browser

19. To activate the files in the SAP HANA repository, select the files and from the context menu choose

Team Activate .

The application is now running on the XS engine.

8. Grant the Role to the User


To enable other users to access your application, you need to grant them the model_access role that you

created in section 6.

Procedure

 Trial SAP HANA instance

1. In the Systems view, select your system and from the context menu choose SQL Console.
2. In the SQL console, enter the following, replacing <SAP HANA Cloud user> with your user:

call
"HCP"."HCP_GRANT_ROLE_TO_USER"('p1234567890trial.myhanaxs.hello::mod
el_access', '<SAP HANA Cloud user>')

3. Execute the procedure. You should see a confirmation that the statement was successfully

executed.

 Productive SAP HANA instance

1. In the Systems view, expand Security Users and then double-click your user ID.

2. On the Granted Roles tab, choose the + (Add) button.

3. Select the model_access role in the list and choose OK. The role is now listed on the Granted

Roles tab.

4. Choose Deploy in the upper right corner of screen. A message confirms that your user has been

modified.

Note (Relevant for Trial landscape only) If you refactor existing SAP HANA database views or create
new ones on your Trial account, you need to refactor/recreate them in the "_SYS_BIC" schema

instead of your account-specific schema.


9. Test Your Application

Procedure
Open the cockpit and proceed as described in: Launching SAP HANA XS Applications

You will be authenticated by SAML and should then see a text similar to the following:
Hello, p1234567890,
This is the response from my SQL. The current user is: p1234567890

Copyright | Important Disclaimers and Legal Information

S-ar putea să vă placă și