本書(shū)重點(diǎn)關(guān)注Spark項(xiàng)目的基本知識(shí),從Spark核心開(kāi)始,然后拓展到各種Spark擴(kuò)展、Spark相關(guān)項(xiàng)目、Spark子項(xiàng)目,以及Spark所處的豐富的生態(tài)系統(tǒng)里各種別的開(kāi)源技術(shù),比如Hadoop、Kafka、Cassandra等。
第一部分 Spark基礎(chǔ)
第1章 大數(shù)據(jù)、Hadoop、Spark介紹 3
1.1 大數(shù)據(jù)、分布式計(jì)算、Hadoop簡(jiǎn)介 3
1.1.1 大數(shù)據(jù)與Hadoop簡(jiǎn)史 4
1.1.2 Hadoop詳解 5
1.2 Apache Spark簡(jiǎn)介 11
1.2.1 Apache Spark背景 11
1.2.2 Spark的用途 12
1.2.3 Spark編程接口 12
1.2.4 Spark程序的提交類(lèi)型 12
1.2.5 Spark應(yīng)用程序的輸入輸出類(lèi)型 14
1.2.6 Spark中的RDD 14
1.2.7 Spark與Hadoop 14
1.3 Python函數(shù)式編程 15
1.3.1 Python函數(shù)式編程用到的數(shù)據(jù)結(jié)構(gòu) 15
1.3.2 Python對(duì)象序列化 18
1.3.3 Python函數(shù)式編程基礎(chǔ) 21
1.4 本章小結(jié) 23
第2章 部署Spark 25
2.1 Spark部署模式 25
2.1.1 本地模式 26
2.1.2 Spark獨(dú)立集群 26
2.1.3 基于YARN運(yùn)行Spark 27
2.1.4 基于Mesos運(yùn)行Spark 28
2.2 準(zhǔn)備安裝Spark 28
2.3 獲取Spark 29
2.4 在Linux或Mac OS X上安裝Spark 30
2.5 在Windows上安裝Spark 32
2.6 探索Spark安裝目錄 34
2.7 部署多節(jié)點(diǎn)的Spark獨(dú)立集群 35
2.8 在云上部署Spark 37
2.8.1 AWS 37
2.8.2 GCP 39
2.8.3 Databricks 40
2.9 本章小結(jié) 41
第3章 理解Spark集群架構(gòu) 43
3.1 Spark應(yīng)用中的術(shù)語(yǔ) 43
3.1.1 Spark驅(qū)動(dòng)器 44
3.1.2 Spark工作節(jié)點(diǎn)與執(zhí)行器 47
3.1.3 Spark主進(jìn)程與集群管理器 49
3.2 使用獨(dú)立集群的Spark應(yīng)用 51
3.3 在YARN上運(yùn)行Spark應(yīng)用的部署模式 51
3.3.1 客戶端模式 52
3.3.2 集群模式 53
3.3.3 回顧本地模式 54
3.4 本章小結(jié) 55
第4章 Spark編程基礎(chǔ) 57
4.1 RDD簡(jiǎn)介 57
4.2 加載數(shù)據(jù)到RDD 59
4.2.1 從文件創(chuàng)建RDD 59
4.2.2 從文本文件創(chuàng)建RDD的方法 61
4.2.3 從對(duì)象文件創(chuàng)建RDD 64
4.2.4 從數(shù)據(jù)源創(chuàng)建RDD 64
4.2.5 從JSON文件創(chuàng)建RDD 67
4.2.6 通過(guò)編程創(chuàng)建RDD 69
4.3 RDD操作 70
4.3.1 RDD核心概念 70
4.3.2 基本的RDD轉(zhuǎn)化操作 75
4.3.3 基本的RDD行動(dòng)操作 79
4.3.4 鍵值對(duì)RDD的轉(zhuǎn)化操作 83
4.3.5 MapReduce與單詞計(jì)數(shù)練習(xí) 90
4.3.6 連接操作 93
4.3.7 在Spark中連接數(shù)據(jù)集 98
4.3.8 集合操作 101
4.3.9 數(shù)值型RDD的操作 103
4.4 本章小結(jié) 106
第二部分 基礎(chǔ)拓展
第5章 Spark核心API高級(jí)編程 109
5.1 Spark中的共享變量 109
5.1.1 廣播變量 110
5.1.2 累加器 114
5.1.3 練習(xí):使用廣播變量和累加器 117
5.2 Spark中的數(shù)據(jù)分區(qū) 118
5.2.1 分區(qū)概述 118
5.2.2 掌控分區(qū) 119
5.2.3 重分區(qū)函數(shù) 121
5.2.4 針對(duì)分區(qū)的API方法 123
5.3 RDD的存儲(chǔ)選項(xiàng) 125
5.3.1 回顧RDD譜系 125
5.3.2 RDD存儲(chǔ)選項(xiàng) 126
5.3.3 RDD緩存 129
5.3.4 持久化RDD 129
5.3.5 選擇何時(shí)持久化或緩存RDD 132
5.3.6 保存RDD檢查點(diǎn) 132
5.3.7 練習(xí):保存RDD檢查點(diǎn) 134
5.4 使用外部程序處理RDD 136
5.5 使用Spark進(jìn)行數(shù)據(jù)采樣 137
5.6 理解Spark應(yīng)用與集群配置 139
5.6.1 Spark環(huán)境變量 139
5.6.2 Spark配置屬性 143
5.7 Spark優(yōu)化 146
5.7.1 早過(guò)濾,勤過(guò)濾 147
5.7.2 優(yōu)化滿足結(jié)合律的操作 147
5.7.3 理解函數(shù)和閉包的影響 149
5.7.4 收集數(shù)據(jù)的注意事項(xiàng) 150
5.7.5 使用配置參數(shù)調(diào)節(jié)和優(yōu)化應(yīng)用 150
5.7.6 避免低效的分區(qū) 151
5.7.7 應(yīng)用性能問(wèn)題診斷 153
5.8 本章小結(jié) 157
第6章 使用Spark進(jìn)行SQL與NoSQL編程 159
6.1 Spark SQL簡(jiǎn)介 159
6.1.1 Hive簡(jiǎn)介 160
6.1.2 Spark SQL架構(gòu) 164
6.1.3 DataFrame入門(mén) 166
6.1.4 使用DataFrame 177
6.1.5 DataFrame緩存、持久化與重新分區(qū) 185
6.1.6 保存DataFrame輸出 186
6.1.7 訪問(wèn)Spark SQL 189
6.1.8 練習(xí):使用Spark SQL 192
6.2 在Spark中使用NoSQL系統(tǒng) 193
6.2.1 NoSQL簡(jiǎn)介 194
6.2.2 在Spark中使用HBase 195
6.2.3 練習(xí):在Spark中使用HBase 198
6.2.4 在Spark中使用Cassandra 200
6.2.5 在Spark中使用DynamoDB 202
6.2.6 其他NoSQL平臺(tái) 204
6.3 本章小結(jié) 204
第7章 使用Spark處理流數(shù)據(jù)與消息 207
7.1 Spark Streaming簡(jiǎn)介 207
7.1.1 Spark Streaming架構(gòu) 208
7.1.2 DStream簡(jiǎn)介 209
7.1.3 練習(xí):Spark Streaming入門(mén) 216
7.1.4 狀態(tài)操作 217
7.1.5 滑動(dòng)窗口操作 219
7.2 結(jié)構(gòu)化流處理 221
7.2.1 結(jié)構(gòu)化流處理數(shù)據(jù)源 222
7.2.2 結(jié)構(gòu)化流處理的數(shù)據(jù)輸出池 223
7.2.3 輸出模式 224
7.2.4 結(jié)構(gòu)化流處理操作 225
7.3 在Spark中使用消息系統(tǒng) 226
7.3.1 Apache Kafka 227
7.3.2 練習(xí):在Spark中使用Kafka 232
7.3.3 亞馬遜Kinesis 235
7.4 本章小結(jié) 238
第8章 Spark數(shù)據(jù)科學(xué)與機(jī)器學(xué)習(xí)簡(jiǎn)介 241
8.1 Spark與R語(yǔ)言 241
8.1.1 R語(yǔ)言簡(jiǎn)介 242
8.1.2 通過(guò)R語(yǔ)言使用Spark 248
8.1.3 練習(xí):在RStudio中使用SparkR 255
8.2 Spark機(jī)器學(xué)習(xí) 257
8.2.1 機(jī)器學(xué)習(xí)基礎(chǔ) 257
8.2.2 使用Spark MLlib進(jìn)行機(jī)器學(xué)習(xí) 260
8.2.3 練習(xí):使用Spark MLlib實(shí)現(xiàn)推薦器 265
8.2.4 使用Spark ML進(jìn)行機(jī)器學(xué)習(xí) 269
8.3 利用筆記本使用Spark 273
8.3.1 利用Jupyter(IPython)筆記本使用Spark 273
8.3.2 利用Apache Zeppelin筆記本使用Spark 276
8.4 本章小結(jié) 277
Contents
I: Spark Foundations
1 Introducing Big Data, Hadoop, and Spark 3
Introduction to Big Data, Distributed Computing, and Hadoop 3
A Brief History of Big Data and Hadoop 4
Hadoop Explained 5
Introduction to Apache Spark 11
Apache Spark Background 11
Uses for Spark 12
Programming Interfaces to Spark 12
Submission Types for Spark Programs 12
Input/Output Types for Spark Applications 14
The Spark RDD 14
Spark and Hadoop 14
Functional Programming Using Python 15
Data Structures Used in Functional Python Programming 15
Python Object Serialization 18
Python Functional Programming Basics 21
Summary 23
2 Deploying Spark 25
Spark Deployment Modes 25
Local Mode 26
Spark Standalone 26
Spark on YARN 27
Spark on Mesos 28
Preparing to Install Spark 28
Getting Spark 29
Installing Spark on Linux or Mac OS X 30
Installing Spark on Windows 32
Exploring the Spark Installation 34
Deploying a Multi-Node Spark Standalone Cluster 35
Deploying Spark in the Cloud 37
Amazon Web Services (AWS) 37
Google Cloud Platform (GCP) 39
Databricks 40Summary 41
3 Understanding the Spark Cluster Architecture 43
Anatomy of a Spark Application 43
Spark Driver 44
Spark Workers and Executors 47
The Spark Master and Cluster Manager 49
Spark Applications Using the Standalone Scheduler 51
Deployment Modes for Spark Applications Running on YARN 51
Client Mode 52
Cluster Mode 53
Local Mode Revisited 54
Summary 55
4 Learning Spark Programming Basics 57
Introduction to RDDs 57
Loading Data into RDDs 59
Creating an RDD from a File or Files 59
Methods for Creating RDDs from a Text File or Files 61
Creating an RDD from an Object File 64
Creating an RDD from a Data Source 64
Creating RDDs from JSON Files 67
Creating an RDD Programmatically 69
Operations on RDDs 70
Key RDD Concepts 70
Basic RDD Transformations 75
Basic RDD Actions 79
Transformations on PairRDDs 83
MapReduce and Word Count Exercise 90
Join Transformations 93
Joining Datasets in Spark 98
Transformations on Sets 101
Transformations on Numeric RDDs 103
Summary 106
II: Beyond the Basics
5 Advanced Programming Using the Spark Core API 109
Shared Variables in Spark 109
Broadcast Variables 110
Accumulators 114
Exercise: Using Broadcast Variables and Accumulators 117
Partitioning Data in Spark 118
Partitioning Overview 118
Controlling Partitions 119
Repartitioning Functions 121
Partition-Specific or Partition-Aware API Methods 123
RDD Storage Options 125
RDD Lineage Revisited 125
RDD Storage Options 126
RDD Caching 129
Persisting RDDs 129
Choosing When to Persist or Cache RDDs 132
Checkpointing RDDs 132
Exercise: Checkpointing RDDs 134
Processing RDDs with External Programs 136
Data Sampling with Spark 137
Understanding Spark Application and Cluster Configuration 139
Spark Environment Variables 139
Spark Configuration Properties 143
Optimizing Spark 146
Filter Early, Filter Often 147
Optimizing Associative Operations 147
Understanding the Impact of Functions and Closures 149
Considerations for Collecting Data 150
Configuration Parameters for Tuning and Optimizing Applications 150
Avoiding Inefficient Partitioning 151
Diagnosing Application Performance Issues 153
Summary 157
6 SQL and NoSQL Programming with Spark 159
Introduction to Spark SQL 159
Introduction to Hive 160Spark SQL Architecture 164
Getting Started with DataFrames 166
Using DataFrames 177
Caching, Persisting, and Repartitioning DataFrames 185
Saving DataFrame Output 186
Accessing Spark SQL 189
Exercise: Using Spark SQL 192
Using Spark with NoSQL Systems 193
Introduction to NoSQL 194
Using Spark with HBase 195
Exercise: Using Spark with HBase 198
Using Spark with Cassandra 200
Using Spark with DynamoDB 202
Other NoSQL Platforms 204
Summary 204
7 Stream Processing and Messaging Using Spark 207
Introducing Spark Streaming 207
Spark Streaming Architecture 208
Introduction to DStreams 209
Exercise: Getting Started with Spark Streaming 216
State Operations 217
Sliding Window Operations 219
Structured Streaming 221
Structured Streaming Data Sources 222
Structured Streaming Data Sinks 223
Output Modes 224
Structured Streaming Operations 225
Using Spark with Messaging Platforms 226
Apache Kafka 227
Exercise: Using Spark with Kafka 232
Amazon Kinesis 235
Summary 238
8 Introduction to Data Science and Machine Learning Using Spark 241
Spark and R 241
Introduction to R 242
Using Spark with R 248
Exercise: Using RStudio with SparkR 255
Machine Learning with Spark 257
Machine Learning Primer 257
Machine Learning Using Spark MLlib 260
Exercise: Implementing a Recommender Using Spark MLlib 265
Machine Learning Using Spark ML 269
Using Notebooks with Spark 273
Using Jupyter (IPython) Notebooks with Spark 273
Using Apache Zeppelin Notebooks with Spark 276
Summary 277