Pycharm 搭建pyspark开发环境

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是一款由JetBrains公司开发的Python集成开发环境,支持代码分析、图形化调试以及集成版本控制等特性,非常适合进行Python开发工作。而PySpark是Apache SparkPython API,它提供了一个高性能的集群计算系统,并且对Python语言提供了良好的支持。 要在PyCharm中开发PySpark项目,你可以遵循以下步骤: 1. 安装PyCharm:从JetBrains官网下载并安装PyCharm到你的电脑上。 2. 安装Python解释器:在PyCharm中创建一个新的项目,并选择安装Python解释器。可以通过PyCharm的项目解释器设置来安装或者配置已有的Python环境。 3. 安装PySpark:在PyCharm的终端中,使用pip命令安装PySpark库。通常命令如下: ``` pip install pyspark ``` 也可以选择使用conda来安装,如果使用的是conda环境管理器: ``` conda install pyspark ``` 4. 创建Spark配置文件:在项目目录中创建一个名为`spark-defaults.conf`的文件,配置必要的Spark参数。例如: ``` spark.master local[*] spark.eventLog.enabled true spark.eventLog.dir file:///path/to/spark/eventLogDir ``` 5. 配置PyCharm以运行PySpark应用:在PyCharm中设置运行/调试配置,指定Python解释器、工作目录、环境变量等。对于PySpark应用,可能需要设置`PYSPARK_PYTHON`环境变量,指定Python解释器的路径。 6. 编写PySpark代码:在PyCharm中编写PySpark代码,使用PySpark的DataFrame API或其他功能进行数据分析和处理。 7. 运行和调试:使用PyCharm的运行按钮来执行PySpark应用,或使用调试功能进行代码调试。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值