如何在 Jupyter 中的一个单元格中创建多个临时表?

How to create multiple temporary tables in one cell in Jupyter?

我在 Azure (HDInsight) 上使用 Spark 2.1 和 Jupyter notebook。

我无法在一个笔记本单元格中创建多个表格。

以下查询工作正常:

%%sql

create table if not exists temp1(Col varchar(32))

returns: 没有结果。

以下查询在单个单元格中不起作用:

%%sql

create table if not exists temp2(Col varchar(32))
create table if not exists temp3(Col varchar(32))

An error was encountered: org.apache.spark.sql.catalyst.parser.ParseException: mismatched input 'create' expecting (line 3, pos 0)

== SQL ==

create table if not exists temp2(Col varchar(32)) create table if not exists temp3(Col varchar(32)) ^^^

at org.apache.spark.sql.catalyst.parser.ParseException.withCommand(ParseDriver.scala:197) at org.apache.spark.sql.catalyst.parser.AbstractSqlParser.parse(ParseDriver.scala:99) at org.apache.spark.sql.execution.SparkSqlParser.parse(SparkSqlParser.scala:45) at org.apache.spark.sql.catalyst.parser.AbstractSqlParser.parsePlan(ParseDriver.scala:53) at org.apache.spark.sql.SparkSession.sql(SparkSession.scala:592) ... 47 elided

如何在一个单元格中创建多个表格?

似乎 Jupyter 中带有 sql 解释器的单个单元对应于 Spark 执行的单个 SQL 查询。

一种可能的解决方法是使用 spark 解释器(或 scala)并在 spark.sql 中执行 SQL 语句,如下所示:

%%spark

spark.sql("create table if not exists temp2(Col varchar(32))")
spark.sql("create table if not exists temp3(Col varchar(32))")

那是应该工作。