手把手带你入门PySpark!

本文详细介绍了PySpark的入门,包括如何在Databricks中设置PySpark集群,使用Spark数据帧进行数据处理,以及如何进行数据读取、转换、分析和可视化。文章强调了避免急切操作和使用Pandas UDF的重要性,提供了从CSV到Parquet格式的转换示例,并探讨了如何使用MLlib构建预测模型。此外,还分享了最佳实践,如避免for循环和使用Pandas UDF以提高可扩展性。
摘要由CSDN通过智能技术生成

PySpark数据科学入门

PySpark是一种很好的语言,可以大规模地进行探索性数据分析、构建机器学习管道以及为数据平台创建ETL。如果您已经熟悉Python和Pandas等库,那么PySpark是一种很好的语言,可以用来创建更具扩展性的分析和管道。这篇文章的目的是展示如何启动和运行PySpark并执行常见任务。

我们将使用Databricks作为Spark环境,将Kaggle的NHL数据集用作分析的数据源。这篇文章展示了如何在Spark 数据帧写入数据,创建这些帧的转换和聚合,可视化结果以及执行线性回归。我还将展示如何使用Pandas UDF以可扩展的方式将常规Python代码与PySpark混合。为了简单起见,我们将专注于批处理并避免流数据管道出现的一些复杂问题。

这篇文章的完整笔记本可以在github上找到。(https://github.com/bgweber/StartupDataScience/blob/master/EDA/PySpark_NHL.ipynb)

环境

启动和运行Spark有许多不同的选项:

  • 自托管:您可以使用裸机或虚拟机自行设置群集。 Apache Ambari是这个选项的一个有用的项目,但它不是我推荐的快速启动和运行的方法。
  • 云提供商:大多数云提供商都提供Spark集群:AWS具有EMR,GCP具有DataProc,它可以比自托管更快地进入互动环境。
  • 供应商解决方案:包括Databricks和Cloudera在内的公司提供Spark解决方案,使Spark易于启动和运行。

使用的解决方案因安全性、成本和现有基础架构而异。如果您正在尝试使用并运行一个需要的环境来学习,那么我建议您使用Databricks Community Edition。

手把手带你入门PySpark!

在Databricks Community Edition中创建PySpark集群

使用此环境,可以轻松启动并运行Spark群集和笔记本环境。在本教程中,我使用Spark 2.4运行时和Python 3创建了一个集群。要运行本文中的代码,您至少需要Spark版本2.3,才能获得Pandas UDF功能。

Spark 数据帧

PySpark中使用的关键数据类型是Spark 数据帧。此对象可以被视为分布在集群中的表,其功能类似于R和Pandas中的数据帧。如果你想使用PySpark进行分布式计算,那么你需要对Spark 数据帧执行操作,而不是其他python数据类型。

使用Spark时,也可以通过在Spark数据帧上调用toPandas()来使用Pandas数据帧,该数据帧返回一个pandas对象。但是,除了处理小型数据帧之外,通常应该避免使用此函数,因为它将整个对象拉入单个节点的内存中。

Pandas和Spark数据帧之间的主要区别之一是急切执行和延迟执行。在PySpark中,操作被延迟,直到管道中实际需要结果。例如,您可以指定从S3加载数据集并对数据帧应用多个转换的操作,但不会立即应用这些操作。相反,记录转换图,并且一旦实际需要数据,例如当将结果写回S3时,则转换被应用为单个管道操作。此方法用于避免将完整数据帧拉入内存,并在整个计算机集群中实现更有效的处理。使用Pandas数据帧,一切都被拉入内存,并立即应用每个Pandas操作。

通常,如果可能的话,最好避免在Spark中进行急切操作,因为它限制了有效分配的管道数量。

阅读数据

使用Spark时学习的第一步是将数据集加载到数据帧中。将数据加载到数据帧后,您可以应用转换、执行分析和建模,创建可视化并保留结果。在Python中,您可以使用Pandas直接从本地文件系统加载文件:

手把手带你入门PySpark!

 

在PySpark中,加载CSV文件要复杂一些。在分布式环境中,没有本地存储,因此需要使用分布式文件系统(如HDFS,Databricks文件存储(DBFS)或S3)来指定文件的路径。

通常,在使用PySpark时,我使用S3中的数据。许多数据库都提供了对S3功能的卸载,并且还可以使用AWS控制台将文件从本地计算机移动到S3。对于这篇文章,我将使用Databricks文件系统(DBFS),它以/ FileStore的形式提供路径。第一步是上传您要处理的CSV文件。

手把手带你入门PySpark!

将文件上载到Databricks文件

About This Book, Learn why and how you can efficiently use Python to process data and build machine learning models in Apache Spark 2.0Develop and deploy efficient, scalable real-time Spark solutionsTake your understanding of using Spark with Python to the next level with this jump start guide, Who This Book Is For, If you are a Python developer who wants to learn about the Apache Spark 2.0 ecosystem, this book is for you. A firm understanding of Python is expected to get the best out of the book. Familiarity with Spark would be useful, but is not mandatory., What You Will Learn, Learn about Apache Spark and the Spark 2.0 architectureBuild and interact with Spark DataFrames using Spark SQLLearn how to solve graph and deep learning problems using GraphFrames and TensorFrames respectivelyRead, transform, and understand data and use it to train machine learning modelsBuild machine learning models with MLlib and MLLearn how to submit your applications programmatically using spark-submitDeploy locally built applications to a cluster, In Detail, Apache Spark is an open source framework for efficient cluster computing with a strong interface for data parallelism and fault tolerance. This book will show you how to leverage the power of Python and put it to use in the Spark ecosystem. You will start by getting a firm understanding of the Spark 2.0 architecture and how to set up a Python environment for Spark., You will get familiar with the modules available in PySpark. You will learn how to abstract data with RDDs and DataFrames and understand the streaming capabilities of PySpark. Also, you will get a thorough overview of machine learning capabilities of PySpark using ML and MLlib, graph processing using GraphFrames, and polyglot persistence using Blaze. Finally, you will learn how to deploy your applications to the cloud using the spark-submit command., By the end of this book, you will have established a firm understanding of the Spark Python API and how it can be used t
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值