In previous articles Introduction to OData
and SAP HANA OData Service
, we explained
- What is OData Service?
- How does OData Service works in SAP HANA?
In this article we will show you how to create a HANA OData (.xsodata) service in simple steps.
Note: In this example we will use HANA Studio to create OData Service. You can also create it using HANA Web-based tools. For HANA Web-based tool, you should first read Create Your First HANA XS Application using HANA Web-Based Tool
Create a Table to be used in OData Service:
Let us first create a table which we will call using OData service.
Copy paste the below SQL script in HANA Studio SQL Console and execute.
--REPLACE <YOUR SCHEMA> WITH YOUR SCHEMA NAME
-- Create Product table
create column table "<YOUR SCHEMA>"."PRODUCT"(
"PRODUCT_ID" INTEGER null primary key,
"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');
Steps to create HANA OData Service:
- Create a XS project as mentioned in the article Create Your First HANA XS Application using HANA Studio
- Right click on the project and select New à XS OData File. Give file name as “ProductODataService” and click on Finish.
- Copy paste the below code. Make sure you change <YOUR_SCHEMA> to the schema where PRODUCT table was created.
"<YOUR_SCHEMA>"."PRODUCT" as "Product";
- Right click and select Team à Activate. This will activate the file.
- Right click and select Run As à XS Service. This will open the XSOData service in default browser.
- Enter user id and password of HANA system. You will see the result as
- Note that the URL is constructed as http://<host>80<instance no>/<package path>/xsodata_file.
Done!! You have successfully created your first OData service.
If you are using Chrome, then you may install 2 plugins to see the data in a formatted way.
- XML Tree: For XML
- JSONView: For JSON
A walk-through the OData Service:
The above OData service URL shows the OData definition.
To see the metadata information of the OData service, we can add $metadata parameter at the end of the URL.
http://<host>80<instance no>/<package path>/xsodata_file/$metadata
To see the data in XML format, you need to specify the Entity name as parameter.
Remember that in xsodata file we specified the entity name as “Product”.
http://<host>80<instance no>/<package path>/xsodata_file/entity_name
To see data in JSON format, you need to specify $format=json as parameter.
http://<host>80<instance no>/<package path>/xsodata_file/entity_name/?$format=json
How to consume this OData Service:
The OData can be called from any SAPUI5 or HTML5 application. This can also be called from any other UI application (in Java, .Net etc.) or any tools that supports OData.