Skip to Content
Author's profile photo Alvaro Tejada Galindo

(Defining SAP HANA (OData) with Racket)

/wp-content/uploads/2015/09/d_shop_blog_logo_796256.jpg

If you are an SAP Employee, please follow us on Jam.


Racket is a full spectrum programming language that goes beyond Lisp and Scheme with dialects that support objects, types, laziness and more. And that more means Functional, Procedural, Object Oriented, Logic, Reflective and Meta.

Racket_Logo.png

Basically…Racket is the new name of PLT Scheme. And PLT Scheme comes from the Lisp family.

So, before we move on…I want to share an XKCD comic that brings some light about Lisp 🙂

XKCD_lisp_Comic.jpg

Well…no example or demonstration would be complete if we didn’t hook it up with SAP HANA, right? So…let’s go and do it -;)

First, we need to create a Calculation View and call it “FLIGHTS_BY_CARRIER”. It will be composed of two tables, SCARR and SFLIGHT.

First, we need to create a Join object and link the table by MANDT and CARRID. From here select the following fields as output MANDT, CARRID, CARRNAME, PRICE and CURRENCY.

Then create an Aggregation object selecting the fields CARRNAME, PRICE (As Aggregated Column) and CURRENCY. Filter the CURRENCY field by ‘USD’.

Then create a Projection object and select only PRICE and CARRNAME.

On the Semantics object make sure to select “CROSS CLIENT” as the Default Client.

Calculation_View.jpg

Now, switch to the SAP HANA Development View and create a new repository. Call it “Flights”.

Create a new “XS Engine” project and call it “Flights” as well. Link it to the “Flights” repository.

Create an empty “.xsapp” file.

Create a file called “.xsaccess” with the following code.

.xsaccess

{

“exposed” : true,

“authentication” : [ { “method” : “Basic” } ]

  }

Finally create a file called “flights.xsodata” with the following code

flights.xodata

service {

          “Blag/FLIGHTS_BY_CARRIER.calculationview” as “FLIGHTS” keys generate local “Id”;

}

Activate your project and launch it on your browser, you should see something like this…


XSProject.jpgThe SAP HANA part is done…so we can move into the Racket part…

Racket is a very complete programming language…so no external packages are needed…


To code for Racket I use the best Racket editor “Dr. Racket” that is actually embedded in the Racket installer…so again…no need to worry 🙂

Copy and paste the following code…

Racket_HANA.rkt

#lang racket

(require json)

(require net/base64)

(require net/url)

(define make-auth-header

     (list (string-append

“Authorization: Basic “

(bytes->string/utf-8

        (base64-encode

(string->bytes/utf-8

(string-append “SYSTEM” “:” “YourPassword”)))))))

(define (get_path path params)

(port->string(get-pure-port path params)))

(define json (string->jsexpr (get_path (string->url “YouServer:8000/Flights/flights.xsodata/FLIGHTS?$format=json”) make-auth-header)))

(define results (hash-ref (hash-ref json ‘d) ‘results))

(define (show_info results len ctr)

  (cond [(or (= ctr len) (< ctr len))

(string-append (hash-ref (list-ref results ctr) ‘CARRNAME) “: “

(hash-ref (list-ref results 0) ‘PRICE) “\n”

(show_info results len (add1 ctr)))]

        [(> ctr len)

(string-append “” “\n”)]))

  (display (show_info results (sub1 (length results)) 0))

Now you see it…Racket is all about parenthesis 🙂 Anyway…press the run button and you will have this…

Racket_HANA.jpg


I have been interfacing SAP HANA OData with a lot of languages…and so far…Racket has been one of my fastest implementations…while it’s hard to get used to all these weird parenthesis…it’s a very nice and powerful language…and thing get done faster that one could imagine…

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Alvaro,

      Wonderful blog. I am trying to replicate from my end and running into error "The word Blag is not spelled correctly". I am new to coding, can you please throw some light on this ?

       

      Author's profile photo Alvaro Tejada Galindo
      Alvaro Tejada Galindo
      Blog Post Author

      Thanks Yoga 🙂 Regarding your question..."Blag" is the name of the schema that I used for my SAP HANA OData service...so you will need to use the name of the schema that you have created on your SAP HANA system 🙂

      Greetings,

      Blag.

      Development Culture.