Thursday 24 August 2017

Using Predictive Analytics and Python on SAP Cloud Platform HANA database - Part 2

This blog is a continuation of the previous blog which focused on connecting an on-premise redictive Analytics software with HANA database on the Cloud Platform. Python is sometime used to implement data science in conjunction with Predictive analytics.

In this blog, I will show you how to connect a Python program to a HANA database available on SAP Cloud Platform. The process to connect to the database is slightly different it is on SAP Cloud Platform.

I am going to use the same “hcpta” HANA database used in the previous blog.

SAP Cloud Platform, SAP HANA Live, SAP HANA Tutorial, SAP HANA Certifications, SAP Certifications, SAP All Modules List

I have setup a Cloud Connector which setup a connection to the account in Cloud Platform. I have also registered a service channel for hcpta database on port 39815

SAP Cloud Platform, SAP HANA Live, SAP HANA Tutorial, SAP HANA Certifications, SAP Certifications, SAP All Modules List

I have installed Python on my laptop .

SAP Cloud Platform, SAP HANA Live, SAP HANA Tutorial, SAP HANA Certifications, SAP Certifications, SAP All Modules List

To connect to HANA database, I am using a python client which is available in github.

The installation steps are documented and you can follow them to prepare python.

SAP Cloud Platform, SAP HANA Live, SAP HANA Tutorial, SAP HANA Certifications, SAP Certifications, SAP All Modules List

The github has got sample code which explains how to use each of the methods.

Below is a sample program which creates a table and inserts a record. I have used localhost as the Cloud Connector is also installed on the same laptop.

import pyhdb

connection = pyhdb.connect(
    host="localhost",
    port=39815,
    user="<HANA_DB_USER>",
    password="<HANA_DB_PASSWORD>"
)
cursor = connection.cursor()
cursor.execute('CREATE TABLE PYHDB_TABLE("NAMES" VARCHAR (255) null)')
print ("Table PYHDB_TAB created ")
cursor.execute("INSERT INTO PYHDB_TABLE VALUES('Hello Python')")
print ("Record Inserted into PYHDB_TABLE")
connection.commit()
connection.close()

After executing this script, I could see the table created in my HANA database in the Cloud Platform.

Below is the new table which is created in my schema along with the record.

SAP Cloud Platform, SAP HANA Live, SAP HANA Tutorial, SAP HANA Certifications, SAP Certifications, SAP All Modules List

This shows how you can connect Python to a HANA database on Cloud Platform and perform DDL/DML operations.
SAP Online Guides, Tutorials, Materials and Certifications.

Related Posts

0 comments:

Post a Comment