Spark + Python - 如何设置系统环境变量?

时间:2015-08-04 09:47:43

标签: python apache-spark

我在spark-1.4.1上。如何为Python设置系统环境变量?

例如,在R中,

Sys.setenv(SPARK_HOME = "C:/Apache/spark-1.4.1")
.libPaths(c(file.path(Sys.getenv("SPARK_HOME"), "R", "lib"), .libPaths()))

在Python中怎么样?

import os
import sys

from pyspark.sql import SQLContext

sc = SparkContext(appName="PythonSQL")
sqlContext = SQLContext(sc)

# Set the system environment variables.
# ref: https://github.com/apache/spark/blob/master/examples/src/main/python/sql.py
if len(sys.argv) < 2:
    path = "file://" + \
        os.path.join(os.environ['SPARK_HOME'], "examples/src/main/resources/people.json")
else:
    path = sys.argv[1]

# Create the DataFrame
df = sqlContext.jsonFile(path)

# Show the content of the DataFrame
df.show()

我收到此错误,

未定义df。

enter image description here

有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

试试这样:https://spark.apache.org/docs/latest/sql-programming-guide.html#creating-dataframes

df = sqlContext.jsonFile(path)作为参数提供给if len(sys.argv) < 2:

如果您在运行python脚本时没有提供参数,那么它将进入SPARK_HOME,这要求您将3,3,2,1 定义为系统变量。如果没有,它将无法找到您指定的.json文件。这似乎是你的问题。