是否可以使用SparkSQL进行更新?

时间:2016-07-23 09:54:05

标签: apache-spark apache-spark-sql

我有一个Data Frame,我是用镶木地板文件创建的。

val file = "/user/spark/pagecounts-20160713-150000.parquet"

val df = sqlContext.read.parquet(file)
df.registerTempTable("wikipedia")

现在我想做一个更新:

// just a dummy update statement    
val sqlDF = sqlContext.sql("update wikipedia set requests=0 where article='!K7_Records'") 

但是我收到了一个错误:

  

java.lang.RuntimeException:[1.1]失败:``with''预期但是   发现标识符更新

update wikipediaEnTemp set requests=0 where article='!K7_Records'
^
  at scala.sys.package$.error(package.scala:27)
  at org.apache.spark.sql.catalyst.AbstractSparkSQLParser.parse(AbstractSparkSQLParser.scala:36)
  at org.apache.spark.sql.catalyst.DefaultParserDialect.parse(ParserDialect.scala:67)
  at org.apache.spark.sql.SQLContext$$anonfun$2.apply(SQLContext.scala:211)
  at org.apache.spark.sql.SQLContext$$anonfun$2.apply(SQLContext.scala:211)
  at org.apache.spark.sql.execution.SparkSQLParser$$anonfun$org$apache$spark$sql$execution$SparkSQLParser$$others$1.apply(SparkSQLParser.scala:114)
  at org.apache.spark.sql.execution.SparkSQLParser$$anonfun$org$apache$spark$sql$execution$SparkSQLParser$$others$1.apply(SparkSQLParser.scala:113)
  at scala.util.parsing.combinator.Parsers$Success.map(Parsers.scala:137)
  at scala.util.parsing.combinator.Parsers$Success.map(Parsers.scala:136)
  at scala.util.parsing.combinator.Parsers$Parser$$anonfun$map$1.apply(Parsers.scala:237)
  at scala.util.parsing.combinator.Parsers$Parser$$anonfun$map$1.apply(Parsers.scala:237)
  at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:217)
  at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append$1$$anonfun$apply$2.apply(Parsers.scala:249)
  at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append$1$$anonfun$apply$2.apply(Parsers.scala:249)
  at scala.util.parsing.combinator.Parsers$Failure.append(Parsers.scala:197)
  at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append$1.apply(Parsers.scala:249)
  at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append$1.apply(Parsers.scala:249)
  at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:217)
  at scala.util.parsing.combinator.Parsers$$anon$2$$anonfun$apply$14.apply(Parsers.scala:882)
  at scala.util.parsing.combinator.Parsers$$anon$2$$anonfun$apply$14.apply(Parsers.scala:882)
  at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
  at scala.util.parsing.combinator.Parsers$$anon$2.apply(Parsers.scala:881)
  at scala.util.parsing.combinator.PackratParsers$$anon$1.apply(PackratParsers.scala:110)
  at org.apache.spark.sql.catalyst.AbstractSparkSQLParser.parse(AbstractSparkSQLParser.scala:34)
  at org.apache.spark.sql.SQLContext$$anonfun$1.apply(SQLContext.scala:208)
  at org.apache.spark.sql.SQLContext$$anonfun$1.apply(SQLContext.scala:208)
  at org.apache.spark.sql.execution.datasources.DDLParser.parse(DDLParser.scala:43)
  at org.apache.spark.sql.SQLContext.parseSql(SQLContext.scala:231)
  at org.apache.spark.sql.SQLContext.sql(SQLContext.scala:817)
  ... 57 elided

2 个答案:

答案 0 :(得分:2)

RDD和Dataframe是不可变的,因为底层数据是不可变的。所以DML选项不包含在sparkSQL中。

答案 1 :(得分:1)

Spark表是不可变的,因此无法进行直接更新。但是,如果您可以更改架构和查询,则可以使用仅附加操作执行等效的更新。一般问题在数据仓库社区中称为Type II Slowly Changing Dimension。这有一个Spark package,我还没有合作过。