在Scala中返回临时Spark SQL表

Gav*_*Niu 3 scala apache-spark apache-spark-sql

首先,我使用将CSV文件转换为Spark DataFrame

val df = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").load("/usr/people.csv")
Run Code Online (Sandbox Code Playgroud)

在那种类型的df和返回后我可以看到

res30: org.apache.spark.sql.DataFrame = [name: string, age: string, gender: string, deptID: string, salary: string]
Run Code Online (Sandbox Code Playgroud)

然后我df.registerTempTable("people")用来将df转换为Spark SQL表.

但在那之后,当我做了people相反得到了类型表,我得到了

<console>:33: error: not found: value people
Run Code Online (Sandbox Code Playgroud)

是因为人们是临时桌子吗?

谢谢

eli*_*sah 7

使用您使用的registerTempTable命令注册临时表时,它将在SQLContext中可用.

这意味着以下内容不正确,将为您提供错误:

scala> people.show
<console>:33: error: not found: value people
Run Code Online (Sandbox Code Playgroud)

要使用临时表,您需要使用sqlContext调用它.示例:

scala> sqlContext.sql("select * from people")
Run Code Online (Sandbox Code Playgroud)

注意: df.registerTempTable("df")将注册一个临时表,其名称df对应于df您应用该方法的DataFrame .

所以坚持df不会坚持表,但DataFrame,甚至认为SQLContext将使用该DataFrame.