Spark中是否可以插入临时表?

heu*_*g77 6 temporary apache-spark

我使用 Databricks 和 Apache Spark 2.4 测试了以下查询:

%sql

<step1>
create temporary view temp_view_t
as select 1 as no, 'aaa' as str;

<step2>
insert into temp_view_t values (2,'bbb');
Run Code Online (Sandbox Code Playgroud)

然后我收到此错误消息。

SQL 语句错误: AnalysisException:不允许插入基于 RDD 的表。;; 'InsertIntoTable 项目 [1 AS no#824,aaa AS str#825],false,false +- LocalRelation [col1#831,col2#832]

我的问题是

  1. Spark中无法插入临时表吗?
  2. 如何在 Spark sql 中创建临时数据?

谢谢。

Shu*_*Shu 2

我们将数据插入临时表,但我们可以用(or)can't模拟插入(以删除重复项)。union allunion

Example:

#create temp view
spark.sql("""create or replace temporary view temp_view_t as select 1 as no, 'aaa' as str""")

spark.sql("select * from temp_view_t").show()
#+---+---+
#| no|str|
#+---+---+
#|  1|aaa|
#+---+---+

#union all with the new data
spark.sql("""create or replace temporary view temp_view_t as select * from temp_view_t union all select 2 as no, 'bbb' as str""")

spark.sql("select * from temp_view_t").show()                                                                     
#+---+---+
#| no|str|
#+---+---+
#|  1|aaa|
#|  2|bbb|
#+---+---+

#to eliminate duplicates we can use union also. 
spark.sql("""create or replace temporary view temp_view_t as select * from temp_view_t union select 1 as no, 'aaa' as str""")

spark.sql("select * from temp_view_t").show()
#+---+---+
#| no|str|
#+---+---+
#|  1|aaa|
#|  2|bbb|
#+---+---+
Run Code Online (Sandbox Code Playgroud)