2017-09-29 62 views
0

我有2个数据帧: dataframe1具有70000行,如:创建火花数据帧基于条件

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

对于每个位置二DF lookup已修改IDS(此数据帧被修改的时间到时间),像:

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

我的问题是,我需要从lookup LOCATION_ID新的ID,如果location_idid不同的,那么,保持CORRE的老ID标志名称为非活动状态(维护历史数据)的新位置和标志名称为每个位置活动的新标识。因此,蜂巢中的输出表应该如下所示:

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

我试图先加入两个帧。然后在加入DF,我执行行动,保存在hive.I尝试操作的所有记录:

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的位置值,但同时节省了这些值数据框中,我获得例外:

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) 
+1

我不认为你已经用'foreach'循环了一个数据框时就可以使用'sc.parallelize'。 – Shaido

+0

@Shaido什么应该是可能的选择呢。谢谢。 – Swati

+0

你只想保存id已经改变的那些行吗? – Shaido

回答

1

根据您的意见,我认为最简单的方法是使用ids代替join。在进行外连接时,缺失的列最终会为空,这些行是已更新并且您感兴趣的行。

之后,剩下的全部内容是更新位置列,以防万一它为空还有标志栏,看到我的代码如下(请注意,我有所改变的列名):

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| 
+---+--------+--------+ 

在此之后,你可以使用这个新的数据框更新蜂巢表。