SapHanaTutorial.Com HOME     Learning-Materials Interview-Q&A Certifications Quiz Online-Courses Forum Jobs Trendz FAQs  
     Explore The World of Hana With Us     
About Us
Contact Us
 Apps
X
HANA App
>>>
Hadoop App
>>>
Tutorial App on SAP HANA
This app is an All-In-One package to provide everything to HANA Lovers.

It contains
1. Courses on SAP HANA - Basics, Modeling and Administration
2. Multiple Quizzes on Overview, Modelling, Architeture, and Administration
3. Most popular articles on SAP HANA
4. Series of Interview questions to brushup your HANA skills
Tutorial App on Hadoop
This app is an All-In-One package to provide everything to Hadoop Lovers.

It contains
1. Courses on Hadoop - Basics and Advanced
2. Multiple Quizzes on Basics, MapReduce and HDFS
3. Most popular articles on Hadoop
4. Series of Interview questions to brushup your skills
Apps
HANA App
Hadoop App
';
Search
Stay Connected
Search Topics
Topic Index
+
-
SAP HANA Overview
+
-
SAP HANA Architecture
+
-
SAP HANA Studio
+
-
Reporting in HANA
+
-
SAP HANA Text analysis
+
-
SAP BW on HANA
+
-
Miscellaneous
Image Processing in SAP HANA


In this article we will learn:
  • How image is saved in HANA database in BLOB format
  • How to upload image in SAP HANA
  • How to download image from HANA and show in HTML

What is a BLOB?

BLOB (Binary Large Object) is a field type for storing Binary Data.
BLOB could store a large chunk of data, document types and even media files like audio or video files. In HANA the BLOB size can be up to 2 GB.

In HANA database, we can use BLOB data type to store image.

Create Image Table:

Let’s first create a table in HANA to store images.
Copy and paste below script to create table.
--REPLACE <SCHEMA_NAME> WITH YOUR SCHEMA NAME
CREATE COLUMN TABLE "<SCHEMA_NAME>"."IMAGE_STORE"(
    IMAGE_NAME NVARCHAR(20) PRIMARY KEY,
    IMAGE_CONTENT BLOB
);

Download some sample images:

Download some sample images and save in a folder.
For this example I downloaded some images of SAP HANA and saved in “C:\ImageToBeUploaded”

SAP HANA Image Upload using BLOB

Upload images into HANA database:

There are several ways of uploading images into HANA database. For example: Java, Python etc.
Here we will use Java program to upload image.
Note: You can either use a different eclipse tool for java or use the same HANA Studio to create a Java project. If you are using HANA Studio, then make sure you have selected “SAP HANA Development” perspective.

  1. Download the JAVA Project ImageUploader.zip and save it to your local computer.
  2. Open JAVA Eclipse (or HANA Studio) and create a Java project called "ImageUploader".

SAP HANA Image Upload using BLOB


SAP HANA Image Upload using BLOB

  1. Go to File --> Import --> General and select "Archive File".
  2. Click on browse and select the " ImageUploader.zip " file you downloaded in step 1. Click on finish.

SAP HANA Image Upload using BLOB

  1. Open ImageUploader.java file and change the HANA system details.

SAP HANA Image Upload using BLOB

  1. Run the Java program. You should see similar message in console.

SAP HANA Image Upload using BLOB

We have successfully uploaded the image files into HANA database.

Have the images really uploaded to HANA?

You might be wondering that have the files really uploaded to HANA. Let us check that.
Open HANA studio and open SQL console. Check the output of IMAGE_STORE table.

SELECT * FROM "<SCHEMA_NAME>"."IMAGE_STORE";

SAP HANA Image Upload using BLOB

The output shows that image files are uploaded in BLOB format.

Download image from SAP HANA:

We need to create a HANA XS project to download the image from HANA.
  1. Create a HANA XS project.

SAP HANA Image Upload using BLOB

  1. Download the zip file and extract it somewhere in your local system.
  2. Copy all the content inside “ImageProcessing” folder.

SAP HANA Image Upload using BLOB

  1. Go to HANA Studio and paste everything inside

SAP HANA Image Upload using BLOB
  1. Open GetImage.xsjs file and change the YOUR_SCHEMA_NAME in first line to your schema name. Make sure it is the same schema where you created the “IMAGE_STORE” table.
  2. Right click on the project and select Team --> Activate.

Done!!
Now let us test this.
Open ImageBrowser.html file under “ui” folder. Right click and select Run As --> HTML
Search for the image that you uploaded. If the image is found it will display the image.

SAP HANA Image Upload using BLOB

You can search with the name of the images you uploaded.

If you have any question or doubt, please leave a comment or contact us.






Support us by sharing this article.



Explore More
Close X
Close X

25 thoughts on “Image Processing in SAP HANA

  1. Arun says:

    Hi,

    I see your image upload and twitter feed tutorial. One question i have is how you are getting the imageuploader.java? Are you creating it or is it available some where with in SAP?

    Thanks,
    Arun

    • Admin says:

      Hi Arun,
      ImageUploader.java is not a SAP provided file. We have created it using the JDBC connection with HANA.

  2. Rachel says:

    How to run this java program? run as web app or run as applet. it gives following error Exception in thread “main” java.lang.Error: Unresolved compilation problem: at ImageUploader.src.ImageUploader.main(ImageUploader.java:25)

    • Admin says:

      Hi Rachel,
      Its same as any other java program. Right click and Run As Java. Have you followed all the steps mentioned in the para “Uploaded images into HANA databse” ?
      Can you please paste the complete error message.

      • Deepak says:

        Hi

        This one really informative, thanks for putting up this blog.

        Iam facing a problem in viewing the images uploaded. Its not showing the error “image not found” but the required image is not opening up.Just after the click of the get image button you have nothing to display.

        Regards
        Deepak

        • Admin says:

          Hi Deepak,
          Could you please crosscheck following things:
          – Is the schema name and table name correct in XSJS service?
          – Is the image name you are searching is same as image uploaded? Note that the file name and file extension both are case sensitive.
          – Check in the HANA table, if the image is uploaded or not.

          Let me know if you find anything in above mentioned points.

          Regards,
          Admin

  3. Upendra Gupta says:

    Please provide some suggestion for resolving the following error :

    Exception in thread “main” java.lang.ClassNotFoundException: com.sap.db.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:435)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:424)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:423)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:493)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:351)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:426)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:259)

  4. Anu says:

    How can we get an image from UI through xsjs????Can any one help???

  5. swaroop says:

    Exception in thread “main” java.lang.Error: Unresolved compilation problem:

    at ImageUploader.src.ImageUploader.main(ImageUploader.java:24)..i m getting tis type of error plz resolve

  6. Rak Sah says:

    Hi, Is there a way to upload an image in hana xs table with sap ui5 frontend (not with java ), how can i do this ? is there already an implementation ? i would be glad for the positive response. regards rakesh

  7. Godwin says:

    Hi, will the same process work if I wanted to upload .wav files or(MP3,MP4,AAC,WAV,3GPP,SMF,OGG.) into Hana.
    Will I be able to store 50GBs worth of audio Data in Hana?

  8. Razwan says:

    Hi, is there a way to upload M3 files into HANA?

  9. Makoto says:

    There’s a risk of SQL injection in the XSJS code.

    It’d be better to use place folder in the SQL statement.

    var query = “Select IMAGE_CONTENT From ” + schema_name + “.IMAGE_STORE Where IMAGE_NAME = ?”;
    var pstmt = conn.prepareStatement(query);
    pstmt.setString( 1, id ) ;

    Regards,
    Makoto

  10. gayatri says:

    Please provide some suggestion for resolving the following error

    Exception in thread “main” com.sap.db.jdbc.exceptions.JDBCDriverException: SAP DBTech JDBC: [258]: insufficient privilege: Not authorized
    at com.sap.db.jdbc.exceptions.SQLExceptionSapDB.createException(SQLExceptionSapDB.java:345)
    at com.sap.db.jdbc.exceptions.SQLExceptionSapDB.generateDatabaseException(SQLExceptionSapDB.java:185)
    at com.sap.db.jdbc.packet.ReplyPacket.buildExceptionChain(ReplyPacket.java:100)
    at com.sap.db.jdbc.ConnectionSapDB.execute(ConnectionSapDB.java:1130)
    at com.sap.db.jdbc.CallableStatementSapDB.sendCommand(CallableStatementSapDB.java:1964)
    at com.sap.db.jdbc.StatementSapDB.sendSQL(StatementSapDB.java:978)
    at com.sap.db.jdbc.CallableStatementSapDB.doParse(CallableStatementSapDB.java:256)
    at com.sap.db.jdbc.CallableStatementSapDB.constructor(CallableStatementSapDB.java:215)
    at com.sap.db.jdbc.CallableStatementSapDB.(CallableStatementSapDB.java:125)
    at com.sap.db.jdbc.CallableStatementSapDBFinalize.(CallableStatementSapDBFinalize.java:31)
    at com.sap.db.jdbc.ConnectionSapDB.prepareStatement(ConnectionSapDB.java:1358)
    at com.sap.db.jdbc.trace.Connection.prepareStatement(Connection.java:413)
    at hellowordjava.helloworld.main(helloworld.java:30)

  11. Mark says:

    Hi,

    I keep getting a launch error (Selection does not contain a main type) after running the java program. Please help. Thanks!

  12. Mark says:

    I just realized that as soon as I import the archive file, it gives an error saying that:

    The declared package “” does not match the expected package “ImageUploader.src” ImageUploader.java /ImageUploader/src/ImageUploader/src line 1 Java Problem

    • LucaTo says:

      Hi, the problem you reported is due to the way you have imported the project. Make sure that the src contains the ImageUploader.java in the default package, and then that the .jar library is added to the path. Any java programmer with Eclipse experience can easily help on this setup. Good luck.

  13. Joao ivan says:

    Can you tell me how can i upload an image to my file system hana xs ?

  14. Jay Malla says:

    Thanks for this very useful post! I was able to get this working! It took a little while to get the HTTP tunnel working to my trial HANA cloud account. Maybe you can add an article on how to get that working?

    Great job!

  15. Shekar says:

    Hi Admin,
    In the similar way am trying to upload and download excel or word document to hana.
    Now am able to upload it successfully.
    Could you please help me how can i download the uploaded attachment.
    Below is my code for downloading the attachment(Excel file).

    var schema_name = “FACT”;
    //var id = $.request.parameters.get(‘id’);
    var conn = $.db.getConnection();

    try {
    var query = “Select IMAGE_CONTENT From \”FACT\”.\”IMAGE_STORE\””;

    var pstmt = conn.prepareStatement(query);
    var rs = pstmt.executeQuery();
    if(rs.next()){
    // $.response.headers.set(“Content-Disposition”, “Content-Disposition: attachment; filename=filename.jpg”);
    // $.response.headers.set(“Content-Disposition”, “Content-Disposition: attachment; filename=filename.txt”);

    //$.response.contentType = “text/html”;
    $.response.setBody(rs.getBlob(1));
    $.response.contentType = “application/vnd.xls”;
    $.response.status = $.net.http.OK;
    }
    else{
    $.response.contentType = ‘text/html’;
    $.response.setBody(“Image not found”);
    $.response.status = 404;
    }

    } catch (e) {
    $.response.setBody(“Error while downloading : ” + e);
    $.response.status = 500;
    }
    conn.close();

    • Admin says:

      Hi Shekar,
      You can upload the file to a table column of type NCLOB.
      To download the content, you may use code similar to below.

      var conn = $.db.getConnection();
      var fileName = “filename.doc”;
      try {
      var query = “Select COLUMN1_OF_TYPE_NCLOB From ” + schema_name + “.TABLE_NAME Where FILE_ID = fileName;

      var pstmt = conn.prepareStatement(query);
      var rs = pstmt.executeQuery();
      if(rs.next()){
      $.response.headers.set(“content-disposition”, “attachment; filename=” + fileName);
      $.response.contentType = ‘image/jpg’;
      $.response.setBody(rs.getBlob(1));
      $.response.status = $.net.http.OK;
      }
      else{
      $.response.contentType = ‘text/html’;
      $.response.setBody(“Image not found”);
      $.response.status = 404;
      }

      } catch (e) {
      $.response.setBody(“Error while downloading : ” + e);
      $.response.status = 500;
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

Current day month ye@r *

 © 2017 : saphanatutorial.com, All rights reserved.  Privacy Policy