pyspark.sql.SQLContext#
- class pyspark.sql.SQLContext(sparkContext, sparkSession=None, jsqlContext=None)[source]#
The entry point for working with structured data (rows and columns) in Spark, in Spark 1.x.
As of Spark 2.0, this is replaced by
SparkSession. However, we are keeping the class here for backward compatibility.A SQLContext can be used to create
DataFrame, registerDataFrameas tables, execute SQL over tables, cache tables, and read parquet files.Deprecated since version 3.0.0: Use
SparkSession.builder.getOrCreate()instead.- Parameters:
- sparkContext
SparkContext The
SparkContextbacking this SQLContext.- sparkSession
SparkSession The
SparkSessionaround which this SQLContext wraps.- jsqlContextoptional
An optional JVM Scala SQLContext. If set, we do not instantiate a new SQLContext in the JVM, instead we make all calls to this object. This is only for internal.
- sparkContext
Examples
>>> from datetime import datetime >>> from pyspark.sql import Row >>> sqlContext = SQLContext(sc) >>> allTypes = sc.parallelize([Row(i=1, s="string", d=1.0, l=1, ... b=True, list=[1, 2, 3], dict={"s": 0}, row=Row(a=1), ... time=datetime(2014, 8, 1, 14, 1, 5))]) >>> df = allTypes.toDF() >>> df.createOrReplaceTempView("allTypes") >>> sqlContext.sql('select i+1, d+1, not b, list[1], dict["s"], time, row.a ' ... 'from allTypes where b and i > 0').collect() [Row((i + 1)=2, (d + 1)=2.0, (NOT b)=False, list[1]=2, dict[s]=0, time=datetime.datetime(2014, 8, 1, 14, 1, 5), a=1)] >>> df.rdd.map(lambda x: (x.i, x.s, x.d, x.l, x.b, x.time, x.row.a, x.list)).collect() [(1, 'string', 1.0, 1, True, datetime.datetime(2014, 8, 1, 14, 1, 5), 1, [1, 2, 3])]
Methods
cacheTable(tableName)Caches the specified table in-memory.
Removes all cached tables from the in-memory cache.
createDataFrame(data[, schema, ...])Creates a
DataFramefrom anRDD, a list, apandas.DataFrame, or apyarrow.Table.createExternalTable(tableName[, path, ...])Creates an external table based on the dataset in a data source.
dropTempTable(tableName)Remove the temporary table from catalog.
getConf(key[, defaultValue])Returns the value of Spark SQL configuration property for the given key.
getOrCreate([sc])Get the existing SQLContext or create a new one with given SparkContext.
Returns a new SQLContext as new session, that has separate SQLConf, registered temporary views and UDFs, but shared SparkContext and table cache.
range(start[, end, step, numPartitions])Create a
DataFramewith singlepyspark.sql.types.LongTypecolumn namedid, containing elements in a range fromstarttoend(exclusive) with step valuestep.registerDataFrameAsTable(df, tableName)Registers the given
DataFrameas a temporary table in the catalog.registerFunction(name, f[, returnType])An alias for
spark.udf.register().registerJavaFunction(name, javaClassName[, ...])An alias for
spark.udf.registerJavaFunction().setConf(key, value)Sets the given Spark SQL configuration property.
sql(sqlQuery)Returns a
DataFramerepresenting the result of the given query.table(tableName)Returns the specified table or view as a
DataFrame.tableNames([dbName])Returns a list of names of tables in the database
dbName.tables([dbName])Returns a
DataFramecontaining names of tables in the given database.uncacheTable(tableName)Removes the specified table from the in-memory cache.
Attributes
Returns a
DataFrameReaderthat can be used to read data in as aDataFrame.Returns a
DataStreamReaderthat can be used to read data streams as a streamingDataFrame.Returns a
StreamingQueryManagerthat allows managing all theStreamingQueryStreamingQueries active on this context.Returns a
UDFRegistrationfor UDF registration.Returns a
UDTFRegistrationfor UDTF registration.