SparkSession object has no attribute sqlContext
时间: 2023-08-01 08:09:34 浏览: 95
Starting from Spark 2.0, the entry point for Spark SQL is the SparkSession object instead of the SQLContext object. Therefore, if you try to access sqlContext attribute of SparkSession object, you will get the error "SparkSession object has no attribute sqlContext".
To use Spark SQL in Spark 2.0 or later versions, you should use the SparkSession object to create DataFrames, execute SQL queries, etc. Here is an example of how to create a SparkSession object and use it to create a DataFrame:
```
from pyspark.sql import SparkSession
# create a SparkSession object
spark = SparkSession.builder \
.appName("MyApp") \
.getOrCreate()
# create a DataFrame from a CSV file
df = spark.read.csv("my_file.csv", header=True, inferSchema=True)
# execute a SQL query
df.createOrReplaceTempView("my_table")
result = spark.sql("SELECT * FROM my_table WHERE age > 30")
# show the result
result.show()
```
Note that you should replace "MyApp" and "my_file.csv" with your own application name and file path, respectively.
阅读全文