我有以下scala代码可从Spark提取数据:
import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.types.{StringType, StructType, TimestampType}
import org.apache.spark.sql.{DataFrame, Row, SQLContext}
import org.apache.spark.sql.functions._
val emailDF = loadTable("email")
.where(s"topic = '${Topics.Email}'")
.cache()
val df = emailDF.withColumn("rank",row_number()
.over(Window.partitionBy("email_address")
.orderBy(desc("created_at"))))
val resultDf = df.filter(s"rank == 1").drop("rank")
运行代码时出现此错误:
org.apache.spark.sql.AnalysisException: Could not resolve window function 'row_number'. Note that, using window functions currently requires a HiveContext;
四处搜寻,发现我需要添加hive依赖关系,这是我更新后的依赖关系:
build.sbt
val sparkVersion = "1.6.3"
libraryDependencies ++= Seq(
"org.apache.spark" %% "spark-sql" % sparkVersion % "provided",
"org.apache.spark" %% "spark-hive" % sparkVersion % "provided"
)
但是我仍然遇到同样的错误。
尝试了hiveContext方法:
val emailDF = Email.load()
.filter(col(Email.TopicId).isin(Topics.Email))
.filter(col(Email.OptIn).isin(optInFlag))
.cache()
val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc)
logger.info(s"sc: ${sc.appName}, ${sc.sparkUser}")
emailDF.registerTempTable("emailDFTable")
val df = hiveContext.sql("""SELECT *,
row_number() over(partition by email_address order by event_at desc) AS rank
FROM emailDFTable""")
val resultDf = df.filter(s"rank == 1").drop("rank")
现在我得到了错误:
Exception in thread "main" org.apache.spark.sql.AnalysisException: Table not found: emailDFTable; line 3 pos 30
at org.apache.spark.sql.catalyst.analysis.package$AnalysisErrorAt.failAnalysis(package.scala:42)
at org.apache.spark.sql.catalyst.analysis.Analyzer$ResolveRelations$.getTable(Analyzer.scala:305)
我尝试过的另一种方法:
val windowSpec = Window.partitionBy(col(EmailChannel.EmailAddress)).orderBy(col(EmailChannel.EventAt).desc)
val resultDf = emailDF.withColumn("maxEventAt", first("event_at").over(windowSpec))
.select("*").where(col("maxEventAt") === col(EmailChannel.EventAt))
.drop("maxEventAt")
然后再次出现类似错误:
org.apache.spark.sql.AnalysisException: Could not resolve window function 'first_value'. Note that, using window functions currently requires a HiveContext;
我真的不明白我已经导入了hiveContext并添加了spark-hive依赖性,为什么它不起作用。我能想到的一件事是我们使用datastax spark,因此build.sbt中存在以下缺陷
"com.datastax.spark" %% "spark-cassandra-connector" % "1.6.11",
我也需要datastax.spark.hive吗?但看不到存在这样的库。
我也显示我的emailDF:emailDF.show(false) 它里面有很多数据,不是空的。
====更新====
是的,切换到HiveContext是可行的,我没有注意到在代码开头已初始化了SparkContext和SQLContext,而不是用HiveContext切换SQLContext,而是尝试从SparkContext创建新的HiveContext:
val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc)
这就是为什么它不起作用。在将SQLContext更改为HiveContext之后,它可以正常工作。
从
更改 implicit val sc: SparkContext = new SparkContext(sparkConfig)
implicit val sqlContext: SQLContext = new SQLContext(sc)
到
implicit val sc: SparkContext = new SparkContext(sparkConfig)
implicit val sqlContext: HiveContext = new HiveContext(sc)
答案 0 :(得分:1)
Spark 1.6 Windowing函数中的
仅适用于HiveContext。
使用sparkContext(sc)创建hiveContext。
import org.apache.spark.sql.hive.HiveContext
val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc)
将数据帧注册为临时表,并使用hiveContext在临时表上运行查询。
emailDF.registerTempTable("emailDFTable")
一旦数据帧被注册为临时表,请检查您的临时表。
hiveContext.sql("SHOW tables").show()
+--------+------------+-----------+
|database| tableName|isTemporary|
+--------+------------+-----------+
| |emaildftable| true|
+--------+------------+-----------+
现在您可以查询临时表。
val df = hiveContext.sql("""SELECT *,
row_number() over(partition by email_address order by created_at desc) AS rank
FROM emailDFTable""")
让我知道怎么回事。