Pycharm 搭建pyspark开发环境

本文详细介绍了如何使用PyCharm搭建PySpark开发环境,包括Spark的下载、安装及验证,以及在PyCharm中配置虚拟环境和项目变量的过程。通过示例代码展示了如何执行WordCount任务,并在本地运行Spark应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Pycharm 搭建pyspark开发环境

spark安装
  • spark下载

下载地址 http://spark.apache.org/downloads.html

本次是搭建环境使用的官网已编译的版本,如需自己编译可参照官网自行编译,地址为 http://spark.apache.org/docs/latest/building-spark.html

  • 验证spark是否安装成功
(spark_demo)  shylin  ~/Desktop/work/spark_demo  cd ~/Downloads/spark-2.4.0-bin-hadoop2.7/bin
(spark_demo)  shylin  ~/Downloads/spark-2.4.0-bin-hadoop2.7/bin  ./pyspark 
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 26 2018, 19:50:54) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
2019-05-24 10:55:19 WARN  NativeCodeLoader:62 - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /__ / .__/\_,_/_/ /_/\_\   version 2.4.0
      /_/

Using Python version 3.6.6 (v3.6.6:4cf1f54eb7, Jun 26 2018 19:50:54)
SparkSession available as 'spark'.
>>> 

搭建pyspark开发环境
  • 打开pycharm,新建project,创建一个新的虚拟环境

  • 配置项目环境变量,方便每次创建新py文件都要再次环境变量,操作如下图所示

在这里插入图片描述

SPARK_HOME /Users/shylin/Downloads/spark-2.4.0-bin-hadoop2.7(spark的下载目录)

  • 添加两个包到项目目录下 包的路径如下: /Users/shylin/Downloads/spark-2.4.0-bin-hadoop2.7/python/lib
    在这里插入图片描述
    在这里插入图片描述
  • 执行wordcount
# word_count.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from pyspark.sql import SparkSession

if __name__ == '__main__':

    spark = SparkSession.builder.appName("test").getOrCreate()
    sc = spark.sparkContext
		# file:// 指明是本地文件,默认是找hdfs文件
    counts = sc.textFile('file:///Users/shylin/Desktop/work/spark_demo/test.txt') \
            .flatMap(lambda line: line.split(" ")) \
            .map(lambda x: (x, 1)) \
            .reduceByKey(lambda a, b: a + b)

    output = counts.collect()

    for (word, count) in output:
        print("%s: %i" % (word, count))

    sc.stop()
  • 执行结果
/Users/shylin/.virtualenvs/spark_demo/bin/python /Users/shylin/Desktop/work/spark_demo/word_count.py
2019-05-24 11:23:05 WARN  NativeCodeLoader:62 - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
[Stage 0:>                                                          (0 + 2) / 2]/Users/shylin/Downloads/spark-2.4.0-bin-hadoop2.7/python/lib/pyspark.zip/pyspark/shuffle.py:60: UserWarning: Please install psutil to have better support with spilling
/Users/shylin/Downloads/spark-2.4.0-bin-hadoop2.7/python/lib/pyspark.zip/pyspark/shuffle.py:60: UserWarning: Please install psutil to have better support with spilling
world: 2
python: 3
hadoop: 1
hello: 4
spark: 1

Process finished with exit code 0

以上结果显示一个警告, pip install psutil 即可消除警告

  • 服务器上提交spark 任务
sudo -uhive spark-submit --master local[4] word_count.py

Shylin

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 to build data-intensive applications., Style and approach, This book takes a very comprehensive, step-by-step approach so you understand how the Spark ecosystem can be used with Python to develop efficient, scalable solutions. Every chapter is standalone and written in a very easy-to-understand manner, with a focus on both the hows and the whys of each concept.
PyCharm是一种功能强大的集成开发环境IDE),可用于开发Python项目。而PySpark是一个用于在Python中使用Apache Spark的库。要在PyCharm中使用PySpark,首先需要安装PySpark库。你可以使用pip命令来安装PySpark,也可以在PyCharm的项目设置中添加PySpark库的依赖。 一旦安装好PySpark,你可以在PyCharm中创建一个新项目,并在项目中编写代码来测试PySpark开发环境。例如,你可以创建一个.py文件,并在其中编写以下代码来测试PySpark环境: import pyspark if __name__ == '__main__': conf = pyspark.SparkConf().setMaster("local[*]").setAppName("PySparkTest") sc = pyspark.SparkContext(conf=conf) words = ["hello", "word", "hello", "python", "hello", "java", "hello", "spark"] rdd = sc.parallelize(words) counts = rdd.map(lambda w: (w, 1)).reduceByKey(lambda a, b: a + b) print(counts.collect()) 在PyCharm中运行上述代码,如果没有报错,则说明PySpark安装成功并且可以在PyCharm中使用。 总结来说,要在PyCharm中使用PySpark,你需要先安装PySpark库,然后在PyCharm中创建一个新项目,并在项目中编写PySpark代码。这样就可以开始使用PySpark来进行数据处理和分析了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【PythonPySpark 数据处理 ② ( 安装 PySpark | PySpark 数据处理步骤 | 构建 PySpark 执行环境入口对象 ...](https://blog.youkuaiyun.com/han1202012/article/details/132000928)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Pycharm搭建PySpark开发环境](https://blog.youkuaiyun.com/zuorichongxian_/article/details/108416411)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值