2017-10-20 69 views
1

我有一个Dataframe,我试图展平。作为该过程的一部分,我想将其分解,所以如果我有一列数组,则将使用数组的每个值创建一个单独的行。我知道我可以使用爆炸函数。但是,我有一个问题,该列包含空值,我使用火花1.6。下面是数据类型和我想要什么的例子:
我的数据:Spark 1.6以空值爆炸

id | ListOfRficAction| RficActionAttachment 
_______________________________ 
1 | Luke   | [baseball, soccer] 
2 | Lucy   | null 

,我想

id | ListOfRficAction| RficActionAttachment 
_______________________________ 
1 | Luke   | baseball 
1 | Luke   | soccer 
2 | Lucy   | null 

我使用的Spark 1.6(所以我不能使用explode_outer功能),我尝试使用爆炸,但我有以下错误:

scala.MatchError: [null] (of class org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema) 

我也尝试:

df.withColumn("likes", explode(
    when(col("likes").isNotNull, col("likes")) 
    // If null explode an array<string> with a single null 
    .otherwise(array(lit(null).cast("string"))))) 

但我的DataFrame架构是一个很复杂(我有字符串和长),所以强制转换功能不起作用。这里是我的架构的一部分,我有错误:

|-- RficActionAttachment: array (nullable = true) 
| |-- element: struct (containsNull = true) 
| | |-- ActivityFileAutoUpdFlg: string (nullable = true) 
| | |-- ActivityFileDate: string (nullable = true) 
| | |-- ActivityFileDeferFlg: string (nullable = true) 
| | |-- ActivityFileDockReqFlg: string (nullable = true) 
| | |-- ActivityFileDockStatFlg: string (nullable = true) 
| | |-- ActivityFileExt: string (nullable = true) 
| | |-- ActivityFileName: string (nullable = true) 
| | |-- ActivityFileRev: string (nullable = true) 
| | |-- ActivityFileSize: long (nullable = true) 
| | |-- ActivityFileSrcPath: string (nullable = true) 
| | |-- ActivityFileSrcType: string (nullable = true) 
| | |-- ActivityId: string (nullable = true) 
| | |-- AttachmentId: string (nullable = true) 
| | |-- Comment: string (nullable = true) 

用户类抛出的异常:

org.apache.spark.sql.AnalysisException: cannot resolve 'CASE WHEN isnotnull(ListOfRficAction.RficAction.ListOfRficActionAttachment.RficActionAttachment) THEN ListOfRficAction.RficAction.ListOfRficActionAttachment.RficActionAttachment ELSE array(ListOfRficAction.RficAction.ListOfRficActionAttachment.RficActionAttachment)' 

由于数据类型不匹配:THEN和ELSE表情都应该是相同类型或强制转换到一种常见的类型;

想知道我能做些什么吗?

+0

我的问题是不同的,因为当我的模式 –

+0

问题是情况下,当我不能使用的情况下不适用于我 –

回答

1

首先全部替换null列中的值将为array(null),然后使用explode。在问题中使用例如数据框:

val df = Seq((1, "Luke", Array("baseball", "soccer")), (2, "Lucy", null)) 
    .toDF("id", "ListOfRficAction", "RficActionAttachment") 

df.withColumn("RficActionAttachment", 
    when($"RficActionAttachment".isNull, array(lit(null))) 
    .otherwise($"RficActionAttachment")) 
    .withColumn("RficActionAttachment", explode($"RficActionAttachment")) 

这会给请求的结果:

+---+----------------+--------------------+ 
| id|ListOfRficAction|RficActionAttachment| 
+---+----------------+--------------------+ 
| 1|   Luke|   baseball| 
| 1|   Luke|    soccer| 
| 2|   Lucy|    null| 
+---+----------------+--------------------+ 
+0

谢谢你@Shaido的回答,但正如我所说我尝试这一点,我仍然有同样的错误:无法解析'CASE WHEN isnull(ListOfRficAction.RficAction.ListOfRficActionAttachment.RficActionAttachment)THEN array(null)ELSE ListOfRficAction .RficAction.ListOfRficActionAttachment.RficActionAttachment'也许是由于我的数据框架模式 –

+0

@MbulaGuyMarcel数据框架架应该不重要,如果你有一个数组,上面应该可以工作。对代码做了一个小小的更新,你可以再试一次吗? – Shaido

+0

对不起,它不起作用 –