配置 Spark 以与 Jupyter Notebook 和 Anaconda 一起使用 [英] Configuring Spark to work with Jupyter Notebook and Anaconda

查看:45
本文介绍了配置 Spark 以与 Jupyter Notebook 和 Anaconda 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几天时间试图让 Spark 与我的 Jupyter Notebook 和 Anaconda 一起工作.这是我的 .bash_profile 的样子:

I've spent a few days now trying to make Spark work with my Jupyter Notebook and Anaconda. Here's what my .bash_profile looks like:

PATH="/my/path/to/anaconda3/bin:$PATH"

export JAVA_HOME="/my/path/to/jdk"
export PYTHON_PATH="/my/path/to/anaconda3/bin/python"
export PYSPARK_PYTHON="/my/path/to/anaconda3/bin/python"

export PATH=$PATH:/my/path/to/spark-2.1.0-bin-hadoop2.7/bin
export PYSPARK_DRIVER_PYTHON=jupyter
export PYSPARK_DRIVER_PYTHON_OPTS="notebook" pyspark
export SPARK_HOME=/my/path/to/spark-2.1.0-bin-hadoop2.7
alias pyspark="pyspark --conf spark.local.dir=/home/puifais --num-executors 30 --driver-memory 128g --executor-memory 6g --packages com.databricks:spark-csv_2.11:1.5.0"

当我输入 /my/path/to/spark-2.1.0-bin-hadoop2.7/bin/spark-shell 时,我可以在命令行 shell 中很好地启动 Spark.并且输出 sc 不为空.似乎工作正常.

When I type /my/path/to/spark-2.1.0-bin-hadoop2.7/bin/spark-shell, I can launch Spark just fine in my command line shell. And the output sc is not empty. It seems to work fine.

当我输入 pyspark 时,它可以正常启动我的 Jupyter Notebook.当我创建一个新的 Python3 notebook 时,出现这个错误:

When I type pyspark, it launches my Jupyter Notebook fine. When I create a new Python3 notebook, this error appears:

[IPKernelApp] WARNING | Unknown error in handling PYTHONSTARTUP file /my/path/to/spark-2.1.0-bin-hadoop2.7/python/pyspark/shell.py: 

而且我的 Jupyter Notebook 中的 sc 是空的.

And sc in my Jupyter Notebook is empty.

谁能帮忙解决这个问题?

Can anyone help solve this situation?

只是想澄清一下:错误末尾的冒号后没有任何内容.我还尝试使用此帖子创建自己的启动文件,我在这里引用,这样您就不必去了看那里:

Just want to clarify: There is nothing after the colon at the end of the error. I also tried to create my own start-up file using this post and I quote here so you don't have to go look there:

我创建了一个简短的初始化脚本 init_spark.py 如下:

I created a short initialization script init_spark.py as follows:

from pyspark import SparkConf, SparkContext
conf = SparkConf().setMaster("yarn-client")
sc = SparkContext(conf = conf)

并将其放在 ~/.ipython/profile_default/startup/目录中

and placed it in the ~/.ipython/profile_default/startup/ directory

当我这样做时,错误变成了:

When I did this, the error then became:

[IPKernelApp] WARNING | Unknown error in handling PYTHONSTARTUP file /my/path/to/spark-2.1.0-bin-hadoop2.7/python/pyspark/shell.py:
[IPKernelApp] WARNING | Unknown error in handling startup files:

推荐答案

Conda 可以帮助正确管理很多依赖项...

Conda can help correctly manage a lot of dependencies...

安装火花.假设 spark 安装在/opt/spark 中,请将其包含在您的 ~/.bashrc 中:

Install spark. Assuming spark is installed in /opt/spark, include this in your ~/.bashrc:

export SPARK_HOME=/opt/spark
export PATH=$SPARK_HOME/bin:$PATH

创建一个 conda 环境,其中包含除 spark 之外的所有需要​​的依赖项:

Create a conda environment with all needed dependencies apart from spark:

conda create -n findspark-jupyter-openjdk8-py3 -c conda-forge python=3.5 jupyter=1.0 notebook=5.0 openjdk=8.0.144 findspark=1.1.0

激活环境

$ source activate findspark-jupyter-openjdk8-py3

启动 Jupyter Notebook 服务器:

Launch a Jupyter Notebook server:

$ jupyter notebook

在浏览器中,创建一个新的 Python3 notebook

In your browser, create a new Python3 notebook

尝试使用以下脚本计算 PI(借自 this)

Try calculating PI with the following script (borrowed from this)

import findspark
findspark.init()
import pyspark
import random
sc = pyspark.SparkContext(appName="Pi")
num_samples = 100000000
def inside(p):     
  x, y = random.random(), random.random()
  return x*x + y*y < 1
count = sc.parallelize(range(0, num_samples)).filter(inside).count()
pi = 4 * count / num_samples
print(pi)
sc.stop()

这篇关于配置 Spark 以与 Jupyter Notebook 和 Anaconda 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆