viernes, 10 de enero de 2014

MeteorJS and SAP HANA

If you thought that I was going to learn MeteorJS and not try to link with SAP HANA...then you were dead wrong -;)

Of course...I'm far from being a good Meteor developer...so please bear with me -:( I'm also not very good at NodeJs...so...you know...this is just me playing around -:)

So...let's begin...there's a lot of ways that we can use to connect Meteor and SAP HANA...but...what if we can somehow do it with a native connection?

If you haven't visit SAP Developers on GitHub yet...I recommend you to do it...we have some really nice Open Source projects in there...

Anyway...in there you will find a hidden gem simply called node-hdb which is course...a NodeJS native connector for SAP HANA...awesome, huh?

Now...you will ask...why does NodeJS has to do with Meteor? Easy...Meteor uses NodeJs in the background...

Checking the web...I found this awesome Meteor Hack called Complete NPM integration for Meteor which is just great...basically...it allows us to include NodeJS packages in the underlying NodeJS service of our Meteor installation...

Simply do this...

Getting node-hdb into Meteor
npm install -g meteor-npm
cd "your_project"
meteor-npm

This will create an "npm" folder into your project...now...simply create a file called packages.json in your root folder...

Packages.json
{
 "hdb": "0.2.0"
}

With that...we're ready to rock with Meteor -;)

First...let's do the html part...

HANA_Test.html
<head>
  <title>SAP HANA and MeteorJS</title>
</head>

<body>
  {{> CallHANA}}
</body>

<template name="CallHANA">
<DIV ID'block' ALIGN='CENTER'>
<H1>Calling SAP HANA from MeteorJS</H2>
 <TABLE BORDER='1'>
 <TR><TH>Carrier</TH><TH>Connection</TH>
     <TH>Country From</TH><TH>City From</TH>
     <TH>Airport From</TH><TH>Country To</TH>
     <TH>City To</TH><TH>Airport To</TH>
     <TH>Distance</TH></TR>
 {{#each Spfli}} 
  <TR>
   <TD>{{CARRID}}</TD><TD>{{CONNID}}</TD>
   <TD>{{COUNTRYFR}}</TD><TD>{{CITYFROM}}</TD>
   <TD>{{AIRPFROM}}</TD><TD>{{COUNTRYTO}}</TD>
   <TD>{{CITYTO}}</TD><TD>{{AIRPTO}}</TD>
   <TD>{{DISTANCE}}</TD>
  </TR>
 {{/each}}
 </TABLE>
</DIV> 
</template>

Then...create two folders...one called "server" and the other called "client"...

Inside the "server" folder create a file called "HANA_TestServer.js"

HANA_TestServer.js
var myjson = [];

Meteor.methods({
    'CallHANA': function CallHANA() {
  hdb    = Meteor.require('hdb');
    client = hdb.createClient({
    host     : 'hanasvr-XX.sapdevcenter.com',
    port     : 30015,
    user     : 'SYSTEM',
    password : 'XXXXXX'
  });

  client.connect(function (err) {
     if (err) {
       return console.error('Connect error', err);
     } 
      client.exec('select top 10 * from SFLIGHT.SPFLI', function (err, rows) {
       client.end();
       if (err) {
          return console.error('Execute error:', err);
       }
        var count = Object.keys(rows).length
        for(var i=0;i<count;i++){
         myjson.push(rows[i]); 
        } 
      });
  });
  return myjson;
 }  
  });

Then inside the "client" folder create a file called ""HANAClient.js"...

HANAClient.js
 Meteor.startup(function () {
    Meteor.call("CallHANA", function (error, result) {
             Session.set("myjson", result);
            });
    });

    Template.CallHANA.Spfli = function () {
     row = Session.get("myjson");
     return row;
    };

With that ready...simply call up your meteor application and after you refresh your page...I need to work on the reactivity part -:( I'm sure a single button calling the function will do the trick...but I'm tired and I'm lazy...The result from the query will be shown -;)


Not that fancy I know...but for the first step...I guess it's good enough -;)

Greetings,

Blag.
Developer Empowerment and Culture.

lunes, 6 de enero de 2014

Machine Learning with R - Book Review

I have to admit it...I'm an R junkie...since the first time I started learning R...it has become an addiction to me...I try to solve every problem with R...which is obviously not the best way to go...but my rule of thumb is..."Try with R first...otherwise...just use something else and then go back to R" -;)

This time I was really excited to read a new R book (well...maybe not new...but new for me) called Machine Learning with R...


For me this book should be called "The Big Book for R nerds"...with 396 pages...this book is just beautiful, amazing and one of the best R books I have ever read...

Of course...keep in mind that is not a book for beginners...you need to have previous R experience to fully understand everything...so if you're not a R advocate...please help yourself and read the also awesome The R Inferno...

The book of course, contains a small introduction to R principles and most used commands like Vectors, Factors, Lists, Data Frames and data manipulation.

When the book really gets interesting is when the Machine Learning gets introduced...


It a nutshell...a Machine Learning algorithm will take input...learn something and the toss out a result that will help us make a decision...simply pure magic -:)

Some of the algorithms covered in this book...and covered in a really easy and digestible way with some of the best examples you could think of...are these...


  • Nearest Neighbor

  • naive Bayes 

  • Decision Trees 

  • Classification Rule Learners 

  • Linear Regression 

  • Regression Trees 

  • Model Trees 

  • Neural Networks 

  • Support Vector Machines 

  • Association Rules 

  • k-means Clustering

  • A little bit overwhelming, huh? Well...not really...R and it's plethora of packages makes your life easier....after reading this book...you will be able to apply each and everything single algorithm to your real life projects...but of course...you experience, trial and error and perseverance will be highly appreciated...

    Let's see some examples...

    Cross table for a Nearest Neighbor

    Decision Tree

    Neural Network Diagram

    Association Rules

    Random Forests

    Amazing isn't it? Of course...but the fun doesn't end there...the book helps us even more with chapters on...
    • Evaluation Model Performance
    • Improving Model Performance
    • Specialized Machine Learning Topics
    If you have played with R before and always wondered what the hell Machine Learning means and why you should learn it...this book is totally for you...I recommend it 100%...

    Greetings,

    Blag.
    Developer Empowerment and Culture.