Return Scala 中的临时 Spark SQL Table

Return Temporary Spark SQL Table in Scala

首先,我使用

将 CSV 文件转换为 Spark DataFrame
val df = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").load("/usr/people.csv")

在那之后输入 df 和 return 我可以看到

res30: org.apache.spark.sql.DataFrame = [name: string, age: string, gender: string, deptID: string, salary: string]

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

但在那之后当我 people 而不是输入 table 时,我得到了

<console>:33: error: not found: value people

难道是因为人是临时table?

谢谢

当您使用您使用的 registerTempTable 命令注册临时 table 时,它将在您的 SQLContext 中可用。

这意味着以下是不正确的,并且会给您带来您遇到的错误:

scala> people.show
<console>:33: error: not found: value people

要使用临时文件 table,您需要使用 sqlContext 调用它。示例:

scala> sqlContext.sql("select * from people")

注意: df.registerTempTable("df") 将注册一个临时的 table 名称 df 对应于您应用的 DataFrame df方法上。

因此坚持 df 不会坚持 table 但 DataFrame,甚至认为 SQLContext 将使用该 DataFrame。

上面的答案也适用于 Zeppelin。如果你想 运行 println 看数据,你必须把它发送回驱动程序看输出。

val querystrings = sqlContext.sql("select visitorDMA, 
        visitorIpAddress, visitorState, allRequestKV
    from {redacted} 
    limit 1000")

querystrings.collect.foreach(entry => {
    print(entry.getString(3).toString() + "\n")
})