搭建环境

时间:2019-02-04 20:33:26

标签: google-colaboratory

在运行设置环境时,出于某种原因,我正在使用Google合作实验室来了解Pyspark。我收到一条错误消息,似乎是从一本笔记本移动到另一本笔记本时发生的。

错误消息:     IndexError Traceback(最近的调用     持续)      在()中       4       5导入findspark      ----> 6个findspark.init()       7从pyspark导入SparkContext       8 sc = SparkContext.getOrCreate()

 /usr/local/lib/python3.6/dist-packages/findspark.py in init(spark_home, 
 python_path, edit_rc, edit_profile)
133     # add pyspark to sys.path
134     spark_python = os.path.join(spark_home, 'python')
 --> 135     py4j = glob(os.path.join(spark_python, 'lib', 'py4j-*.zip')) 
[0]
136     sys.path[:0] = [spark_python, py4j]
137 

IndexError: list index out of range

Google提供的用于设置环境的代码

 import os
 os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-8-openjdk-amd64"
 os.environ["SPARK_HOME"] = "/content/spark-2.3.1-bin-hadoop2.7"

 import findspark
 findspark.init()
 from pyspark import SparkContext
 sc = SparkContext.getOrCreate()

import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate() 
spark

1 个答案:

答案 0 :(得分:0)

在运行上面的代码片段之前,您需要安装spark。这是经过调整的食谱,可以在新的Colab后端上为我工作:

!apt-get install openjdk-8-jdk-headless -qq > /dev/null
!wget -q http://apache.osuosl.org/spark/spark-2.3.2/spark-2.3.2-bin-hadoop2.7.tgz
!tar xf spark-2.3.2-bin-hadoop2.7.tgz
!pip install -q findspark

import os
import findspark
findspark.init('/content/spark-2.3.2-bin-hadoop2.7/')
from pyspark import SparkContext
sc = SparkContext.getOrCreate()

import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate() 
spark