registerTempTable

registerTempTable registers a DataFrame 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(DataFrame, tableName)

Parameters:

  • DataFrame: Any SparkR DataFrame
  • tableName: String, desired table name

Output:

  • Spark SQL Table
# Create SparkR DataFrame from local R data frame
df <- createDataFrame(sqlContext, mtcars)
head(df)
# Register df as Temporary Table, with table name: tempTable
registerTempTable(df, "tempTable")

# Perform SQL queries on temporary table
head(sql(sqlContext, "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(sqlContext, sqlQuery).

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