This post was originally posted on SAP HANA on the moon (with Lua).
Lua is a lightweight multi-paradigm programming language designed as a scripting language with extensible semantics as a primary goal. Lua is cross-platform since it is written in ANSI C, and has a relatively simple C API.
If you wonder about the title…well…Lua means Moon in Portuguese -;)
Lua is very powerful but easy to learn and use. If you already know Python or Ruby, you will be just fine…
Before we move on…I want to share a little joke…which is not specific to Lua only…but still funny -;)
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 -;)
Finally create a file called “flights.xsodata” with the following code
Activate your project and launch it on your browser, you should see something like this…
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"; } |
The SAP HANA part is done…so we can move into the Lua part…
Before we continue…I will recommend you to install a couple of things…if you’re on Linux…install this…
sudo apt-get install lua5.2
And then follow the instructions to install LuaRocks a package manager for Lua…
Install the following packages…
sudo luarocks install luasec
sudo luarocks install luajson
sudo luarocks install luasocket
To code for Lua, you can use any editor that you like…Copy and paste the following code…
Before we continue…I will recommend you to install a couple of things…if you’re on Linux…install this…
sudo apt-get install lua5.2
And then follow the instructions to install LuaRocks a package manager for Lua…
Install the following packages…
sudo luarocks install luasec
sudo luarocks install luajson
sudo luarocks install luasocket
To code for Lua, you can use any editor that you like…Copy and paste the following code…
Lua_HANA.lua |
---|
http = require("socket.http") mime = require("mime") ltn12 = require("ltn12") json = require("json") h = {Authorization = "Basic " .. (mime.b64("SYSTEM:YourPassword")), ["Content-Type"] = "application/json", ["content-length"] = jsonsize } local resp ={} ok, code, headers = http.request{url = "http://YourServer:8000/Flights/flights.xsodata/FLIGHTS?$format=json", redirect = true, method = "GET", headers = h, source = source, sink = ltn12.sink.table(resp)} local dec = json.decode(resp[1]) local d = dec["d"]["results"] for i = 1, #d do io.write(d[i]["CARRNAME"]," : ",d[i]["PRICE"], "\n") end print("") |
Doing this integration with Lua was quick and easy…Lua is a really nice language with some really nice features that can’t be found on other languages…
Greetings,
Blag.
Development Culture.
No hay comentarios:
Publicar un comentario