In this blog, we will demonstrate how to convert an image into a BLOB object using Python and store it in the HANA database.
BLOB is the acronym for Binary Large Object File (BLOB) which can store images/other files in the binary format up to a maximum of 2 GB. The functionality demonstrated here can also be extended to other BLOB objects such as audio, video and pdf files.
Steps
- Install Python along with the required SAP libraries (Refer to GitHub repository for more details https://github.com/SAP/PyHDB)
- Establish a connection between Python and HANA DB
- Convert object using Python
- Insert the BLOB object into a HANA table
- Consume the HANA table/view in SAP Analytics Cloud
In the example below, the SAP Analytics Cloud application has been developed to analyze the details of a customer.
Customer details, logos/images have been taken from the HANA table where they are stored as BLOBs.
Implementation details
In order to consume BLOBs in SAP Analytics Cloud (SAC), we require it to be of the format ST_MEMORY_LOB. Hence, the table also needs to be defined in the following format;
(CREATE COLUMN TABLE <SCHEMA_NAME>.<TABLENAME>(<COLUMNNAME1> <DATATYPE> PRIMARY KEY, <COLUMNNAME2> BLOB ST_MEMORY_LOB);
After creating the table, the connection from Python to HANA DB needs to be established using its corresponding host/port number and credentials. This connection can then be verified by triggering a dummy SQL query.
Now, the required image file can be converted to BLOB in Python by just reading it.
In this example, ‘Hana1.png’ is converted using a read statement. ‘binaryData’ contains the image in the encoded Binary Format.
Create a connection to HANA using ‘pyhdb.connect()’ along with the required connection details. Then we can define a cursor in HANA and leverage it for executing the necessary commands.
Cursor.execute(‘INSERT INTO <SCHEMA_NAME>.<TABLE_NAME> Values (‘Hana1.png’,binaryData)’)
‘Hana1.png’ refers to the filename and acts as the primary key while ‘binaryData’ is the corresponding BLOB. Once the execution is successful, we can commit this cursor to insert a row in our HANA table.
Now, we have successfully inserted and stored an image into the HANA table. This table can be joined with other tables/views and consumed in SAP Analytics Cloud using HANA live connectivity.
Read our other blogs on SAP HANA here.
Learn more about Visual BI Solutions SAP HANA services offerings here.
Subscribe to our Newsletter
The post Storing Images (BLOB) in SAP HANA using Python appeared first on Visual BI Solutions.