Dataframe Spark Hive:用另一个数据帧的值过滤一个数据帧的行';s柱
我有以下两个Dataframe Spark Hive:用另一个数据帧的值过滤一个数据帧的行';s柱,dataframe,spark-dataframe,hiveql,spark-hive,Dataframe,Spark Dataframe,Hiveql,Spark Hive,我有以下两个数据帧: DataFrame "dfPromotion": date | store =================== 2017-01-01 | 1 2017-01-02 | 1 DataFrame "dfOther": date | store =================== 2017-01-01 | 1 2017-01-03 | 1 稍后,我需要对上面的两个数据帧进行union。但在此之前,我必须删除
数据帧
:
DataFrame "dfPromotion":
date | store
===================
2017-01-01 | 1
2017-01-02 | 1
DataFrame "dfOther":
date | store
===================
2017-01-01 | 1
2017-01-03 | 1
稍后,我需要对上面的两个数据帧进行union
。但在此之前,我必须删除dfOther
中包含date
值的所有行,该值也包含在dfPromotion
中
以下过滤步骤的结果应如下所示:
DataFrame "dfPromotion" (this stays always the same, must not be changed in this step!)
date | store
===================
2017-01-01 | 1
2017-01-02 | 1
DataFrame "dfOther" (first row is removed as dfPromotion contains the date 2017-01-01 in the "date" column)
date | store
===================
2017-01-03 | 1
有没有一种在Java中实现这一点的方法?我只找到了数据帧。除了前面的
方法,但这会检查数据帧的所有列。我需要只按日期列过滤第二个数据帧,因为以后可以添加其他列,这些列可能包含不同的值
调用dfOther.filter(dfOther.col(“日期”).isin(dfPromotion.col(“日期”))
会引发以下异常:
Exception in thread "main" org.apache.spark.sql.AnalysisException: resolved attribute(s) date#64 missing from date#0,store#13 in operator !Filter date#0 IN (date#64);
您可以使用减法函数
dfOther.select("date").except(dfPromotion.select("date")).join(dfOther,'date').show()
既然您提到了Spark Hive,您可以尝试下面的Spark sql方法吗
val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc);
val dfpromotion = sqlContext.sql("select * from dfpromotion");
dfpromotion.show
+----------+-----+
| dt|store|
+----------+-----+
|2017-01-01| 1|
|2017-01-02| 1|
+----------+-----+
val dfother = sqlContext.sql("select * from dfother");
dfother.show
+----------+-----+
| dt|store|
+----------+-----+
|2017-01-01| 1|
|2017-01-03| 1|
+----------+-----+
val dfdiff = sqlContext.sql("select o.dt, o.store from dfpromotion p right outer join dfother o on p.dt = o.dt where p.dt is null");
val dfunion = dfpromotion.union(dfdiff);
scala> dfunion.show
+----------+-----+
| dt|store|
+----------+-----+
|2017-01-01| 1|
|2017-01-02| 1|
|2017-01-03| 1|
这是为我做的,非常感谢。现在我的最后一个代码是:dfPromotion.join(dfOther,dfPromotion.col(“date”).equalTo(dfOther.col(“date”),“right_outer”)。其中(dfPromotion.col(“date”).isNull()。选择(dfOther.col(“date”)、dfOther.col(“store”)
抱歉,但是Spark API中没有数据帧。subtract
方法:哦,抱歉。我在pyspark中使用了它,在JavaRDD中使用了减法。不知道它是为数据帧删除的。不管怎么说,我们有except函数,你可以在上面的代码行中使用它,而不是减法。它应该最有效。except
函数的要点是,我必须只按date
列过滤dfOther
数据帧。所以我不能使用这个方法,否则这将是最简单的方法。我相信我们可以,dfOther.select(“date”),它返回一个数据帧,其中只有date列,类似的还有dfPromotion.select(“date”)。因此,两个数据框之间的日期列将返回我们需要的结果。试试看,如果我错过了什么,请告诉我。这就是你做的,dfOther.select(“日期”)。除了(dfPromotion.select(“日期”)。join(dfOther,'date')。show()