SAP HANA OData Services and JQuery

Introduction

In my previous post I described how I came across with the UI Toolkit for Information Access while building the content of our HANA workshop Get Your Hands-on IT for SAP HANA. I found the UI toolkit to be a very cool feature so I decided to dive deeper and to my surprise I found that HANA implements OData to expose the data in the HANA db to the web. In this post I’m going to describe how to build a simple web application using JQuery that consumes data stored in a HANA db using OData Services.

The Open Data Protocol (OData) is a web protocol that will allow you to query and update data using web technologies like HTTP and JSON. In other words, OData allows you to query a data source over the HTTP protocol and get the results back in JSON format. OData works pretty much the same as ODBC, this means you can perform any operation to manipulate the data in your data source, like Insert new rows, run Queries, Update rows and Delete rows. Each of these operations are mapped to an HTTP verb, see table below:

HTTP Verb

CRUD Method

POST

Create (Insert new rows)

GET

Read (Query)

PUT

Update

DELETE

Delete

We are going to use a HANA Attribute view as the data source, so let’s focus on learning how to query the HANA Attribute View over the HTTP protocol, but before we get started, let’s talk about the prerequisites.

Use HTTP Get to Retrieve Data

Like I already said I’m going to use a HANA Attribute view as the data source, so the first thing we need to do is create it. In my case I will use an Attribute View that contains all the different airports from around the world with the corresponding Latitude and Longitude. Once you are done creating the attribute view open the following URL to make sure it has been exposed using the SAP HANA REST service:

There are multiple query string parameters defined by OData that you can use to query your entity set, not all of them are supported by SAP HANA, but I will outline the most important ones that are supported by SAP HANA:

You can play around with the different query string options to get familiar or you can refer to the OData official documentation for more information

Building a Simple Web Application using JQuery

JQuery is a JavaScript library that simplifies the creation of HTML documents. JQuery is capable of handling data in JSON format using the getJSON() function. The first thing we need to do is set up a development environment in order to be able to create the web application. If you don’t know how to do this you can refer to my previous post or to the SAP HANA official documentation. Once you are done setting up your development environment create a new HTML document and name it jquerysample.html. The code inside the html document should look like this:

Let’s take a look at the code to understand what we are doing here. First we implement the JQuery library in the header of the page:

Then we create a <div> that will be the placeholder to show the data that we query from the HANA db:

Then we query the data from the HANA db using the getJSON() function. You can use any of the query string options that we discussed above:

Then we create a <table> using JQuery inside the <div> and we loop thru every column and every row that we are bringing from the HANA db:

Once you are done commit the newly created web page back to the HANA server and activate it using the regi commit and regi activate commands. The resulting web page will looks like this:

Here we are showing Airports with the corresponding Airport ID, Latitude and Longitude. Not very fancy right? Well, I wanted to start with something simple so that you could easily follow it. Now that you understand the concept, let’s do something a little bit more complicated. In our next example we are going to use the Google Maps JQuery library to show the Airports in a map, so you will need to download the Google Maps library from here. Unzip the downloaded file, copy the “UI” folder inside your development environment and create a new web page called gmapssample.html. The code should look like this:

Let’s take a look at the code. First we implement the Google Maps API, the JQuery library and Google Maps JQuery library that we downloaded:

Then we create a <div> element that will be the container for our map:

Then we initialize the map with a hardcoded latitude and longitude:

Then we use the getJSON() function to query the data in the Attribute View, again you can use any of the query string options that I described above, in this case I’m filtering by column COUNTRYID equal 218 (United States):

Then we loop thru the data and add markers in our map for every Airport that we retrieve from the HANA db. We also use the InfoWindow to show the Airport Name:

Again, once you are done commit the newly created web page back to the HANA server and activate it using the regi commit and regi activate commands. The resulting web page will looks like this:

These are two very basic examples of how to use JQuery with SAP HANA OData Services, following this same concept you can build more complicated things and even build mobile applications. I hope you liked it and don’t forget to drop by our Get Your Hands-on IT for SAP HANA page to see what the workshop is about, you won’t find any other offering like this!