How to insert images into a MySQL table with BLOB type

author
Shicong Hong
EnrichVersion
6.4
6.3
6.2
6.1
6.0
EnrichProdName
Talend Big Data Platform
Talend Big Data
Talend Data Integration
Talend Data Services Platform
Talend ESB
Talend Open Studio for Data Integration
Talend Data Fabric
Talend Open Studio for ESB
Talend Open Studio for MDM
Talend Real-Time Big Data Platform
Talend MDM Platform
Talend Data Management Platform
Talend Open Studio for Big Data
task
Design and Development
Data Governance > Third-party systems > Database components > MySQL components
Data Quality and Preparation > Third-party systems > Database components > MySQL components
Design and Development > Third-party systems > Database components > MySQL components
EnrichPlatform
Talend Studio

How to insert images into a MySQL table with BLOB type

This article explains how to insert images into a MySQL table with BLOB type.

Some projects require a large string or block of binary data to be stored in a database. A BLOB (Binary Large Object) is a data type that can be used to store a large collection of binary data in a database table. A CLOB (Character Large Object) is a data type that can be used to store a large collection of character data in a database table.

For example, a digital file containing a picture, video, or a song can be stored in a database using a BLOB, or a plain text file can be stored in a database using a CLOB. This article explains how to insert images into a Mysql table with BLOB type.

The example can be adapted, with minor changes, to use the CLOB type or another database.

Prerequisites

  • This example uses a MySQL table, named blobdemo, with two fields: Id and Picture. The table definition is as follows:
  • A specific folder, in this example "C:/Talend/images", stores the pictures that will be inserted into the blobdemo table.

Creating a user routine

Procedure

  1. In the Repository tree view, expand Code to display the Routines folder.
  2. Right-click Routines and select Create routine.
  3. The [New routine] dialog box opens. Enter its name, in this example MyRoutineDemo, a purpose and a description.
  4. Click Finish to close the dialog box.
    Note: The newly created routine appears in the Repository tree view, directly below the Routines node.

    The routine editor opens to reveal a model routine which contains a simple example, by default, comprising descriptive text in blue, followed by the corresponding code.

  5. Replace the model with the following code and press Ctrl+S to save the routine.

    It has a ByteArrayFromFile function, that requires a file path as input parameter and is used to read a file and convert it to a byte array.

    package routines;public class MyRoutineDemo {
    public static byte[] ByteArrayFromFile(String filepath) {
    try{ 
    java.io.File file=new java.io.File(filepath);
    java.io.FileInputStream fis = new java.io.FileInputStream(file);
    int fileLength = (int) file.length();
    byte[] incoming_file_data = new byte[fileLength]; // allocate byte array of right size
    fis.read(incoming_file_data, 0, fileLength ); // read into byte array
    fis.close();
    return incoming_file_data;
    }catch(Exception err){
    err.printStackTrace();return null;
    }
    }
    }

    For more information on how to create user routines, see the Talend User Guide on Talend Help Center.

Creating a Job to insert BLOB data into a MySQL table

Create a Job to iterate over the picture files in the "C:/Talend/images" folder and insert each one into the blobdemo table.
  • The tFileList iterates all pictures in the "C:/Talend/images" folder.
  • The tFixedFlowInput gets the current file path and outputs it.
  • The tMap calls the routine to read the files as byte array.
  • The tMysqlOutput inserts the BLOB data into the target table.

Set up the Job

Procedure

  1. Create a Standard Job and enter its name, in this example Insertblobdatatodatabase.
  2. Drag and drop the following components to the design workspace: tFileList, tFixedFlowInput, tMap and tMysqlOutput.
  3. Connect the tFileList and the tFixedFlowInput using an Iterate link.
  4. Connect the tFixedFlowInput to the tMap and the tMap to the tMysqlOutput using Row > Main links.

Configure the data input

Procedure

  1. In the Basic settings view of the tFileList, in the Directory field, click the [...] button to browse to the directory, in this example "C:/Talend/images".
  2. In the FileList Type list, select Files.
  3. Click the [+] button to add a row in the Files area and define a filter to match files. In this example, type in "*.png" to retrieve all picture files.
  4. In the Basic settings view of the tFixedFlowInput, click the Edit Schema button and add a column, filepath of type String.

    The defined column displays in the Values panel.

  5. Click in the Value cell of the filepath column and press Ctrl+Space to access the global variable list.
  6. From the global variable list, select tFileList_1_CURRENT_FILEPATH.

Configure the tMap component to call the user routine

Before you begin

  • You have created a user routine.

Procedure

  1. Double-click the tMap to open the map editor.
  2. Click the [+] button to add an output table named out1.
  3. At the bottom right corner (out1) of the Schema editor view, click the [+] button to add a row, picture of type byte.
  4. In the Expression field of the picture column on the output side, type in MyRoutineDemo.ByteArrayFromFile(row1.filepath).
  5. Click OK to validate these changes and accept the propagation prompted by the dialog box that pops up.

Insert the BLOB data into the target table

Before you begin

  • You have created a MySQL table.

Procedure

  1. In the design workspace, double-click the tMysqlOutput component to display its Basic settings view and set its parameters.
  2. Set Property Type to Repository and then click the three-dot button to select the correct DB connection.

    The connection details display automatically in the corresponding fields.

  3. Click the [...] button next to the Table field and select the target table, blobdemo in this example.

Executing the Job

Procedure

  1. Press Ctrl+S to save your Job.
  2. Press F6 to execute the Job.
  3. Query the blobdemo table to verify that the pictures were inserted successfully.