为什么spark告诉我"name'sqlContext'未定义",我如何使用sqlContext?

Liu*_*ong 6 apache-spark apache-spark-sql spark-dataframe

我尝试运行spark-ml的例子,但是

from pyspark import SparkContext
import pyspark.sql 

sc = SparkContext(appName="PythonStreamingQueueStream")    
training = sqlContext.createDataFrame([
(1.0, Vectors.dense([0.0, 1.1, 0.1])),
(0.0, Vectors.dense([2.0, 1.0, -1.0])),
(0.0, Vectors.dense([2.0, 1.3, 1.0])),
(1.0, Vectors.dense([0.0, 1.2, -0.5]))], ["label", "features"])
Run Code Online (Sandbox Code Playgroud)

无法运行,因为终端告诉我

NameError: name 'SQLContext' is not defined
Run Code Online (Sandbox Code Playgroud)

为什么会这样?我该如何解决?

Den*_*Lee 14

如果您正在使用Apache Spark 1.x系列(即在Apache Spark 2.0之前),要访问它sqlContext,您需要导入sqlContext; 即

from pyspark.sql import SQLContext
sqlContext = SQLContext(sc)
Run Code Online (Sandbox Code Playgroud)

如果你正在使用Apache Spark 2.0,你可以Spark Session直接使用它.因此,您的代码将是

training = spark.createDataFrame(...)
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅Spark SQL编程指南.