內容簡介
本書描述瞭減少數據基礎設施成本和開發時間的技巧,適用於軟件工程師、數據工程師、開發者和係統管理員。你不僅可以從中獲得關於Spark的全麵理解,也將學會如何讓它運轉自如。
在本書中你將發現:
* Spark SQL的新接口如何在SQL的RDD數據結構上改善性能
* Core Spark和Spark SQL之間的數據拼接選擇
* 充分發揮標準RDD轉換功能的技巧
* 如何處理Spark的鍵/值對範式的相關性能問題
* 編寫高性能Spark代碼,不使用Scala或JVM
* 如何在應用建議的改進措施時測試功能和性能
* 使用Spark MLlib和Spark ML機器學習庫
* Spark的流組件和外部社區軟件包
作者簡介
Holden Karau是一位跨性彆加拿大人,在IBM Spark技術中心擔任軟件開發工程師。她是Spark代碼貢獻者,並且經常提交貢獻代碼,特彆是PySpark和機器學習部分。Holden在多個國際活動中演講Spark相關話題。
Rachel Warren是Alpine Data的軟件工程師和數據科學傢。在日常工作中,她使用Spark來處理真實世界的數據和機器學習問題。她也曾在工業界和學術界擔任分析師和導師。
精彩書評
“《高性能Spark》是幫助你實現Apache Spark産品級解決方案的重要資源。由此不僅可以理解重要的Spark優化方法,還有底層的內部細節。”
—— Denny Lee(微軟資深項目經理,Azure DocumentDB團隊)
目錄
Preface
1.Introduction to High Performance Spark
What Is Spark and Why Performance Matters
What You Can Expect to Get from This Book
Spark Versions
Why Scala?
To Be a Spark Expert You Have to Learn a Little Scala Anyway
The Spark Scala API Is Easier to Use Than the lava API
Scala Is More Performant Than Python
Why Not Scala?
Learning Scala
Conclusion
2.How Spark Works
How Spark Fits into the Big Data Ecosystem
Spark Components
Spark Model of Parallel Computing: RDDs
Lazy Evaluation
In-Memory Persistence and Memory Management
Immutability and the RDD Interface
Types of RDDs
Functions on RDDs: Transformations Versus Actions
Wide Versus Narrow Dependencies
Spark Job Scheduling
Resource Allocation Across Applications
The Spark Application
The Anatomy of a Spark lob
The DAG
Jobs
Stages
Tasks
Conclusion
3.DataFrames, Datasets, and Spark SQL
Getting Started with the SparkSession (or HiveContext or SQLContext)
Spark SQL Dependencies
Managing Spark Dependencies
Avoiding Hive JARs
Basics of Schemas
DataFrame API
Transformations
Multi-DataFrame Transformations
Plain Old SQL Queries and Interacting with Hive Data
Data Representation in DataFrames and Datasets
Tungsten
Data Loading and Saving Functions
DataFrameWriter and DataFrameReader
Formats
Save Modes
Partitions (Discovery and Writing)
Datasets
Interoperability with RDDs, DataFrames, and Local Collections
Compile-Time Strong Typing
Easier Functional (RDD "like") Transformations
Relational Transformations
Multi-Dataset Relational Transformations
Grouped Operations on Datasets
Extending with User-Defined Functions and Aggregate Functions (UDFs,UDAFs)
Query Optimizer
Logical and Physical Plans
Code Generation
Large Query Plans and Iterative Algorithms
Debugging Spark SQL Queries
JDBC/ODBC Server
Conclusion
4.Joins (SQL and Core)
Core Spark Joins
Choosing a Join Type
Choosing an Execution Plan
Spark SQL Joins
DataFrame Joins
Dataset Joins
Conclusion
5.Effective Transformations
Narrow Versus Wide Transformations
Implications for Performance
Implications for Fault Tolerance
The Special Case of coalesce
What Type of RDD Does Your Transformation Return?
Minimizing Object Creation
Reusing Existing Objects
Using Smaller Data Structures
Iterator-to-Iterator Transformations with mapPartitions
What Is an Iterator-to-Iterator Transformation?
Space and Time Advantages
An Example
Set Operations
Reducing Setup Overhead
Shared Variables
Broadcast Variables
Accumulators
Reusing RDDs
Cases for Reuse
Deciding if Recompute Is Inexpensive Enough
Types of Reuse: Cache, Persist, Checkpoint, Shuffle Files
Alluxio (nee Tachyon)
LRU Caching
Noisy Cluster Considerations
Interaction with Accumulators
Conclusion
6.Working with Key/Value Data
The Goldilocks Example
Goldilocks Version 0: Iterative Solution
How to Use PairRDDFunctions and OrderedRDDFunctions
Actions on Key/Value Pairs
What's So Dangerous About the groupByKey Function
Goldilocks Version 1: groupByKey Solution
Choosing an Aggregation Operation
Dictionary of Aggregation Operations with Performance Considerations
Multiple RDD Operations
Co-Grouping
Partitioners and Key/Value Data
Using the Spark Partitioner Object
Hash Partitioning
Range Partitioning
Custom Partitioning
Preserving Partitioning Information Across Transformations
Leveraging Co-Located and Co-Partitioned RDDs
Dictionary of Mapping and Partitioning Functions PairRDDFunctions
Dictionary of OrderedRDDOperations
Sorting by Two Keys with SortByKey
Secondary Sort and repartitionAndSortWithinPartitions
Leveraging repartitionAndSortWithinPartitions for a Group by Key and Sort Values Function
How Not to Sort by Two Orderings
Goldilocks Version 2: Secondary Sort
A Different Approach to Goldilocks
Goldilocks Version 3: Sort on Cell Values
Straggler Detection and Unbalanced Data
Back to Goldilocks (Again)
Goldilocks Version 4: Reduce to Distinct on Each Partition
Conclusion
7.Going Beyond Scala
Beyond Scala within the JVM
Beyond Scala, and Beyond the JVM
How PySpark Works
How SparkR Works
Spark.jl (Julia Spark)
How Eclair JS Works
Spark on the Common Language Runtime (CLR)——C# and Friends
Calling Other Languages from Spark
Using Pipe and Friends
JNI
Java Native Access (JNA)
Underneath Everything Is FORTRAN
Getting to the GPU
The Future
Conclusion
8.Testing and Validation
Unit Testing
General Spark Unit Testing
Mocking RDDs
Getting Test Data
Generating Large Datasets
Sampling
Property Checking with ScalaCheck
Computing RDD Difference
Integration Testing
Choosing Your Integration Testing Environment
Verifying Performance
Spark Counters for Verifying Performance
Projects for Verifying Performance
Job Validation
Conclusion
9.Spark MLlib and ML
Choosing Between Spark MLlib and Spark ML
Working with MLlib
Getting Started with MLlib (Organization and Imports)
MLlib Feature Encoding and Data Preparation
Feature Scaling and Selection
MLlib Model Training
Predicting
Serving and Persistence
Model Evaluation
Working with Spark ML
Spark ML Organization and Imports
Pipeline Stages
Explain Params
Data Encoding
Data Cleaning
Spark ML Models
Putting It All Together in a Pipeline
Training a Pipeline
Accessing Individual Stages
Data Persistence and Spark ML
Extending Spark ML Pipelines with Your Own Algorithms
Model and Pipeline Persistence and Serving with Spark ML
General Serving Considerations
Conclusion
10.Spark Components and Packages
Stream Processing with Spark
Sources and Sinks
Batch Intervals
Data Checkpoint Intervals
Considerations for DStreams
Considerations for Structured Streaming
High Availability Mode (or Handling Driver Failure or Checkpointing)
GraphX
Using Community Packages and Libraries
Creating a Spark Package
Conclusion
A.Tuning, Debugging, and Other Things Developers Like to Pretend Don't Exist
Index
深入剖析分布式計算的基石:以卓越性能驅動大數據革命 本書並非一本單純的技術手冊,而是一次深刻的分布式計算之旅,旨在揭示現代大數據處理的深層機製,並提供一套係統性的方法論,以構建和優化高性能的Spark應用程序。它不僅僅是傳授API的使用技巧,更側重於理解Spark底層原理,從而在麵對海量數據和復雜計算任務時,能夠遊刃有餘地設計齣高效、可擴展且健壯的解決方案。 第一部分:理解Spark的核心——性能的基石 本部分將帶領讀者從宏觀到微觀,逐步揭開Spark的神秘麵紗。我們將首先建立對Spark整體架構的深刻理解,包括Master、Worker、Driver、Executor等核心組件的職責和交互方式。在此基礎上,我們將深入探討Spark的執行模型,特彆是其革命性的RDD(彈性分布式數據集)抽象。讀者將瞭解到RDD的惰性求值、容錯機製(Lineage)以及其作為數據處理基礎的強大之處。 隨後的章節將重點關注Spark的核心執行引擎——DAG(有嚮無環圖)調度器。我們將詳細解析Spark如何將用戶的操作轉化為一係列的Stage和Task,以及Task是如何在Executor上並行執行的。理解DAG調度器的內部工作原理,對於診斷性能瓶頸、優化任務執行順序至關重要。 數據分區(Partitioning)是分布式計算中的關鍵概念,也是Spark性能的重中之重。我們將深入探討Spark中數據分區的概念、不同的分區策略(如Hash Partitioning、Range Partitioning),以及它們如何直接影響數據shuffle的效率和計算的並行度。讀者將學會如何根據數據特性和計算需求,選擇最閤適的分區方式,從而最大程度地減少數據移動和網絡開銷。 Shuffle是Spark中成本最高的操作之一,它涉及到數據的重新分布和聚閤。本部分將對Shuffle的內部機製進行詳盡的剖析,包括Shuffle Map Task和Shuffle Reduce Task的交互過程,以及Spark如何通過各種優化技術(如Tungsten、Catalyst優化器)來提升Shuffle的性能。掌握Shuffle的細節,意味著掌握瞭Spark性能調優的“牛鼻子”。 第二部分:精進Spark應用程序——性能優化的實踐之道 在建立瞭對Spark底層原理的紮實理解後,本部分將把焦點轉嚮如何將這些知識轉化為實際的應用程序優化。我們將從數據讀取和寫入的效率入手,探討如何選擇閤適的數據源格式(如Parquet、ORC),以及如何配置Spark的輸入/輸齣參數以獲得最佳性能。 內存管理是Spark性能優化的另一大核心。我們將深入解析Spark的內存模型,包括Executor內存的劃分(Storage Memory、Execution Memory、User Memory)、內存溢齣(OOM)的成因以及各種規避和處理策略。讀者將學會如何有效地利用內存,將關鍵數據緩存(Caching)到內存中,避免不必要的磁盤I/O,從而顯著提升計算速度。 數據序列化是Spark在節點間傳輸數據時必不可少的一環,其效率直接影響著網絡的吞吐量。本部分將深入比較Kryo、Java序列化等不同的序列化機製,並指導讀者如何選擇並配置最適閤自身應用場景的序列化器,以最小化數據傳輸的開銷。 Spark SQL是Spark生態中最受歡迎的模塊之一,它提供瞭強大的聲明式查詢能力。本部分將深入解析Spark SQL的Catalyst優化器。讀者將瞭解到Catalyst是如何解析SQL語句,生成邏輯計劃和物理計劃,以及如何通過各種優化規則(如常量摺疊、謂詞下推、列裁剪)來顯著提升查詢性能。掌握Catalyst的原理,意味著掌握瞭SQL查詢優化的“秘密武器”。 緩存(Caching)和持久化(Persistence)是Spark中提升重復計算效率的關鍵手段。本部分將詳細講解`cache()`、`persist()`等方法的用法,並深入探討不同持久化級彆(如`MEMORY_ONLY`、`MEMORY_AND_DISK`)的權衡,以及何時使用它們纔能最大化收益。 第三部分:高級主題與最佳實踐——構建健壯高效的分布式係統 在掌握瞭Spark的基礎和優化技巧後,本部分將進一步深入到更高級的主題,以幫助讀者構建真正健壯且高性能的分布式應用程序。 流式計算是大數據領域日益重要的一個方嚮,Spark Streaming(以及其演進版本Spark Structured Streaming)提供瞭強大的流處理能力。本部分將深入探討Spark Streaming的微批處理模型,以及如何理解和優化其延遲和吞吐量。讀者將學習到如何處理事件時間、窗口操作以及如何應對流數據中的異常情況。 對於那些需要處理海量數據並對延遲有極緻要求的應用,廣播(Broadcasting)變量和纍加器(Accumulators)是不可或缺的工具。本部分將詳細講解它們的原理、使用場景以及最佳實踐,幫助讀者在分布式環境中高效地共享和聚閤數據。 性能調優是一個持續迭代的過程。本部分將指導讀者如何有效地利用Spark的監控工具(如Web UI、Metrics)來識彆性能瓶頸。我們將學習如何解讀執行計劃、查看任務執行詳情、分析GC(垃圾迴收)情況,並根據監控結果製定相應的優化策略。 最後,本部分還將探討Spark在生産環境中的部署、監控和故障排除的最佳實踐。我們將討論如何配置Spark集群以適應不同的工作負載,如何處理常見的性能問題和穩定性挑戰,以及如何構建一個可維護、可擴展的Spark數據處理管道。 總結 本書通過深入剖析Spark的底層原理,結閤大量的實際應用場景和優化技巧,旨在為讀者提供一套完整的、體係化的高性能Spark開發指南。它不僅僅教會你“如何做”,更重要的是讓你理解“為什麼這樣做”,從而賦能你構建齣真正能夠應對大數據挑戰的卓越應用程序。無論你是希望提升現有Spark應用的性能,還是計劃從零開始構建高性能的數據處理解決方案,本書都將是你不可或缺的寶貴資源。它將引導你穿越復雜的技術細節,抵達性能優化的彼岸,讓你成為一名真正意義上的Spark性能專傢。