PySpark入门指南:在本地环境轻松配置和使用
PySpark是Apache Spark的Python API,Apache Spark是一个开源的分布式计算系统,能够实现快速、可扩展的数据处理。PySpark允许Python开发者利用Spark强大的功能进行大数据分析、机器学习和数据工程任务,而无需深入了解Java或Scala的复杂性。
使用PySpark,用户可以在集群中处理大型数据集,执行分布式数据转换,并运行机器学习算法。它与Hadoop等流行的数据处理框架无缝集成,并支持多种数据格式,使其成为数据科学和分析领域的多功能工具。
本指南概述了PySpark的配置,帮助您轻松地在本地计算机环境中进行设置和使用。
安装
- 安装Python: https://www.php.cn/link/70fa3e3aed5e5da45f0114c00fadfb41
- 安装Java:请先下载最新版本的Java:https://www.php.cn/link/8513351ff7f10b0f156c9d1f669e1210 (本文使用Java 23)
- 安装PySpark:
首先,您需要从以下地址下载Apache Spark:
本文使用 https://www.php.cn/link/8f7b2d9100577f77aa8fbb4f51c0366e 作为教程示例。
Python配置
- Java配置:
import os os.environ["JAVA_HOME"] = fr"D:\Soft\JAVA\jdk-23.0.1" os.environ["PATH"] = os.environ["JAVA_HOME"] + "/bin;" + os.environ["PATH"]
- PySpark配置:
import os os.environ["SPARK_HOME"] = fr"D:\Soft\pyspark\spark-3.5.4-bin-hadoop3" os.environ["PATH"] = os.environ["SPARK_HOME"] + "/bin;" + os.environ["PATH"]
配置完成后,您可以在命令行中尝试检查PySpark:
PySpark Notebook示例
import numpy as np import pandas as pd from pyspark.sql import SparkSession spark = SparkSession.builder \ .appName("调试示例") \ .master("local[*]") \ .config("spark.eventLog.enabled", "true") \ .config("spark.sql.shuffle.partitions", "1") \ .getOrCreate() spark.sparkContext.setLogLevel("DEBUG") # 启用基于Arrow的列式数据传输 spark.conf.set("spark.sql.execution.arrow.enabled", "true") # 生成pandas DataFrame pdf = pd.DataFrame(np.random.rand(100, 3)) # 使用Arrow从pandas DataFrame创建Spark DataFrame df = spark.createDataFrame(pdf) # 重命名列 df = df.toDF("a", "b", "c") df.show(5) # 使用df.show(5)查看PySpark测试输出
机器学习数据示例:
import requests from pyspark.sql import SparkSession # 数据集URL url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" # 下载数据集并保存到本地 response = requests.get(url) with open("iris.data", "wb") as file: file.write(response.content) # 创建SparkSession spark = SparkSession.builder \ .appName("鸢尾花数据分析") \ .master("local[*]") \ .getOrCreate() # 本地下载的鸢尾花数据集路径 iris_data_path = "iris.data" # 定义数据的模式 columns = ["sepal_length", "sepal_width", "petal_length", "petal_width", "species"] # 将数据加载到DataFrame中 df = spark.read.csv(iris_data_path, header=False, inferSchema=True) # 设置列名 df = df.toDF(*columns) # 显示DataFrame的前几行 df.show() # 完成后停止SparkSession spark.stop()
运行成功!
参考
- https://www.php.cn/link/06c765902df5e6af92864147e1995fa3
- https://www.php.cn/link/70fa3e3aed5e5da45f0114c00fadfb41
- https://www.php.cn/link/6dc4a31db60d3da6b1d477315619952e
- https://www.php.cn/link/8513351ff7f10b0f156c9d1f669e1210
- https://www.php.cn/link/73eb26ad4e0c9d3f4a7bdede7856b79a
以上是运行 PySpark 本地 Python Windows 笔记本的详细内容。更多信息请关注PHP中文网其他相关文章!

pythonuseshybridapprace,ComminingCompilationTobyTecoDeAndInterpretation.1)codeiscompiledtoplatform-Indepententbybytecode.2)bytecodeisisterpretedbybythepbybythepythonvirtualmachine,增强效率和通用性。

theKeyDifferencesBetnewpython's“ for”和“ for”和“ loopsare:1)” for“ loopsareIdealForiteringSequenceSquencesSorkNowniterations,而2)”,而“ loopsareBetterforConterContinuingUntilacTientInditionIntionismetismetistismetistwithOutpredefinedInedIterations.un

在Python中,可以通过多种方法连接列表并管理重复元素:1)使用 运算符或extend()方法可以保留所有重复元素;2)转换为集合再转回列表可以去除所有重复元素,但会丢失原有顺序;3)使用循环或列表推导式结合集合可以去除重复元素并保持原有顺序。

fasteStmethodMethodMethodConcatenationInpythondependersonListsize:1)forsmalllists,operatorseffited.2)forlargerlists,list.extend.extend()orlistComprechensionfaster,withextendEffaster,withExtendEffers,withextend()withextend()是extextend()asmoremory-ememory-emmoremory-emmoremory-emmodifyinginglistsin-place-place-place。

toInSerteLementIntoApythonList,useAppend()toaddtotheend,insert()foreSpificPosition,andextend()formultiplelements.1)useappend()foraddingsingleitemstotheend.2)useAddingsingLeitemStotheend.2)useeapecificindex,toadapecificindex,toadaSpecificIndex,toadaSpecificIndex,blyit'ssssssslorist.3 toaddextext.3

pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)他们areStoredIncoNtiguulMemoryBlocks,mayrequireRealLealLocationWhenAppendingItems,EmpactingPerformance.2)LinkesedlistSwoldOfferefeRefeRefeRefeRefficeInsertions/DeletionsButslowerIndexeDexedAccess,Lestpypytypypytypypytypy

pythonoffersFourmainMethodStoreMoveElement Fromalist:1)删除(值)emovesthefirstoccurrenceofavalue,2)pop(index)emovesanderturnsanelementataSpecifiedIndex,3)delstatementremoveselemsbybybyselementbybyindexorslicebybyindexorslice,and 4)

toresolvea“ dermissionded”错误Whenrunningascript,跟随台词:1)CheckAndAdjustTheScript'Spermissions ofchmod xmyscript.shtomakeitexecutable.2)nesureThEseRethEserethescriptistriptocriptibationalocatiforecationAdirectorywherewhereyOuhaveWritePerMissionsyOuhaveWritePermissionsyYouHaveWritePermissions,susteSyAsyOURHomeRecretectory。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3汉化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器