Simple Spark Application to run on EMR

An engineer excelled in developing data solutions using Big Data Technologies and Machine Learning algorithms in the Cloud infrastructure.
Search for a command to run...

An engineer excelled in developing data solutions using Big Data Technologies and Machine Learning algorithms in the Cloud infrastructure.
No comments yet. Be the first to comment.
As part of this series, I will be highlighting the Amazon Web Services (AWS) services and practices that I use in both my personal and professional capacities.
Git is a source code management system that keeps track of the changes made to their codebase and collaborates with other team members. It is commonly used for source code management in software development, but it can track changes to any set of fil...
Git is a source code management system that keeps track of the changes made to their codebase and collaborates with other team members. It is commonly used for source code management in software development, but it can track changes to any set of fil...

What It Is and Why It Matters?

It's no secret that cyber intelligence pros must have data engineering skills. But what exactly is data engineering? Data engineering is a skill that's used to collect and analyze data. Then, it can be used to understand the data's quality and make p...

Build, deploy, and manage mobile and web apps that need real-time or offline data are simple with AWS AppSync, a fully managed serverless GraphQL service. Your apps may securely access and work with data stored in AWS services like Amazon DynamoDB, A...

AWS re: Invent is a learning conference hosted for the global cloud computing community with in-person and virtual content where they announced many new features and updates. This post is the summary of the list of changes within the Data & Analytics...

In this post, we will be looking at how to submit a Spark job in AWS EMR. For this, we have an application that converts any given files to Avro or Parquet format using Apache Spark.
|-- project/
| |-- build.properties
| |-- plugins.sbt
|-- target/
|-- src/
| |-- main
| |-- test
|-- build.sbt
Based on the above structure, the project folder contains all the configurations including build properties and sbt setup. target folder contains the artifacts which include the assembly JAR and other build files. The src folder has all the source code including the business logic to convert file types and unit test suits using the ScalaTest framework.
To run any Spark application we would need to initialise the Spark session first which is present in the src/main/scala/au/fayaz/Utils.scala file :
def initiateSpark(): SparkSession = {
val spark = SparkSession.builder.master("local[*]").appName("File Converter").getOrCreate()
spark
}
The initiateSpark function will return a session to perform our DataFrame operations. In this example, we will be converting the file to Parquet or Avro based on the parameter.
def writeOutput(dataframe: DataFrame,
outputPath: String, format: String): = {
if (format == "parquet") {
dataframe
.write
.option("compression", "snappy")
.mode("overwrite")
.parquet(outputPath)
}
else
dataframe
.write
.format(
"com.databricks.spark.avro"
)
.mode("overwrite")
.save(outputPath)
}
The writeOutput function takes the parameters of dataframe, outputPath and format. Based on the format it performs a simple write operation to save the file in an appropriate format with the right compression.
To make use of this code, you can simply follow the below steps to be able to run in your EMR environment.
Once you have both, all you need to do is a simple clone of this repository and run the steps below:
Step 1: git clone https://github.com/fayaz92/file-converter-spark-scala.git.
Step 2: Open your IDE and import this project, my choice is IntelliJ in this case.
Step 3: From the Command Line, type sbt test which will perform the unit testing to ensure your code is working as expected.
Step 4: From the command line, type sbt assembly which build the artifacts as .JAR file.
Step 5: Now to run this code in your EMR, you would need to copy this .jar file to S3.
Step 6: Once you have the JAR file in S3, now you are ready to run the code. So SSH into the EMR cluster and run the below command.
spark-submit --class au.fayaz.Converter
s3://yourbucket/converter-assembly-0.1.jar
s3://yourbucket/inputFiles/sample.csv
s3://yourbucket/outputFiles/20190524/
parquet
🎉 Now you should get a converted Parquet file in the output location !!
Thank you for reading and click here to download this application.