SlideShare a Scribd company logo
Data processing with Spark
in R & Python
Maloy Manna
linkedin.com/in/maloy @itsmaloy biguru.wordpress.com
Abstract
With ever increasing adoption by vendors and enterprises, Spark is fast
becoming the de facto big data platform.
As a general purpose data processing engine, Spark can be used in both R and
Python programs.
In this webinar, we’ll see how to use Spark to process data from various
sources in R and Python and how new tools like Spark SQL and data frames
make it easy to perform structured data processing.
Speaker profile
Maloy Manna
Data science engineering
AXA Data Innovation Lab
• Building data driven products and services for over 15 years
• Worked in Thomson Reuters, Infosys, TCS and data science startup Saama
linkedin.com/in/maloy @itsmaloy biguru.wordpress.com
Agenda
• Overview of Spark
• Data processing operations
• RDD operations
– Transformations, Actions
• Spark SQL
– DataFrames
– DataFrame operations
• Spark R
• Useful Tips
• References
Overview of Spark
• Fast, general-purpose engine for large-scale data
processing
• Smarter than Hadoop in utilizing memory
• Faster than MapReduce in memory & on disk
• Can run on Hadoop, or standalone; can access data in
HDFS, Cassandra, Hive / any Hadoop data source
• Provides high-level APIs in Scala, Java, Python & R
• Supports high-level tools like Spark SQL for structured
data processing
Using Spark for data science & big data
• Data science lifecycle
• 50% – 80% of time spent in data preparation stage
• Automation is key to efficiency
• R & Python already have packages & libraries for data processing
• Apache Spark adds more power to R & Python big data wrangling
Data processing
Getting data to the right format for analysis:
• Data manipulations
• Data tidying
• Data visualization
reshaping formatting
cleaning Transformations
munging Wrangling carpentry
manipulation cleaning
processing
Data processing - operations
• Reshaping data
Change layout (rows/columns “shape”) of dataset
• Subset data
Select rows or columns
• Group data
Group data by categories, summarize values
• Make new variables
Compute and append new columns, drop old columns
• Combine data sets
Joins, append rows/columns, set operations
• Driver program runs main function
• RDD (resilient distributed datasets) and shared
variables help in parallel execution
• Cluster manager distributes code and manages data in
RDDs
Spark for data processing
Installing and using Spark
• Install pre-compiled package
http://spark.apache.org/downloads.html
• Build from source code
http://spark.apache.org/docs/latest/building-spark.html
• Run Spark on Amazon EC2 or use Databricks Spark notebooks (Python / R)
http://spark.apache.org/docs/latest/ec2-scripts.html | www.databricks.com/registration
• Run as Docker image
https://hub.docker.com/r/sequenceiq/spark/
• Download pre-compiled release version
• Choose “pre-built for Hadoop 2.6 and later”
• Unpack/untar package
• Try out the Python interactive shell
bin/pyspark
• Ensure JAVA_HOME is set
bin/sparkR
Installing Spark
Using Spark in Python
• Import Spark classes
• Create SparkContext object (driver program) and
initialize it
• In practice, use the spark-submit script to launch
applications on a cluster, using configurable
options and including dependencies
• Once a SparkContext is available, it can be used
to build RDDs.
RDD: Transformations & Actions
• RDD is immutable, distributed data structure
– Each RDD is split into multiple partitions
• Can be created in 2 ways:
– Loading external dataset or
– Distributing a collection of objects in driver
• RDDs support 2 different types of operations:
– Transformations (construct new RDD)
– Actions (compute result based on RDD)
RDD: Transformations & Actions
Transformations
 No (lazy) evaluations
 New RDD returned
 Examples:
⁻ map
⁻ filter
⁻ flatMap
⁻ groupByKey
⁻ reduceByKey
⁻ aggregateByKey
⁻ union
⁻ join
⁻ coalesce
Actions
 Evaluations done
 New value returned
 Examples:
⁻ reduce
⁻ collect
⁻ count
⁻ first
⁻ take
⁻ countByKey
⁻ foreach
⁻ saveAsTextFile
⁻ saveAsSequenceFile
Create RDDs
• Creating distributed datasets
– From any storage source supported by Hadoop
• Use SparkContext methods:
– Support directories, compressed files, wildcards
Loading data
• Loading text files
• Loading unstructured JSON files
• Loading sequence files
Loading data
• Loading csv files
• Loading csv files in full
Saving data
• Saving text files
• Saving unstructured JSON files
• Saving csv files
Spark SQL
• Spark’s interface for working with structured
and semi-structured data
• Can load data from JSON, Hive, Parquet
• Can query using SQL
• Can be combined with regular code e.g.
Python / Java inside Spark application
• Provides “DataFrames” (SchemaRDD < v1.3)
• Like RDDs, DataFrames are evaluated “lazily”
Using Spark SQL
• HiveContext (or SQLContext for a stripped-
down version) based on SparkContext
• Construct a SQLContext:
• Basic query:
Spark SQL: DataFrames
• Spark SQL provides DataFrames as programming
abstractions
• A DataFrame is a distributed collection of data
organized into named columns
• Conceptually equivalent to relational table
• Familiar syntax (R dplyr / Pandas) but scales to PBs
• Entry-point remains SQLContext
Spark SQL: DataFrame Operations
• Selecting rows, columns
• Grouping / aggregation
• Running SQL queries
• Window functions
• Reading JSON data into dataframe in Python
• Reading JSON data into dataframe in R
DataFrames – Data Operations
• Generic load/save
– Python
– R
• Default data source parquet
– Can be changed by manually specifying format
DataFrames – Saving data
SparkR
• R package providing light-weight front-end to
use Apache Spark from R
• Entry point in SparkContext
• With SQLContext, dataframes can be created
from local R data frames, Hive tables or other
Spark data sources
• Introduced with Spark 1.4
SparkR: Creating DataFrames
• From local data frames
• From data sources like JSON
• From Hive tables
Useful tips
• Use Spark SQL dataframes to write less code.
Easier to avoid closure problems.
• Be aware of closure issues while working in
cluster mode. Use accumulator variables instead
of locally defined methods
• Utilize Spark SQL capability to automatically infer
schema of JSON datasets
SQLContext.read.json
• Other than using command-line, IDEs like IntelliJ
IDEA community edition can be used for free
References
• Spark pages: http://spark.apache.org/
• Databricks blog: https://databricks.com/blog
• Spark summit: https://spark-summit.org/
• Additional Spark packages at: http://spark-packages.org/
• Example scripts:
• https://github.com/apache/spark/blob/master/examples/src/main/pytho
n/sql.py
• https://github.com/apache/spark/blob/master/examples/src/main/r/data
-manipulation.R
• https://github.com/apache/spark/blob/master/examples/src/main/r/data
frame.R
• API docs: http://spark.apache.org/docs/latest/api/python/index.html
• http://spark.apache.org/docs/latest/api/R/index.html
• Using SparkR in Rstudio: http://www.r-bloggers.com/how-to-use-sparkr-
within-rstudio/
Ad

Recommended

Apache Spark Fundamentals
Apache Spark Fundamentals
Zahra Eskandari
 
Big data overview
Big data overview
beCloudReady
 
SF Big Analytics 20190612: Building highly efficient data lakes using Apache ...
SF Big Analytics 20190612: Building highly efficient data lakes using Apache ...
Chester Chen
 
Spark sql
Spark sql
Zahra Eskandari
 
Reshape Data Lake (as of 2020.07)
Reshape Data Lake (as of 2020.07)
Eric Sun
 
Spark ai summit_oct_17_2019_kimhammar_jimdowling_v6
Spark ai summit_oct_17_2019_kimhammar_jimdowling_v6
Kim Hammar
 
SPARK ARCHITECTURE
SPARK ARCHITECTURE
GauravBiswas9
 
Optimizing Delta/Parquet Data Lakes for Apache Spark
Optimizing Delta/Parquet Data Lakes for Apache Spark
Databricks
 
Hoodie: How (And Why) We built an analytical datastore on Spark
Hoodie: How (And Why) We built an analytical datastore on Spark
Vinoth Chandar
 
Apache Spark Streaming
Apache Spark Streaming
Zahra Eskandari
 
A Thorough Comparison of Delta Lake, Iceberg and Hudi
A Thorough Comparison of Delta Lake, Iceberg and Hudi
Databricks
 
Managing ADLS gen2 using Apache Spark
Managing ADLS gen2 using Apache Spark
Databricks
 
Apache Spark 101
Apache Spark 101
Abdullah Çetin ÇAVDAR
 
Spark SQL
Spark SQL
Joud Khattab
 
Spark SQL Beyond Official Documentation
Spark SQL Beyond Official Documentation
Databricks
 
Hoodie - DataEngConf 2017
Hoodie - DataEngConf 2017
Vinoth Chandar
 
Hoodie: Incremental processing on hadoop
Hoodie: Incremental processing on hadoop
Prasanna Rajaperumal
 
Apache spark - Architecture , Overview & libraries
Apache spark - Architecture , Overview & libraries
Walaa Hamdy Assy
 
Apache Spark PDF
Apache Spark PDF
Naresh Rupareliya
 
HKOSCon18 - Chetan Khatri - Scaling TB's of Data with Apache Spark and Scala ...
HKOSCon18 - Chetan Khatri - Scaling TB's of Data with Apache Spark and Scala ...
Chetan Khatri
 
Apache Spark Based Reliable Data Ingestion in Datalake with Gagan Agrawal
Apache Spark Based Reliable Data Ingestion in Datalake with Gagan Agrawal
Databricks
 
Introduction to Apache Spark
Introduction to Apache Spark
datamantra
 
Spark Core
Spark Core
Todd McGrath
 
Introduction to apache spark
Introduction to apache spark
Muktadiur Rahman
 
Meetup Oracle Database BCN: 2.1 Data Management Trends
Meetup Oracle Database BCN: 2.1 Data Management Trends
avanttic Consultoría Tecnológica
 
Introduction to Apache Spark
Introduction to Apache Spark
Juan Pedro Moreno
 
Lighting up Big Data Analytics with Apache Spark in Azure
Lighting up Big Data Analytics with Apache Spark in Azure
Jen Stirrup
 
Introduction to SparkR
Introduction to SparkR
Olgun Aydın
 
Apache Spark on HDinsight Training
Apache Spark on HDinsight Training
Synergetics Learning and Cloud Consulting
 
Big_data_analytics_NoSql_Module-4_Session
Big_data_analytics_NoSql_Module-4_Session
RUHULAMINHAZARIKA
 

More Related Content

What's hot (20)

Hoodie: How (And Why) We built an analytical datastore on Spark
Hoodie: How (And Why) We built an analytical datastore on Spark
Vinoth Chandar
 
Apache Spark Streaming
Apache Spark Streaming
Zahra Eskandari
 
A Thorough Comparison of Delta Lake, Iceberg and Hudi
A Thorough Comparison of Delta Lake, Iceberg and Hudi
Databricks
 
Managing ADLS gen2 using Apache Spark
Managing ADLS gen2 using Apache Spark
Databricks
 
Apache Spark 101
Apache Spark 101
Abdullah Çetin ÇAVDAR
 
Spark SQL
Spark SQL
Joud Khattab
 
Spark SQL Beyond Official Documentation
Spark SQL Beyond Official Documentation
Databricks
 
Hoodie - DataEngConf 2017
Hoodie - DataEngConf 2017
Vinoth Chandar
 
Hoodie: Incremental processing on hadoop
Hoodie: Incremental processing on hadoop
Prasanna Rajaperumal
 
Apache spark - Architecture , Overview & libraries
Apache spark - Architecture , Overview & libraries
Walaa Hamdy Assy
 
Apache Spark PDF
Apache Spark PDF
Naresh Rupareliya
 
HKOSCon18 - Chetan Khatri - Scaling TB's of Data with Apache Spark and Scala ...
HKOSCon18 - Chetan Khatri - Scaling TB's of Data with Apache Spark and Scala ...
Chetan Khatri
 
Apache Spark Based Reliable Data Ingestion in Datalake with Gagan Agrawal
Apache Spark Based Reliable Data Ingestion in Datalake with Gagan Agrawal
Databricks
 
Introduction to Apache Spark
Introduction to Apache Spark
datamantra
 
Spark Core
Spark Core
Todd McGrath
 
Introduction to apache spark
Introduction to apache spark
Muktadiur Rahman
 
Meetup Oracle Database BCN: 2.1 Data Management Trends
Meetup Oracle Database BCN: 2.1 Data Management Trends
avanttic Consultoría Tecnológica
 
Introduction to Apache Spark
Introduction to Apache Spark
Juan Pedro Moreno
 
Lighting up Big Data Analytics with Apache Spark in Azure
Lighting up Big Data Analytics with Apache Spark in Azure
Jen Stirrup
 
Introduction to SparkR
Introduction to SparkR
Olgun Aydın
 
Hoodie: How (And Why) We built an analytical datastore on Spark
Hoodie: How (And Why) We built an analytical datastore on Spark
Vinoth Chandar
 
A Thorough Comparison of Delta Lake, Iceberg and Hudi
A Thorough Comparison of Delta Lake, Iceberg and Hudi
Databricks
 
Managing ADLS gen2 using Apache Spark
Managing ADLS gen2 using Apache Spark
Databricks
 
Spark SQL Beyond Official Documentation
Spark SQL Beyond Official Documentation
Databricks
 
Hoodie - DataEngConf 2017
Hoodie - DataEngConf 2017
Vinoth Chandar
 
Hoodie: Incremental processing on hadoop
Hoodie: Incremental processing on hadoop
Prasanna Rajaperumal
 
Apache spark - Architecture , Overview & libraries
Apache spark - Architecture , Overview & libraries
Walaa Hamdy Assy
 
HKOSCon18 - Chetan Khatri - Scaling TB's of Data with Apache Spark and Scala ...
HKOSCon18 - Chetan Khatri - Scaling TB's of Data with Apache Spark and Scala ...
Chetan Khatri
 
Apache Spark Based Reliable Data Ingestion in Datalake with Gagan Agrawal
Apache Spark Based Reliable Data Ingestion in Datalake with Gagan Agrawal
Databricks
 
Introduction to Apache Spark
Introduction to Apache Spark
datamantra
 
Introduction to apache spark
Introduction to apache spark
Muktadiur Rahman
 
Introduction to Apache Spark
Introduction to Apache Spark
Juan Pedro Moreno
 
Lighting up Big Data Analytics with Apache Spark in Azure
Lighting up Big Data Analytics with Apache Spark in Azure
Jen Stirrup
 
Introduction to SparkR
Introduction to SparkR
Olgun Aydın
 

Similar to Data processing with spark in r &amp; python (20)

Apache Spark on HDinsight Training
Apache Spark on HDinsight Training
Synergetics Learning and Cloud Consulting
 
Big_data_analytics_NoSql_Module-4_Session
Big_data_analytics_NoSql_Module-4_Session
RUHULAMINHAZARIKA
 
Processing Large Data with Apache Spark -- HasGeek
Processing Large Data with Apache Spark -- HasGeek
Venkata Naga Ravi
 
Extending Apache Spark SQL Data Source APIs with Join Push Down with Ioana De...
Extending Apache Spark SQL Data Source APIs with Join Push Down with Ioana De...
Databricks
 
Pyspark presentationsfspfsjfspfjsfpsjfspfjsfpsjfsfsf
Pyspark presentationsfspfsjfspfjsfpsjfspfjsfpsjfsfsf
sasuke20y4sh
 
Large Scale Data Analytics with Spark and Cassandra on the DSE Platform
Large Scale Data Analytics with Spark and Cassandra on the DSE Platform
DataStax Academy
 
Big data processing with Apache Spark and Oracle Database
Big data processing with Apache Spark and Oracle Database
Martin Toshev
 
Unit II Real Time Data Processing tools.pptx
Unit II Real Time Data Processing tools.pptx
Rahul Borate
 
Apache spark its place within a big data stack
Apache spark its place within a big data stack
Junjun Olympia
 
Big Data training
Big Data training
vishal192091
 
Apache Spark for RDBMS Practitioners: How I Learned to Stop Worrying and Lov...
Apache Spark for RDBMS Practitioners: How I Learned to Stop Worrying and Lov...
Databricks
 
Apache Spark - Lightning Fast Cluster Computing - Hyderabad Scalability Meetup
Apache Spark - Lightning Fast Cluster Computing - Hyderabad Scalability Meetup
Hyderabad Scalability Meetup
 
Dec6 meetup spark presentation
Dec6 meetup spark presentation
Ramesh Mudunuri
 
Large Scale Machine learning with Spark
Large Scale Machine learning with Spark
Md. Mahedi Kaysar
 
Big Data Processing with Apache Spark 2014
Big Data Processing with Apache Spark 2014
mahchiev
 
2014-10-20 Large-Scale Machine Learning with Apache Spark at Internet of Thin...
2014-10-20 Large-Scale Machine Learning with Apache Spark at Internet of Thin...
DB Tsai
 
Jump Start on Apache Spark 2.2 with Databricks
Jump Start on Apache Spark 2.2 with Databricks
Anyscale
 
APACHE SPARK.pptx
APACHE SPARK.pptx
DeepaThirumurugan
 
Paris Data Geek - Spark Streaming
Paris Data Geek - Spark Streaming
Djamel Zouaoui
 
Spark and Couchbase: Augmenting the Operational Database with Spark
Spark and Couchbase: Augmenting the Operational Database with Spark
Spark Summit
 
Big_data_analytics_NoSql_Module-4_Session
Big_data_analytics_NoSql_Module-4_Session
RUHULAMINHAZARIKA
 
Processing Large Data with Apache Spark -- HasGeek
Processing Large Data with Apache Spark -- HasGeek
Venkata Naga Ravi
 
Extending Apache Spark SQL Data Source APIs with Join Push Down with Ioana De...
Extending Apache Spark SQL Data Source APIs with Join Push Down with Ioana De...
Databricks
 
Pyspark presentationsfspfsjfspfjsfpsjfspfjsfpsjfsfsf
Pyspark presentationsfspfsjfspfjsfpsjfspfjsfpsjfsfsf
sasuke20y4sh
 
Large Scale Data Analytics with Spark and Cassandra on the DSE Platform
Large Scale Data Analytics with Spark and Cassandra on the DSE Platform
DataStax Academy
 
Big data processing with Apache Spark and Oracle Database
Big data processing with Apache Spark and Oracle Database
Martin Toshev
 
Unit II Real Time Data Processing tools.pptx
Unit II Real Time Data Processing tools.pptx
Rahul Borate
 
Apache spark its place within a big data stack
Apache spark its place within a big data stack
Junjun Olympia
 
Apache Spark for RDBMS Practitioners: How I Learned to Stop Worrying and Lov...
Apache Spark for RDBMS Practitioners: How I Learned to Stop Worrying and Lov...
Databricks
 
Apache Spark - Lightning Fast Cluster Computing - Hyderabad Scalability Meetup
Apache Spark - Lightning Fast Cluster Computing - Hyderabad Scalability Meetup
Hyderabad Scalability Meetup
 
Dec6 meetup spark presentation
Dec6 meetup spark presentation
Ramesh Mudunuri
 
Large Scale Machine learning with Spark
Large Scale Machine learning with Spark
Md. Mahedi Kaysar
 
Big Data Processing with Apache Spark 2014
Big Data Processing with Apache Spark 2014
mahchiev
 
2014-10-20 Large-Scale Machine Learning with Apache Spark at Internet of Thin...
2014-10-20 Large-Scale Machine Learning with Apache Spark at Internet of Thin...
DB Tsai
 
Jump Start on Apache Spark 2.2 with Databricks
Jump Start on Apache Spark 2.2 with Databricks
Anyscale
 
Paris Data Geek - Spark Streaming
Paris Data Geek - Spark Streaming
Djamel Zouaoui
 
Spark and Couchbase: Augmenting the Operational Database with Spark
Spark and Couchbase: Augmenting the Operational Database with Spark
Spark Summit
 
Ad

More from Maloy Manna, PMP® (10)

Data Modeling in Hadoop - Essentials for building data driven applications
Data Modeling in Hadoop - Essentials for building data driven applications
Maloy Manna, PMP®
 
From Big Data to AI
From Big Data to AI
Maloy Manna, PMP®
 
Pre processing big data
Pre processing big data
Maloy Manna, PMP®
 
Data Visualization in Data Science
Data Visualization in Data Science
Maloy Manna, PMP®
 
Coursera Data Analysis and Statistical Inference 2014
Coursera Data Analysis and Statistical Inference 2014
Maloy Manna, PMP®
 
Coursera Getting and Cleaning Data 2014
Coursera Getting and Cleaning Data 2014
Maloy Manna, PMP®
 
Coursera Exploratory Data Analysis 2014
Coursera Exploratory Data Analysis 2014
Maloy Manna, PMP®
 
Scrum Certification - SFC
Scrum Certification - SFC
Maloy Manna, PMP®
 
Coursera R Programming 2014
Coursera R Programming 2014
Maloy Manna, PMP®
 
Coursera The Data Scientist's Toolbox 2014
Coursera The Data Scientist's Toolbox 2014
Maloy Manna, PMP®
 
Data Modeling in Hadoop - Essentials for building data driven applications
Data Modeling in Hadoop - Essentials for building data driven applications
Maloy Manna, PMP®
 
Data Visualization in Data Science
Data Visualization in Data Science
Maloy Manna, PMP®
 
Coursera Data Analysis and Statistical Inference 2014
Coursera Data Analysis and Statistical Inference 2014
Maloy Manna, PMP®
 
Coursera Getting and Cleaning Data 2014
Coursera Getting and Cleaning Data 2014
Maloy Manna, PMP®
 
Coursera Exploratory Data Analysis 2014
Coursera Exploratory Data Analysis 2014
Maloy Manna, PMP®
 
Coursera The Data Scientist's Toolbox 2014
Coursera The Data Scientist's Toolbox 2014
Maloy Manna, PMP®
 
Ad

Recently uploaded (20)

Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
Health Care Planning and Organization of Health Care at Various Levels – Unit...
Health Care Planning and Organization of Health Care at Various Levels – Unit...
RAKESH SAJJAN
 
Community Health Nursing Approaches, Concepts, Roles & Responsibilities – Uni...
Community Health Nursing Approaches, Concepts, Roles & Responsibilities – Uni...
RAKESH SAJJAN
 
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
National Information Standards Organization (NISO)
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
jutaydeonne
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
LDMMIA Practitioner Level Orientation Updates
LDMMIA Practitioner Level Orientation Updates
LDM & Mia eStudios
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
LDM Recording Presents Yogi Goddess by LDMMIA
LDM Recording Presents Yogi Goddess by LDMMIA
LDM & Mia eStudios
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
Health Care Planning and Organization of Health Care at Various Levels – Unit...
Health Care Planning and Organization of Health Care at Various Levels – Unit...
RAKESH SAJJAN
 
Community Health Nursing Approaches, Concepts, Roles & Responsibilities – Uni...
Community Health Nursing Approaches, Concepts, Roles & Responsibilities – Uni...
RAKESH SAJJAN
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
jutaydeonne
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
LDMMIA Practitioner Level Orientation Updates
LDMMIA Practitioner Level Orientation Updates
LDM & Mia eStudios
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
LDM Recording Presents Yogi Goddess by LDMMIA
LDM Recording Presents Yogi Goddess by LDMMIA
LDM & Mia eStudios
 

Data processing with spark in r &amp; python

  • 1. Data processing with Spark in R & Python Maloy Manna linkedin.com/in/maloy @itsmaloy biguru.wordpress.com
  • 2. Abstract With ever increasing adoption by vendors and enterprises, Spark is fast becoming the de facto big data platform. As a general purpose data processing engine, Spark can be used in both R and Python programs. In this webinar, we’ll see how to use Spark to process data from various sources in R and Python and how new tools like Spark SQL and data frames make it easy to perform structured data processing.
  • 3. Speaker profile Maloy Manna Data science engineering AXA Data Innovation Lab • Building data driven products and services for over 15 years • Worked in Thomson Reuters, Infosys, TCS and data science startup Saama linkedin.com/in/maloy @itsmaloy biguru.wordpress.com
  • 4. Agenda • Overview of Spark • Data processing operations • RDD operations – Transformations, Actions • Spark SQL – DataFrames – DataFrame operations • Spark R • Useful Tips • References
  • 5. Overview of Spark • Fast, general-purpose engine for large-scale data processing • Smarter than Hadoop in utilizing memory • Faster than MapReduce in memory & on disk • Can run on Hadoop, or standalone; can access data in HDFS, Cassandra, Hive / any Hadoop data source • Provides high-level APIs in Scala, Java, Python & R • Supports high-level tools like Spark SQL for structured data processing
  • 6. Using Spark for data science & big data • Data science lifecycle • 50% – 80% of time spent in data preparation stage • Automation is key to efficiency • R & Python already have packages & libraries for data processing • Apache Spark adds more power to R & Python big data wrangling
  • 7. Data processing Getting data to the right format for analysis: • Data manipulations • Data tidying • Data visualization reshaping formatting cleaning Transformations munging Wrangling carpentry manipulation cleaning processing
  • 8. Data processing - operations • Reshaping data Change layout (rows/columns “shape”) of dataset • Subset data Select rows or columns • Group data Group data by categories, summarize values • Make new variables Compute and append new columns, drop old columns • Combine data sets Joins, append rows/columns, set operations
  • 9. • Driver program runs main function • RDD (resilient distributed datasets) and shared variables help in parallel execution • Cluster manager distributes code and manages data in RDDs Spark for data processing
  • 10. Installing and using Spark • Install pre-compiled package http://spark.apache.org/downloads.html • Build from source code http://spark.apache.org/docs/latest/building-spark.html • Run Spark on Amazon EC2 or use Databricks Spark notebooks (Python / R) http://spark.apache.org/docs/latest/ec2-scripts.html | www.databricks.com/registration • Run as Docker image https://hub.docker.com/r/sequenceiq/spark/
  • 11. • Download pre-compiled release version • Choose “pre-built for Hadoop 2.6 and later” • Unpack/untar package • Try out the Python interactive shell bin/pyspark • Ensure JAVA_HOME is set bin/sparkR Installing Spark
  • 12. Using Spark in Python • Import Spark classes • Create SparkContext object (driver program) and initialize it • In practice, use the spark-submit script to launch applications on a cluster, using configurable options and including dependencies • Once a SparkContext is available, it can be used to build RDDs.
  • 13. RDD: Transformations & Actions • RDD is immutable, distributed data structure – Each RDD is split into multiple partitions • Can be created in 2 ways: – Loading external dataset or – Distributing a collection of objects in driver • RDDs support 2 different types of operations: – Transformations (construct new RDD) – Actions (compute result based on RDD)
  • 14. RDD: Transformations & Actions Transformations  No (lazy) evaluations  New RDD returned  Examples: ⁻ map ⁻ filter ⁻ flatMap ⁻ groupByKey ⁻ reduceByKey ⁻ aggregateByKey ⁻ union ⁻ join ⁻ coalesce Actions  Evaluations done  New value returned  Examples: ⁻ reduce ⁻ collect ⁻ count ⁻ first ⁻ take ⁻ countByKey ⁻ foreach ⁻ saveAsTextFile ⁻ saveAsSequenceFile
  • 15. Create RDDs • Creating distributed datasets – From any storage source supported by Hadoop • Use SparkContext methods: – Support directories, compressed files, wildcards
  • 16. Loading data • Loading text files • Loading unstructured JSON files • Loading sequence files
  • 17. Loading data • Loading csv files • Loading csv files in full
  • 18. Saving data • Saving text files • Saving unstructured JSON files • Saving csv files
  • 19. Spark SQL • Spark’s interface for working with structured and semi-structured data • Can load data from JSON, Hive, Parquet • Can query using SQL • Can be combined with regular code e.g. Python / Java inside Spark application • Provides “DataFrames” (SchemaRDD < v1.3) • Like RDDs, DataFrames are evaluated “lazily”
  • 20. Using Spark SQL • HiveContext (or SQLContext for a stripped- down version) based on SparkContext • Construct a SQLContext: • Basic query:
  • 21. Spark SQL: DataFrames • Spark SQL provides DataFrames as programming abstractions • A DataFrame is a distributed collection of data organized into named columns • Conceptually equivalent to relational table • Familiar syntax (R dplyr / Pandas) but scales to PBs • Entry-point remains SQLContext
  • 22. Spark SQL: DataFrame Operations • Selecting rows, columns • Grouping / aggregation • Running SQL queries • Window functions
  • 23. • Reading JSON data into dataframe in Python • Reading JSON data into dataframe in R DataFrames – Data Operations
  • 24. • Generic load/save – Python – R • Default data source parquet – Can be changed by manually specifying format DataFrames – Saving data
  • 25. SparkR • R package providing light-weight front-end to use Apache Spark from R • Entry point in SparkContext • With SQLContext, dataframes can be created from local R data frames, Hive tables or other Spark data sources • Introduced with Spark 1.4
  • 26. SparkR: Creating DataFrames • From local data frames • From data sources like JSON • From Hive tables
  • 27. Useful tips • Use Spark SQL dataframes to write less code. Easier to avoid closure problems. • Be aware of closure issues while working in cluster mode. Use accumulator variables instead of locally defined methods • Utilize Spark SQL capability to automatically infer schema of JSON datasets SQLContext.read.json • Other than using command-line, IDEs like IntelliJ IDEA community edition can be used for free
  • 28. References • Spark pages: http://spark.apache.org/ • Databricks blog: https://databricks.com/blog • Spark summit: https://spark-summit.org/ • Additional Spark packages at: http://spark-packages.org/ • Example scripts: • https://github.com/apache/spark/blob/master/examples/src/main/pytho n/sql.py • https://github.com/apache/spark/blob/master/examples/src/main/r/data -manipulation.R • https://github.com/apache/spark/blob/master/examples/src/main/r/data frame.R • API docs: http://spark.apache.org/docs/latest/api/python/index.html • http://spark.apache.org/docs/latest/api/R/index.html • Using SparkR in Rstudio: http://www.r-bloggers.com/how-to-use-sparkr- within-rstudio/