Skip to Content
Author's profile photo Alvaro Tejada Galindo

Getting functional with SAP HANA and Erlang

/wp-content/uploads/2015/11/d_shop_blog_logo_827658.jpg

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



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.

Erlang_logo.png

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


Hipster_Erlang.jpg


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.

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.xsodata

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.jpg

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.

Options.jpg

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://YourHANAServer:8000/Flights/flights.xsodata/FLIGHTS?$format=json“,

                          [{“Authorization”, “Basic ” ++ base64:encode_to_string(“SYSTEM:YourPassword”)}]}, [], []),

  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.

Erlang_HANA.jpg

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

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.