我使用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');
然后我收到此错误消息。
SQL语句中的错误:AnalysisException:不允许插入基于RDD的表中。 'InsertIntoTable项目[1 AS No#824,aaa AS str#825],false,false +-LocalRelation [col1#831,col2#832]
我的问题是
谢谢。
答案 0 :(得分:0)
我们can't
将数据插入到临时表中,但是我们可以使用 union all
(或) union
(删除重复项。
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|
#+---+---+
答案 1 :(得分:0)
是的,您可以插入到临时视图中,但它必须基于文件中的 df 构建。然后新行将作为单独的文件保存在存储中。
例如
df.read.parquet(path).createOrReplaceTempView('temp')
spark.sql("INSERT INTO temp VALUES (....)")