lunes, 9 de noviembre de 2015

Getting functional with SAP HANA and Erlang

This post was originally posted on Getting functional with SAP HANA and Erlang.


Erlang is a general-purpose concurrent, garbage-collected programming language and runtime system. And it’s also Functional, with eager evaluation, single assignment and dynamic typing. Did I forget to mention that it’s also distributed, concurrent and fault-tolerant.


Big description, huh? Well…Erlang has been around for the almost past 30 years…


So…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.


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 Erlang part…

Erlang, as opposed to many other languages, doesn’t have a Package Manager, so we need to only download the package that we want and compile it -;)

To code for Erlang I use one of the most awesome editors I have ever found…Geany. It comes almost pre-configured to handle a lot of programming languages, so you will be ready to code for Erlang.

We need to download mochijson2.erl and save it on our project folder, then we can simply compile it by calling

erlc mochijson2.erl

From the terminal or press the “Compile the current file” button. First one on the image.


With that, we should be more than ready to start coding -:) Copy and paste the following code…

Erlang_HANA.erl
-module(erlang_HANA).
-export([getOData/0,showInfo/5]).

showInfo(Carriers, Prices, Acc, Len, Ctr) when Ctr =< Len ->
 showInfo(Carriers, Prices,  Acc ++ [binary_to_list(lists:nth(Ctr,Carriers))] ++ [": "] ++
   [binary_to_list(lists:nth(Ctr,Prices))] ++ ["\n"], Len, Ctr + 1);
showInfo(_, _, Acc, Len, Ctr) when Ctr > Len ->
 io:format("~s",[Acc]).
 
getOData() ->
 inets:start(),
 Response = httpc:request(get, {"http://54.65.196.224:8000/Flights/flights.xsodata/FLIGHTS?$format=json", 
                          [{"Authorization", "Basic " ++ base64:encode_to_string("SYSTEM:Blag1977")}]}, [], []),
 Flights = mochijson2:decode(response_body(Response)),
 {struct, JsonData} = Flights,
 {struct, D} = proplists:get_value(<<"d">>, JsonData),
 Results = proplists:get_value(<<"results">>, D),
 Carriers = [proplists:get_value(<<"CARRNAME">>, S) || {struct, S} <- Results],
 Prices = [proplists:get_value(<<"PRICE">>, S) || {struct, S} <- Results],
 showInfo(Carriers, Prices, "", length(Carriers), 1).

response_body({ok, { _, _, Body}}) -> Body.

Save your file and press the “Run or view the current file” button. The last one from the image that displays two engines.


You will need to use the format “name_of_file:method()” to load it into memory and execute it -;)

Greetings,

Blag.
Development Culture.

No hay comentarios: