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:
- Creation of a new HANA XS instance
- Creation of a new HANA Table
- Creation of a new application
- Definition the application permissions
- Creation the OData service
Let's get started!
1. Creation of a new HANA XS instance
- Open your HANA Trial Cockpit https://account.hanatrial.ondemand.com/cockpit#
- Click on the HANA Instances menu and click on New Trial Instance
- Enter the name of the new instance (i.e. "dev") and click on Save
- The instance has been created, now click on the SAP HANA Web-based Development Workbench link
- From the top menu click on the down arrow and select Catalog
- Click on the SQL button on the top menu
- 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";
- You get a name of the schema we need to use
- 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") );
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
- 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
- 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
- The content of the new table is displayed:
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
- Enter the application name (i.e. "empdemo") and select as template the "Blank Application - (xsapp and xsaccess)"; then click on Create
- Once the new application is created you should find 3 new files: .xsaccess, .xsapp, index.html
- Delete the index.html file as we don't need it for this exercise
4. Definition the application permissions
- Right click on the empdemo application and select Create File
- 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
- 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
- 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
- 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
- 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
- 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
- Save the file. You shouldn't get any error in the console
- Select the services.xsodata file and click on the Execute button:
- You should get the working service. This is the URL you can use for addressing your new service.
- You can also get the metadata file by appending the suffix "/$metadata" to the previous URL:
That's all folks!