O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Spark SQL

Book Description

Design, implement, and deliver successful streaming applications, machine learning pipelines and graph applications using Spark SQL API

About This Book

  • Learn about the design and implementation of streaming applications, machine learning pipelines, deep learning, and large-scale graph processing applications using Spark SQL APIs and Scala.
  • Learn data exploration, data munging, and how to process structured and semi-structured data using real-world datasets and gain hands-on exposure to the issues and challenges of working with noisy and "dirty" real-world data.
  • Understand design considerations for scalability and performance in web-scale Spark application architectures.

Who This Book Is For

If you are a developer, engineer, or an architect and want to learn how to use Apache Spark in a web-scale project, then this is the book for you. It is assumed that you have prior knowledge of SQL querying. A basic programming knowledge with Scala, Java, R, or Python is all you need to get started with this book.

What You Will Learn

  • Familiarize yourself with Spark SQL programming, including working with DataFrame/Dataset API and SQL
  • Perform a series of hands-on exercises with different types of data sources, including CSV, JSON, Avro, MySQL, and MongoDB
  • Perform data quality checks, data visualization, and basic statistical analysis tasks
  • Perform data munging tasks on publically available datasets
  • Learn how to use Spark SQL and Apache Kafka to build streaming applications
  • Learn key performance-tuning tips and tricks in Spark SQL applications
  • Learn key architectural components and patterns in large-scale Spark SQL applications

In Detail

In the past year, Apache Spark has been increasingly adopted for the development of distributed applications. Spark SQL APIs provide an optimized interface that helps developers build such applications quickly and easily. However, designing web-scale production applications using Spark SQL APIs can be a complex task. Hence, understanding the design and implementation best practices before you start your project will help you avoid these problems.

This book gives an insight into the engineering practices used to design and build real-world, Spark-based applications. The book's hands-on examples will give you the required confidence to work on any future projects you encounter in Spark SQL.

It starts by familiarizing you with data exploration and data munging tasks using Spark SQL and Scala. Extensive code examples will help you understand the methods used to implement typical use-cases for various types of applications. You will get a walkthrough of the key concepts and terms that are common to streaming, machine learning, and graph applications. You will also learn key performance-tuning details including Cost Based Optimization (Spark 2.2) in Spark SQL applications. Finally, you will move on to learning how such systems are architected and deployed for a successful delivery of your project.

Style and approach

This book is a hands-on guide to designing, building, and deploying Spark SQL-centric production applications at scale.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Getting Started with Spark SQL
    1. What is Spark SQL?
    2. Introducing SparkSession
    3. Understanding Spark SQL concepts
      1. Understanding Resilient Distributed Datasets (RDDs)
      2. Understanding DataFrames and Datasets
      3. Understanding the Catalyst optimizer
        1. Understanding Catalyst optimizations
        2. Understanding Catalyst transformations
      4. Introducing Project Tungsten
    4. Using Spark SQL in streaming applications
      1. Understanding Structured Streaming internals
    5. Summary
  3. Using Spark SQL for Processing Structured and Semistructured Data
    1. Understanding data sources in Spark applications
      1. Selecting Spark data sources
    2. Using Spark with relational databases
    3. Using Spark with MongoDB (NoSQL database)
    4. Using Spark with JSON data
    5. Using Spark with Avro files
    6. Using Spark with Parquet files
    7. Defining and using custom data sources in Spark
    8. Summary
  4. Using Spark SQL for Data Exploration
    1. Introducing Exploratory Data Analysis (EDA)
    2. Using Spark SQL for basic data analysis
      1. Identifying missing data
      2. Computing basic statistics
      3. Identifying data outliers
    3. Visualizing data with Apache Zeppelin
    4. Sampling data with Spark SQL APIs
      1. Sampling with the DataFrame/Dataset API
      2. Sampling with the RDD API
    5. Using Spark SQL for creating pivot tables
    6. Summary
  5. Using Spark SQL for Data Munging
    1. Introducing data munging
    2. Exploring data munging techniques
      1. Pre-processing of the household electric consumption Dataset
      2. Computing basic statistics and aggregations
      3. Augmenting the Dataset
      4. Executing other miscellaneous processing steps
      5. Pre-processing of the weather Dataset
      6. Analyzing missing data
      7. Combining data using a JOIN operation
    3. Munging textual data
      1. Processing multiple input data files
      2. Removing stop words
    4. Munging time series data
      1. Pre-processing of the time-series Dataset
      2. Processing date fields
      3. Persisting and loading data
      4. Defining a date-time index
      5. Using the  TimeSeriesRDD object
      6. Handling missing time-series data
      7. Computing basic statistics
    5. Dealing with variable length records
      1. Converting variable-length records to fixed-length records
      2. Extracting data from "messy" columns
    6. Preparing data for machine learning
      1. Pre-processing data for machine learning
      2. Creating and running a machine learning pipeline
    7. Summary
  6. Using Spark SQL in Streaming Applications
    1. Introducing streaming data applications
    2. Building Spark streaming applications
      1. Implementing sliding window-based functionality
      2. Joining a streaming Dataset with a static Dataset
      3. Using the Dataset API in Structured Streaming
      4. Using output sinks
        1. Using the Foreach Sink for arbitrary computations on output
        2. Using the Memory Sink to save output to a table
        3. Using the File Sink to save output to a partitioned table
      5. Monitoring streaming queries
    3. Using Kafka with Spark Structured Streaming
      1. Introducing Kafka concepts
      2. Introducing ZooKeeper concepts
      3. Introducing Kafka-Spark integration
      4. Introducing Kafka-Spark Structured Streaming
    4. Writing a receiver for a custom data source
    5. Summary
  7. Using Spark SQL in Machine Learning Applications
    1. Introducing machine learning applications
      1. Understanding Spark ML pipelines and their components
      2. Understanding the steps in a pipeline application development process
    2. Introducing feature engineering
      1. Creating new features from raw data
      2. Estimating the importance of a feature
      3. Understanding dimensionality reduction
      4. Deriving good features
    3. Implementing a Spark ML classification model
      1. Exploring the diabetes Dataset
      2. Pre-processing the data
      3. Building the Spark ML pipeline
        1. Using StringIndexer for indexing categorical features and labels
        2. Using VectorAssembler for assembling features into one column
        3. Using a Spark ML classifier
        4. Creating a Spark ML pipeline
        5. Creating the training and test Datasets
        6. Making predictions using the PipelineModel
        7. Selecting the best model
      4. Changing the ML algorithm in the pipeline
    4. Introducing Spark ML tools and utilities
      1. Using Principal Component Analysis to select features
      2. Using encoders
      3. Using Bucketizer
      4. Using VectorSlicer
      5. Using Chi-squared selector
      6. Using a Normalizer
      7. Retrieving our original labels
    5. Implementing a Spark ML clustering model
    6. Summary
  8. Using Spark SQL in Graph Applications
    1. Introducing large-scale graph applications
    2. Exploring graphs using GraphFrames
      1. Constructing a GraphFrame
      2. Basic graph queries and operations
      3. Motif analysis using GraphFrames
      4. Processing subgraphs
      5. Applying graph algorithms
      6. Saving and loading GraphFrames
    3. Analyzing JSON input modeled as a graph 
    4. Processing graphs containing multiple types of relationships
    5. Understanding GraphFrame internals
      1. Viewing GraphFrame physical execution plan
      2. Understanding partitioning in GraphFrames
    6. Summary
  9. Using Spark SQL with SparkR
    1. Introducing SparkR
    2. Understanding the SparkR architecture
    3. Understanding SparkR DataFrames
    4. Using SparkR for EDA and data munging tasks
      1. Reading and writing Spark DataFrames
      2. Exploring structure and contents of Spark DataFrames
      3. Running basic operations on Spark DataFrames
      4. Executing SQL statements on Spark DataFrames
      5. Merging SparkR DataFrames
      6. Using User Defined Functions (UDFs)
    5. Using SparkR for computing summary statistics
    6. Using SparkR for data visualization
      1. Visualizing data on a map
      2. Visualizing graph nodes and edges
    7. Using SparkR for machine learning
    8. Summary
  10. Developing Applications with Spark SQL
    1. Introducing Spark SQL applications
    2. Understanding text analysis applications
      1. Using Spark SQL for textual analysis
        1. Preprocessing textual data
        2. Computing readability
        3. Using word lists
      2. Creating data preprocessing pipelines
    3. Understanding themes in document corpuses
    4. Using Naive Bayes classifiers
    5. Developing a machine learning application
    6. Summary
  11. Using Spark SQL in Deep Learning Applications
    1. Introducing neural networks
      1. Understanding deep learning
      2. Understanding representation learning
      3. Understanding stochastic gradient descent
    2. Introducing deep learning in Spark
      1. Introducing CaffeOnSpark
      2. Introducing DL4J
      3. Introducing TensorFrames
      4. Working with BigDL
      5. Tuning hyperparameters of deep learning models
      6. Introducing deep learning pipelines
    3. Understanding Supervised learning
      1. Understanding convolutional neural networks
      2. Using neural networks for text classification
    4. Using deep neural networks for language processing
      1. Understanding Recurrent Neural Networks
    5. Introducing autoencoders
    6. Summary
  12. Tuning Spark SQL Components for Performance
    1. Introducing performance tuning in Spark SQL
    2. Understanding DataFrame/Dataset APIs
      1. Optimizing data serialization
    3. Understanding Catalyst optimizations
      1. Understanding the Dataset/DataFrame API
      2. Understanding Catalyst transformations
    4. Visualizing Spark application execution
      1. Exploring Spark application execution metrics
      2. Using external tools for performance tuning
    5. Cost-based optimizer in Apache Spark 2.2
      1. Understanding the CBO statistics collection
      2. Statistics collection functions
        1. Filter operator
        2. Join operator
      3. Build side selection
    6. Understanding multi-way JOIN ordering optimization
    7. Understanding performance improvements using whole-stage code generation
    8. Summary
  13. Spark SQL in Large-Scale Application Architectures
    1. Understanding Spark-based application architectures
      1. Using Apache Spark for batch processing
      2. Using Apache Spark for stream processing
    2. Understanding the Lambda architecture
    3. Understanding the Kappa Architecture
    4. Design considerations for building scalable stream processing applications
    5. Building robust ETL pipelines using Spark SQL
      1. Choosing appropriate data formats
      2. Transforming data in ETL pipelines
      3. Addressing errors in ETL pipelines
    6. Implementing a scalable monitoring solution
    7. Deploying Spark machine learning pipelines
      1. Understanding the challenges in typical ML deployment environments
      2. Understanding types of model scoring architectures
    8. Using cluster managers
    9. Summary