在Scala中返回临时Spark SQL表

时间:2015-11-06 16:59:02

标签: 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")

之后输入df并返回我可以看到

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

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

但在那之后我做people而是获得了类型表,我得到了

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

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

由于

2 个答案:

答案 0 :(得分:7)

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

这意味着以下内容不正确,并会显示您收到的错误:

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

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

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

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

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

答案 1 :(得分:0)

以上答案也适用于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")
})