Table Of ContentBeginning
Apache Spark Using
Azure Databricks
Unleashing Large Cluster Analytics
in the Cloud
—
Robert Ilijason
Beginning Apache Spark
Using Azure Databricks
Unleashing Large Cluster Analytics
in the Cloud
Robert Ilijason
Beginning Apache Spark Using Azure Databricks: Unleashing Large Cluster
Analytics in the Cloud
Robert Ilijason
Viken, Sweden
ISBN-13 (pbk): 978-1-4842-5780-7 ISBN-13 (electronic): 978-1-4842-5781-4
https://doi.org/10.1007/978-1-4842-5781-4
Copyright © 2020 by Robert Ilijason
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Joan Murray
Development Editor: Laura Berendson
Coordinating Editor: Jill Balzano
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-
sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member
(owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a
Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit http://www.apress.com/
rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484257807. For more
detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
Select support from family;
SUPPORT
----------
Malin
Max
Mom
Table of Contents
About the Author ���������������������������������������������������������������������������������������������������xiii
About the Technical Reviewer ���������������������������������������������������������������������������������xv
Introduction �����������������������������������������������������������������������������������������������������������xvii
Chapter 1: Introduction to Large-Scale Data Analytics ��������������������������������������������1
Analytics, the hype ������������������������������������������������������������������������������������������������������������������������1
Analytics, the reality ����������������������������������������������������������������������������������������������������������������������2
Large-scale analytics for fun and profit ����������������������������������������������������������������������������������������3
Data: Fueling analytics ������������������������������������������������������������������������������������������������������������������5
Free as in speech� And beer! ��������������������������������������������������������������������������������������������������������7
Into the clouds ������������������������������������������������������������������������������������������������������������������������������8
Databricks: Analytics for the lazy ones �����������������������������������������������������������������������������������������9
How to analyze data ��������������������������������������������������������������������������������������������������������������������10
Large-scale examples from the real world ���������������������������������������������������������������������������������12
Telematics at Volvo Trucks �����������������������������������������������������������������������������������������������������12
Fraud detection at Visa ����������������������������������������������������������������������������������������������������������12
Customer analytics at Target �������������������������������������������������������������������������������������������������13
Targeted ads at Cambridge Analytica ������������������������������������������������������������������������������������13
Summary�������������������������������������������������������������������������������������������������������������������������������������14
Chapter 2: Spark and Databricks ���������������������������������������������������������������������������15
Apache Spark, the short overview ����������������������������������������������������������������������������������������������15
Databricks: Managed Spark ��������������������������������������������������������������������������������������������������������17
The far side of the Databricks moon �������������������������������������������������������������������������������������18
Spark architecture ����������������������������������������������������������������������������������������������������������������������19
Apache Spark processing ������������������������������������������������������������������������������������������������������20
v
Table of ConTenTs
Cool components on top of Core �������������������������������������������������������������������������������������������������24
Summary�������������������������������������������������������������������������������������������������������������������������������������24
Chapter 3: Getting Started with Databricks ������������������������������������������������������������27
Cloud-only �����������������������������������������������������������������������������������������������������������������������������������27
Community edition: No money? No problem �������������������������������������������������������������������������������28
Mostly good enough ��������������������������������������������������������������������������������������������������������������28
Getting started with the community edition ��������������������������������������������������������������������������29
Commercial editions: The ones you want �����������������������������������������������������������������������������������30
Databricks on Amazon Web Services ������������������������������������������������������������������������������������32
Azure Databricks �������������������������������������������������������������������������������������������������������������������36
Summary�������������������������������������������������������������������������������������������������������������������������������������38
Chapter 4: Workspaces, Clusters, and Notebooks ��������������������������������������������������39
Getting around in the UI ��������������������������������������������������������������������������������������������������������������39
Clusters: Powering up the engines ���������������������������������������������������������������������������������������������42
Data: Getting access to the fuel ��������������������������������������������������������������������������������������������������45
Notebooks: Where the work happens �����������������������������������������������������������������������������������������46
Summary�������������������������������������������������������������������������������������������������������������������������������������49
Chapter 5: Getting Data into Databricks �����������������������������������������������������������������51
Databricks File System ���������������������������������������������������������������������������������������������������������������51
Navigating the file system �����������������������������������������������������������������������������������������������������52
The FileStore, a portal to your data ���������������������������������������������������������������������������������������55
Schemas, databases, and tables ������������������������������������������������������������������������������������������������55
Hive Metastore ����������������������������������������������������������������������������������������������������������������������56
The many types of source files ���������������������������������������������������������������������������������������������������57
Going binary ��������������������������������������������������������������������������������������������������������������������������58
Alternative transportation ������������������������������������������������������������������������������������������������������60
Importing from your computer ����������������������������������������������������������������������������������������������������60
Getting data from the Web ����������������������������������������������������������������������������������������������������������62
Working with the shell �����������������������������������������������������������������������������������������������������������62
vi
Table of ConTenTs
Basic importing with Python �������������������������������������������������������������������������������������������������64
Getting data with SQL ������������������������������������������������������������������������������������������������������������66
Mounting a file system ����������������������������������������������������������������������������������������������������������������67
Mounting example Amazon S3 ����������������������������������������������������������������������������������������������67
Mounting example Microsoft Blog Storage ���������������������������������������������������������������������������69
Getting rid of the mounts �������������������������������������������������������������������������������������������������������70
How to get data out of Databricks ����������������������������������������������������������������������������������������������71
Summary�������������������������������������������������������������������������������������������������������������������������������������72
Chapter 6: Querying Data Using SQL�����������������������������������������������������������������������75
The Databricks flavor ������������������������������������������������������������������������������������������������������������������75
Getting started ����������������������������������������������������������������������������������������������������������������������������76
Picking up data ���������������������������������������������������������������������������������������������������������������������������77
Filtering data �������������������������������������������������������������������������������������������������������������������������������79
Joins and merges������������������������������������������������������������������������������������������������������������������������82
Ordering data ������������������������������������������������������������������������������������������������������������������������������84
Functions ������������������������������������������������������������������������������������������������������������������������������������85
Windowing functions ������������������������������������������������������������������������������������������������������������������86
A view worth keeping �����������������������������������������������������������������������������������������������������������������89
Hierarchical data �������������������������������������������������������������������������������������������������������������������������90
Creating data�������������������������������������������������������������������������������������������������������������������������������91
Manipulating data �����������������������������������������������������������������������������������������������������������������������94
Delta Lake SQL ����������������������������������������������������������������������������������������������������������������������������95
UPDATE, DELETE, and MERGE ������������������������������������������������������������������������������������������������96
Keeping Delta Lake in order ��������������������������������������������������������������������������������������������������98
Transaction logs ��������������������������������������������������������������������������������������������������������������������99
Selecting metadata ���������������������������������������������������������������������������������������������������������������99
Gathering statistics��������������������������������������������������������������������������������������������������������������101
Summary�����������������������������������������������������������������������������������������������������������������������������������102
vii
Table of ConTenTs
Chapter 7: The Power of Python ���������������������������������������������������������������������������103
Python: The language of choice ������������������������������������������������������������������������������������������������103
A turbo-charged intro to Python ������������������������������������������������������������������������������������������������104
Finding the data ������������������������������������������������������������������������������������������������������������������������107
DataFrames: Where active data lives ����������������������������������������������������������������������������������������108
Getting some data ���������������������������������������������������������������������������������������������������������������������110
Selecting data from DataFrames ����������������������������������������������������������������������������������������������114
Chaining combo commands������������������������������������������������������������������������������������������������������116
Working with multiple DataFrames �������������������������������������������������������������������������������������������125
Slamming data together �����������������������������������������������������������������������������������������������������������132
Summary�����������������������������������������������������������������������������������������������������������������������������������136
Chapter 8: ETL and Advanced Data Wrangling �����������������������������������������������������139
ETL: A recap ������������������������������������������������������������������������������������������������������������������������������139
An overview of the Spark UI ������������������������������������������������������������������������������������������������������140
Cleaning and transforming data �����������������������������������������������������������������������������������������������142
Finding nulls ������������������������������������������������������������������������������������������������������������������������143
Getting rid of nulls ���������������������������������������������������������������������������������������������������������������144
Filling nulls with values �������������������������������������������������������������������������������������������������������146
Removing duplicates �����������������������������������������������������������������������������������������������������������148
Identifying and clearing out extreme values �����������������������������������������������������������������������150
Taking care of columns �������������������������������������������������������������������������������������������������������153
Pivoting ��������������������������������������������������������������������������������������������������������������������������������154
Explode ��������������������������������������������������������������������������������������������������������������������������������156
When being lazy is good ������������������������������������������������������������������������������������������������������156
Caching data ������������������������������������������������������������������������������������������������������������������������158
Data compression ����������������������������������������������������������������������������������������������������������������160
A short note about functions �����������������������������������������������������������������������������������������������163
Lambda functions ����������������������������������������������������������������������������������������������������������������164
Storing and shuffling data���������������������������������������������������������������������������������������������������������165
Save modes �������������������������������������������������������������������������������������������������������������������������165
viii
Table of ConTenTs
Managed vs� unmanaged tables �����������������������������������������������������������������������������������������167
Handling partitions ��������������������������������������������������������������������������������������������������������������169
Summary �����������������������������������������������������������������������������������������������������������������������������174
Chapter 9: Connecting to and from Databricks ����������������������������������������������������177
Connecting to and from Databricks ������������������������������������������������������������������������������������������177
Getting ODBC and JDBC up and running �����������������������������������������������������������������������������������178
Creating a token ������������������������������������������������������������������������������������������������������������������179
Preparing the cluster �����������������������������������������������������������������������������������������������������������180
Let’s create a test table �������������������������������������������������������������������������������������������������������180
Setting up ODBC on Windows ����������������������������������������������������������������������������������������������181
Setting up ODBC on OS X �����������������������������������������������������������������������������������������������������182
Connecting tools to Databricks �������������������������������������������������������������������������������������������������185
Microsoft Excel on Windows ������������������������������������������������������������������������������������������������185
Microsoft Power BI Desktop on Windows ����������������������������������������������������������������������������186
Tableau on OS X �������������������������������������������������������������������������������������������������������������������187
PyCharm (and more) via Databricks Connect ����������������������������������������������������������������������188
Using RStudio Server ����������������������������������������������������������������������������������������������������������������191
Accessing external systems �����������������������������������������������������������������������������������������������������193
A quick recap of libraries ����������������������������������������������������������������������������������������������������194
Connecting to external systems ������������������������������������������������������������������������������������������������195
Azure SQL ����������������������������������������������������������������������������������������������������������������������������195
Oracle ����������������������������������������������������������������������������������������������������������������������������������196
MongoDB �����������������������������������������������������������������������������������������������������������������������������198
Summary�����������������������������������������������������������������������������������������������������������������������������������199
Chapter 10: Running in Production ����������������������������������������������������������������������201
General advice ��������������������������������������������������������������������������������������������������������������������������201
Assume the worst ����������������������������������������������������������������������������������������������������������������202
Write rerunnable code ���������������������������������������������������������������������������������������������������������202
Document in the code ���������������������������������������������������������������������������������������������������������202
Write clear, simple code ������������������������������������������������������������������������������������������������������203
Print relevant stuff ���������������������������������������������������������������������������������������������������������������204
ix
Table of ConTenTs
Jobs ������������������������������������������������������������������������������������������������������������������������������������������204
Scheduling ���������������������������������������������������������������������������������������������������������������������������206
Running notebooks from notebooks ������������������������������������������������������������������������������������206
Widgets ��������������������������������������������������������������������������������������������������������������������������������208
Running jobs with parameters ��������������������������������������������������������������������������������������������210
The command line interface �����������������������������������������������������������������������������������������������������212
Setting up the CLI ����������������������������������������������������������������������������������������������������������������212
Running CLI commands �������������������������������������������������������������������������������������������������������213
Revisiting cost ���������������������������������������������������������������������������������������������������������������������������220
Users, groups, and security options ������������������������������������������������������������������������������������������220
Users and groups ����������������������������������������������������������������������������������������������������������������221
Access Control ���������������������������������������������������������������������������������������������������������������������222
The rest �������������������������������������������������������������������������������������������������������������������������������225
Summary�����������������������������������������������������������������������������������������������������������������������������������225
Chapter 11: Bits and Pieces ���������������������������������������������������������������������������������227
MLlib �����������������������������������������������������������������������������������������������������������������������������������������228
Frequent Pattern Growth �����������������������������������������������������������������������������������������������������������228
Creating some data �������������������������������������������������������������������������������������������������������������������229
Preparing the data ��������������������������������������������������������������������������������������������������������������������230
Running the algorithm ��������������������������������������������������������������������������������������������������������������231
Parsing the results ��������������������������������������������������������������������������������������������������������������������232
MLflow ��������������������������������������������������������������������������������������������������������������������������������������233
Running the code ����������������������������������������������������������������������������������������������������������������233
Checking the results �����������������������������������������������������������������������������������������������������������������236
Updating tables �������������������������������������������������������������������������������������������������������������������������236
Create the original table ������������������������������������������������������������������������������������������������������������237
Connect from Databricks ����������������������������������������������������������������������������������������������������������238
Pulling the delta ������������������������������������������������������������������������������������������������������������������������239
Verifying the formats ����������������������������������������������������������������������������������������������������������������240
Update the table ������������������������������������������������������������������������������������������������������������������������241
x