Skip to Content

I don’t claim to be a data scientist, but I have setup, configured and exploited SAP HANA, Predictive Analytics and a number of other tools before.  R is another great tool to have in your kitbag and it can easily be connected to HANA by either JDBC or ODBC.

What’s Needed

  • R Desktop
  • R Studio
  • HANA JDBC Driver
  • R Code to open the connection

Download & Install R Desktop
https://cran.rstudio.com

Download & Install R Studio
https://www.rstudio.com/products/rstudio/download3/

Download & Install the HANA JDBC Driver
The only file you need is ngdbc.jar, this is can be found as part of the SAP HANA Client

SAP ONE Support Launchpad – Software Download – HANA Client

Or if you’re on a Mac there isn’t a HANA Client but you can either copy the ngdbc.jar from another platform or alternatively locate the jdbc driver within the HANA Studio Installation.
On my install one is located here
/Applications/SAP Clients.app/Contents/Eclipse/plugins/com.sap.ndb.studio.jdbc_2.2.8.jar

Official documentation for the JDBC driver can be found here
Connect to SAP HANA via JDBC

Pasted below is a code snippet to establish the connection in R Studio.

# Things you may need to change:
# - classPath (This needs to Point to the HANA JDBC Driver)
# - jdbcDriver (This slightly different depending which JDBC driver you have)
# - SAP HANA Host & port name after jdbc:sap://
# - username (HANA DB User)
# - password (HANA DB User Password)
# - dbGetQuery (Change the Select Query, here I selected a 10% Sample of the CENSUS table in my own schema)

if (!require("RJDBC")) {
  install.packages("RJDBC",repos="http://cran.rstudio.com/")
  library("RJDBC")
}

# classPath="/Users/i049374/Documents/HANA/Install/R/ngdbc.jar")
# classPath="/Applications/SAP Clients.app/Contents/Eclipse/plugins/com.sap.ndb.studio.jdbc_2.2.8.jar")
# For ngdbc.jar use        # jdbcDriver <- JDBC(driverClass="com.sap.db.jdbc.Driver", 
# For HANA Studio jar use  # jdbcDriver <- JDBC(driverClass="com.sap.ndb.studio.jdbc.JDBCConnection",
                                    
jdbcDriver <- JDBC(driverClass="com.sap.db.jdbc.Driver",  
                  classPath="/Users/i049374/Documents/HANA/Install/R/ngdbc.jar")
jdbcConnection <- dbConnect(jdbcDriver,
                  "jdbc:sap://ukhana.mo.sap.corp:30015/?autocommit=false"
                  ,"username"
                  ,"password")
result <- dbGetQuery(jdbcConnection, "select * from CENSUS TABLESAMPLE SYSTEM (10)")
print(result)
dbDisconnect(jdbcConnection)

Below is the code in my R Studio environment

Below is the output of the SQL query inside R from SAP HANA.

To report this post you need to login first.

5 Comments

You must be Logged on to comment or reply to a post.

  1. Al Kafi Khan

    Hi

    I am getting an error during connection. I used following code :

    jdbcDriver <- JDBC(driverClass=”com.sap.db.jdbc.Driver”,
    classPath=”C:/ngdbc.jar”)

    I am getting following error. Please take a look & give me some clue.

    Error in .jfindClass(as.character(driverClass)[1]) : class not found

    Thanks Advance

    (1) 
  2. Ian Henry Post author

    It could be a permission issue. Often C:\ is a protected location.
    I would try putting ngdbc.jar is a more standard directory, My Documents\Something, and reference that path.

     

    (0) 
      1. Lars Breddemann

        Have you configured the database tunnel for what seems to be a HANA instance in your SAP cloud trial account? If not, then you won’t be able to connect to it via JDBC. Check the documentation for details on that.

        And before you ask: HANA studio does that automagically under the covers.

        Also: username and password have NO PLACE in your application/R code. Use secure storage for that. Whenever you have to manually hide these details in a screenshot – that’s a security bug in your code!

        (0) 

Leave a Reply