jueves, 28 de junio de 2012

When SAP HANA met R - Bring home your graphics


A couple of days ago, I started to think about SAP HANA and R on Amazon Web Services...as far as I know, graphics can't get generated using this kind of integration because the graphic will get generated on the server and could not make the trip back into HANA Studio...so I kept thinking as said to myself..."Wouldn't it be a good idea to use a Linux command to send my email the graphics generated in the R server?"...I had a deal for sure...

I spend a couple of days trying to install X11 on my SUSE box...until I discovered...that it's a headless server and doesn't provide any support for X11...that really bring me down as I tried to hard...anyway...as nothing can stop my ideas from becoming some real...I thought of using PDF generation as it doesn't need X11 at all...but...today...after doing some more research...I discovered that the Cairo library supports image creation without the need of X11...

If you haven't installed SAP HANA and R on Amazon Web Services, read this... When SAP HANA met R - First kiss.

Let's take a look at what we need to do:

Install Cairo
zypper install cairo-devel


Install Cairo library on R
>R
>install.packages("Cairo")

Start the Rserve server
R CMD Rserve --RS-port 6311 --no-save --RS-encoding "utf8"

Using the same user you used for starting the Rserver Server, create a folder and change it's permissions...

Change permissions
>chmod 777 Blag

Now, we can move to HANA Studio.
We need to first create a table called TICKETS_BY_YEAR_MONTH.




Create_and_send_graphics
drop procedure GetTicketsByYearMonth;
drop procedure Generate_Graphic;
drop procedure Get_Tickets;

CREATE PROCEDURE GetTicketsByYearMonth(IN var_year NVARCHAR(4),IN var_month NVARCHAR(2))
LANGUAGE SQLSCRIPT AS BEGIN
select count(bookid), carrid from sflight.snvoice
where year(fldate) = VAR_YEAR
and month(fldate) = VAR_MONTH
group by carrid
into TICKETS_BY_YEAR_MONTH;
END;

CREATE PROCEDURE Generate_Graphic(IN tickets_year TICKETS_BY_YEAR_MONTH, OUT result TICKETS_BY_YEAR_MONTH)
LANGUAGE RLANG AS
BEGIN
setwd("/Blag")
library("Cairo")
tickets=as.integer(tickets_year$TICKETS)
carriers=as.character(tickets_year$CARRIERS)
Cairo(600,600,file="Tickets.png",type="png",bg="white")
barplot(tickets,names.arg=carriers,main="Tickets for December 2011")
dev.off()
command<-"uuencode Tickets.png Tickets.png | mail -s 'Tickets December Report' atejada@gmail.com"
system(command,intern=TRUE)
result<-data.frame(TICKETS=tickets,CARRIERS=carriers)
END;

CREATE PROCEDURE Get_Tickets()
LANGUAGE SQLSCRIPT AS
BEGIN
CALL GetTicketsByYearMonth('2011','12');
Tickets = SELECT * FROM TICKETS_BY_YEAR_MONTH;
CALL Generate_Graphic(:Tickets,TICKETS_BY_YEAR_MONTH);
END;

CALL Get_Tickets();


When we execute this, a couple of things are going to happen...


  • We're going to get the amount of tickets per airline and per year and month. We're going to save this info in a table.
  • We're going to read this information, create a graphic and save it as an .png
  • We're going to send this graphic to ourselves by email.

After execution, we're going to see a nice email...


Cool, huh? Now, we can create graphics on SAP HANA and R on Amazon Web Services -;)


No hay comentarios: