我在Zeppelin 0.7
下运行代码%spark
//val sc: SparkContext // An existing SparkContext.
sc
import sqlContext.implicits._
import org.apache.spark.sql._
import org.apache.spark.sql.DataFrame;
import org.apache.spark.sql.SQLContext;
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
val people = sqlContext.jsonFile("/Users/asdf/Desktop/people.json")
people.printSchema()
people.show()
people.select("name").show()
people.toDF().registerAsTable("people")
它工作到people.select("name").show()
但在最后一行抛出错误,下面是错误:
+-------+
| name|
+-------+
|Michael|
| Andy|
| Justin|
+-------+
<console>:230: error: value registerAsTable is not a member of org.apache.spark.sql.DataFrame
people.toDF().registerAsTable("people")
asper我的知识我导入了所有必需的并将其转换为df,然后将其作为表注册。我在这里失踪了什么?
答案 0 :(得分:1)
在“people.select(”name“)行之后添加以下两行.show()”
people.toDF().createOrReplaceTempView("people")
sqlContext.sql("SELECT * from people").show()
虽然启动期间火花版本中会显示spark版本,但您可以使用以下命令检查版本。
sc.version
答案 1 :(得分:0)
下面的代码对我有用,问题在于Zeppelin我们不应该创建一个SQLContext Zeppelin提供它自己的。
%spark
import sqlContext.implicits._
import org.apache.spark.sql._
import org.apache.spark.sql.DataFrame;
import org.apache.spark.sql.SQLContext;
val people = sqlContext.jsonFile("/Users/asdf/Desktop/people.json")
people.printSchema()
people.show()
people.select("name").show()
people.toDF().registerTempTable("people")
然后在上面创建的Temp表可用于查询Zeppelin的%sql
笔记本中的交互式图表