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

How to create a new OData service using the Web-Based Workbench

$
0
0

Introduction

In this blog we will see how to create a very simple table on the HANA XS platform and how to build, on top of it, an OData service we can use to consume our data. For this exercise we will use the HANA Trial landscape publicly available here. You just need to register and get your access to the full trial HANA platform.

For this walkthrough we won't use any external development tool like Eclipse, but we will rather take advantage of the integrated SAP HANA Web-based Development Workbench.

 

 

Prerequisites

No particular prerequisites are needed for this blog: just a web browser and an account on the HANA Trial landscape.

Once you have registered to this service you will receive a username, usually a character and a number (i.e. i012345 or p01234567). You will be also assigned with an account name, which is normally the concatenation of your username with the word "trial".

So in this document we will refer to

<your_HANA_trial_username> as the username you received when you registered to the Trial landscape

<your_HANA_trial_account> as "<your_HANA_trial_username>trial"

 

Pay also attention to the fact that here we have used the following hardcoded names:

 

dev for the instance name

Employees for the name of the table

empdemo for the name of the application

 

If you want to have different names for these three objects, please remember to change them accordingly in all the SQL statements listed here.

 

 

Walkthrough

This is the list of steps we will go through:

 

  1. Creation of a new HANA XS instance
  2. Creation of a new HANA Table
  3. Creation of a new application
  4. Definition the application permissions
  5. Creation the OData service

 

Let's get started!

 

1. Creation of a new HANA XS instance

 

  • Click on the HANA Instances menu and click on New Trial Instance

2015-03-16_10-12-02_01.png

 

  • Enter the name of the new instance (i.e. "dev") and click on Save

2015-03-16_10-12-54.png


  • The instance has been created, now click on the SAP HANA Web-based Development Workbench link

2015-03-16_10-17-21.png

 

  • From the top menu click on the down arrow and select Catalog

2015-03-16_10-44-49.png

 

  • Click on the SQL button on the top menu

2015-03-16_10-54-23.png

 

  • Paste the following select statement  in the right side text area and click on the Execute button

 

SELECT SCHEMA_NAME FROM "HCP"."HCP_DEV_METADATA";

2015-03-16_10-55-42.png

 

  • You get a name of the schema we need to use

2015-03-16_10-56-08.png

 

  • Copy this name and paste it in a separate text file in order to keep it for later use.


NOTE: From this moment on when mentioning <NEO_schema_name> we'll refer to this string.

 

 

2. Creation of a new HANA Table

You should be still on the Catalog page. If you are not, please reopen it.

 

  • Delete everything in the SQL text area and paste there the following query:

 

CREATE COLUMN TABLE "<NEO_schema_name>"."Employees" (  "id" VARCHAR(256) NOT NULL ,  "firstName" NVARCHAR(256),  "lastName" VARCHAR(256),  "address" VARCHAR(256),  "age" INTEGER CS_INT,  PRIMARY KEY ("id")
);

01.png

NOTE: Remember to replace the string <NEO_schema_name> with the name of your schema you found at the previous step

 

  • Click on the Execute button again: a new table will be created under the selected schema.


  • From the Catalog Explorer on the left side expand the name of your schema: you will find the new table under the Tables branch

2015-03-16_11-09-05.png


  • We want now to put some sample records in this table. Delete once again the content of the SQL text area.

Paste the following lines:

INSERT INTO "<NEO_schema_name>"."Employees" VALUES('1','John','Smith','Downtown',35);
INSERT INTO "<NEO_schema_name>"."Employees" VALUES('2','Andrew','Parker','Neverland',26);
INSERT INTO "<NEO_schema_name>"."Employees" VALUES('3','Sandra','Stone','Longbridge',23);

For all the three lines you need to replace the string <NEO_schema_name> with the name of your schema

2015-03-16_11-22-36.png

 

  • Click on the Execute button. The statement will be executed.

 

  • If you right click on the name of the table in the Catalog Explorer you can choose Open Content to display the content of the table

2015-03-16_11-26-20.png

 

  • The content of the new table is displayed:

2015-03-16_11-26-53.png

 

 

3. Creation of a new application

  • Go back to the SAP HANA Web-based Development Workbench tab

 

  • Expand the Content\<your_HANA_trial_account> branch and select the dev package

 

  • Right click on it and choose Create Application

2015-03-16_11-43-43.png


  • Enter the application name (i.e. "empdemo") and select as template the "Blank Application - (xsapp and xsaccess)"; then click on Create

2015-03-16_11-44-57.png


  • Once the new application is created you should find 3 new files: .xsaccess, .xsapp, index.html

2015-03-16_11-48-58.png

 

  • Delete the index.html file as we don't need it for this exercise

02.png

 

 

 

4. Definition the application permissions

  • Right click on the empdemo application and select Create File

2015-03-16_11-55-59.png

 

  • Create a new file named .xsprivileges and enter the following content

 

{
"privileges": [  {"name": "Execute", "description": "Execute"}
]
}
  • Save the file. You shouldn't get any error in the console

03.png

 

  • Click on the .xsaccess file, it will be opened in the editor. Change the entire content to the following:

 

{
"exposed": true,
"authentication": [{"method": "Form"}],
"authorization": ["<your_HANA_trial_account>.dev.empdemo::Execute"],
"prevent_xsrf": true
}

Remember to replace the string <your_HANA_trial_account> with your real account

2015-03-16_22-05-29.png

 

  • Save the file. Again, you shouldn't get any error in the console.

 

  • Create a new file named user.hdbrole with the following content

 

role <your_HANA_trial_account>.dev.empdemo::user
{  catalog schema "<NEO_schema_name>": CREATE ANY, DROP, INDEX, SELECT, INSERT, UPDATE, DELETE;  application privilege: <your_HANA_trial_account>.dev.empdemo::Execute;
}

Remember to replace the string <NEO_schema_name> with the name of your schema

2015-03-16_22-10-51.png

 

  • Save the file. Check that you don't get any error in the console.

 

  • Open again the Catalog, click on the SQL button and paste in the SQL text area the following statement

 

CALL HCP.HCP_GRANT_ROLE_TO_USER('<your_HANA_trial_account>.dev.empdemo::user','<your_HANA_trial_username>');

Remember to replace the string <your_HANA_trial_account> with your real account and <your_HANA_trial_username> with your username

04.png

 

  • Click on the Execute button to execute the query. It might take some time

 

  • Close the Catalog tab

 

 

5. Creation the OData service

  • Go back to the SAP HANA Web-based Development Workbench

 

  • Right click on the empdemo application and select Create File

2015-03-16_11-55-59.png

 

  • Create a new file named services.xsodata and paste the following content there

 

service {    "<NEO_schema_name>"."Employees" as "Employees"        create forbidden        update forbidden        delete forbidden;
}

Replace again the string <NEO_schema_name> with the name of your schema

05.png

  • Save the file. You shouldn't get any error in the console

 

  • Select the services.xsodata file and click on the Execute button:

08.png

 

  • You should get the working service. This is the URL you can use for addressing your new service.

2015-03-16_12-04-50.png

  • You can also get the metadata file by appending the suffix "/$metadata" to the previous URL:

06.png

That's all folks!


Viewing all articles
Browse latest Browse all 3285

Trending Articles



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