sparkDataframe生成

hs1rzwqc  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(393)

我有两个Dataframe: dataframe1 有70000行,如:

location_id, location, flag
1,Canada,active
2,Paris,active
3,London,active
4,Berlin,active

第二测向 lookup 已修改每个位置的ID(此Dataframe会不时修改),例如:

id,location
1,Canada
10,Paris
4,Berlin
3,London

我的问题是,我需要把新的身份证作为 lookup 如果 location_id 不同于 id 然后,对于每个位置,保持标志名称为非活动的对应位置的旧id(以维护历史数据)和标志名称为活动的新id。因此,配置单元中的输出表应该如下所示:

location_id,location,flag
1,Canada,active
2,Paris,inactive
10,Paris,active
3,London,active
4,Berlin,active

我试着先加入两个框架。然后在加入df时,我正在执行操作,以将所有记录保存在配置单元中。我尝试了以下操作:

val joinedFrame = dataframe1.join(lookup, "location")
val df_temp = joinedFrame.withColumn("flag1", when($"tag_id" === $"tag_number", "active").otherwise("inactive"))
var count = 1
df_temp.foreach(x => {
  val flag1 = x.getAs[String]("flag1").toString
  val flag = x.getAs[String]("flag").toString
  val location_id = x.getAs[String]("location_id").toString
  val location = x.getAs[String]("location").toString
  val id = x.getAs[String]("id").toString
  if ((count != 1)&&(flag1 != flag)){
    println("------not equal-------",flag1,"-------",flag,"---------",id,"---------",location,"--------",location_id)
    val df_main = sc.parallelize(Seq((location_id, location,flag1), (id, location, flag))).toDF("location_id", "location", "flag")
    df_main.show
    df_main.write.insertInto("location_coords")
  }
  count += 1
})

它打印具有不同ID的位置值,但在将这些值保存为Dataframe时,我遇到了一个异常:

not equal------inactive------active---10---------Paris---------2    
17/09/29 03:43:29 ERROR Executor: Exception in task 0.0 in stage 25.0 (TID 45)
    java.lang.NullPointerException
            at $line83.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:75)
            at $line83.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:65)
            at scala.collection.Iterator$class.foreach(Iterator.scala:893)
            at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
            at org.apache.spark.rdd.RDD$$anonfun$foreach$1$$anonfun$apply$28.apply(RDD.scala:918)
            at org.apache.spark.rdd.RDD$$anonfun$foreach$1$$anonfun$apply$28.apply(RDD.scala:918)
            at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1951)
            at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1951)
            at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
            at org.apache.spark.scheduler.Task.run(Task.scala:99)
            at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:322)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
            at java.lang.Thread.run(Thread.java:748)
    17/09/29 03:43:29 WARN TaskSetManager: Lost task 0.0 in stage 25.0 (TID 45, localhost, executor driver): java.lang.NullPointerException
            at $line83.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:75)
            at $line83.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$1.apply(<console>:65)
            at scala.collection.Iterator$class.foreach(Iterator.scala:893)
            at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
            at org.apache.spark.rdd.RDD$$anonfun$foreach$1$$anonfun$apply$28.apply(RDD.scala:918)
            at org.apache.spark.rdd.RDD$$anonfun$foreach$1$$anonfun$apply$28.apply(RDD.scala:918)
            at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1951)
            at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1951)
            at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
            at org.apache.spark.scheduler.Task.run(Task.scala:99)
            at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:322)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
            at java.lang.Thread.run(Thread.java:748)
c7rzv4ha

c7rzv4ha1#

根据你的意见,我认为最简单的方法是 join 而是在身份证上。在执行外部联接时,缺少的列将以null结束,这些行是您感兴趣的已更新行。
在这之后,剩下的就是更新location列(如果它是空的)以及flag列,请参见下面的代码(注意,我稍微更改了列名):

val spark = SparkSession.builder.getOrCreate()
import spark.implicits._

val df = Seq((1,"Canada","active"),(2,"Paris","active"),(3,"London","active"),(4,"Berlin","active"))
  .toDF("id", "location", "flag")
val df2 = Seq((1,"Canada"),(10,"Paris"),(4,"Berlin"),(3,"London"))
  .toDF("id", "location_new") 

val df3 = df.join(df2, Seq("id"), "outer")
  .filter($"location".isNull or $"location_new".isNull)
  .withColumn("location", when($"location_new".isNull, $"location").otherwise($"location_new"))
  .withColumn("flag", when($"location" === $"location_new", "active").otherwise("inactive"))
  .drop("location_new")

> df3.show()
+---+--------+--------+
| id|location|    flag|
+---+--------+--------+
| 10|   Paris|  active|
|  2|   Paris|inactive|
+---+--------+--------+

在此之后,您可以使用这个新的Dataframe来更新配置单元表。

相关问题