Pyspark: NameError: name "spark" not defined

I copy the pyspark.ml example from the official website of the document: http://spark.apache.org/docs/latest/api/python/pyspark.ml.html#pyspark.ml.Transformer

data = [(Vectors.dense([0.0, 0.0]),), (Vectors.dense([1.0, 1.0]),),(Vectors.dense([9.0, 8.0]),), (Vectors.dense([8.0, 9.0]),)]
df = spark.createDataFrame(data, ["features"])
kmeans = KMeans(k=2, seed=1)
model = kmeans.fit(df)

However, the above example will not work and would give me the following errors:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-28-aaffcd1239c9> in <module>()
      1 from pyspark import *
      2 data = [(Vectors.dense([0.0, 0.0]),), (Vectors.dense([1.0, 1.0]),),(Vectors.dense([9.0, 8.0]),), (Vectors.dense([8.0, 9.0]),)]
----> 3 df = spark.createDataFrame(data, ["features"])
      4 kmeans = KMeans(k=2, seed=1)
      5 model = kmeans.fit(df)

NameError: name 'spark' is not defined

What additional configuration / variable needs to be configured to run the example?

+4
source share
2 answers

Since you are calling createDataFrame () , you need to do this:

df = sqlContext.createDataFrame(data, ["features"])

instead of this:

df = spark.createDataFrame(data, ["features"])

sparkstanding there like sqlContext.


, sc, , , :

df = sc.createDataFrame(data, ["features"])
+8

from pyspark.context import SparkContext
from pyspark.sql.session import SparkSession
sc = SparkContext('local')
spark = SparkSession(sc)

SparkSession, spark.createDataFrame() .

+6

Source: https://habr.com/ru/post/1654872/


All Articles