registerTempTableΒΆ

registerTempTable registers a SparkDataFrame as a Temporary Table in the SQLContext. You can execute SQL queries on these temporary tables.

Note:

Temporary Tables do not persist across clusters and cluster restarts.

Syntax:

  • registerTempTable(SparkDataFrame, tableName)

Parameters:

  • SparkDataFrame: Any SparkDataFrame
  • tableName: String, desired table name

Output:

  • Spark SQL Table
require(SparkR)

# Create SparkDataFrame from local R data frame
df <- createDataFrame(mtcars)
head(df)
# Register df as Temporary Table, with table name: tempTable
registerTempTable(df, "tempTable")

# Perform SQL queries on temporary table
head(sql("SELECT mpg, wt, vs FROM tempTable WHERE vs = 0"))

Note that the tempTable that you’ve created cannot be accessed like a variable. It is best to access it with sql(sqlQuery).

# Temp tables cannot be accessed like variables
tempTable
# View created tables
# column isTemporary indicates if table is temporary or not
head(sql("SHOW tables"))