miércoles, 23 de septiembre de 2015

(Defining SAP HANA (OData) with Racket)

This post was originally posted on (Defining SAP HANA (OData) with Racket).

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.

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 -:)


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.


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…


The 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…


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…

Greetings,

Blag.
Development Culture.

No hay comentarios: