How to process Instagram images with Spark 1.1.0 and OpenCV (with Java)

by Ruben Tous

Installation: You will need

Installation: You will need

Preparing the data

We will have thousands of .jpg files, along with their corresponding .txt files (with json metadata). On the one hand, it is not convenient to store all the files within the same directory. On the other hand, it is not convenient to access the files one by one from Spark. So, we will need to perform some previous steps:

Building the code

Create the following directory structure (let's call the root YOUR_APP_PATH):

Edit the pom.xml file:

This pom.xml uses the maven-assembly-plugin to pack all the dependent jars (except for the Spark-related jars) within a single .jar. This will make easier the submission of the application to Spark.

Now edit Main.java. This class 1) reads the images from the SequenceFile into a key-value RDD (key will be the filename) and uses OpenCV to determine the width of the images, 2) reads the metadata from the SequenceFile into another key-value RDD, 3) joins both RDD into a single RDD where the value is a tuple (image width, username)

Before building it is necessary to import the OpenCV .jar (opencv-249.jar or opencv-300.jar) into the local Maven respository. Type the following:

In order to build, simply type:

Execute on Spark 1.1.0

Type the following from

The previous command tells Spark where to find the native OpenCV library. However, if you copy the library in /usr/lib/ it would work without the --driver-library-path directive.