2016-09-29 62 views
0

我有一个Spark斯卡拉数据框与嵌套结构:重命名Scala中嵌套元素星火据帧

|-- _History: struct (nullable = true) 
| |-- Article: array (nullable = true) 
| | |-- element: struct (containsNull = true) 
| | | |-- Id: string (nullable = true) 
| | | |-- Timestamp: long (nullable = true) 
| |-- Channel: struct (nullable = true) 
| | |-- <font><font>Cultura pop</font></font>: array (nullable = true) 
| | | |-- element: long (containsNull = true) 
| | |-- <font><font>Deportes</font></font>: array (nullable = true) 
| | | |-- element: long (containsNull = true) 

我试图重新命名嵌套元素(如<font><font>Deportes</font></font>Deportes有没有办法做到这一点使用UDF或类似的东西

我试过以下,它不工作:?

var filterDF2 = filterDF 
    .withColumnRenamed("_History.Channel.<font><font>Deportes</font></font>", "_History.Channel.Deportes") 

回答

3

最简单的方法是使用类型转换与正确命名的模式字符串(或同等StructField定义)荷兰国际集团:

val schema = """struct< 
    Article: array<struct<Id:string,Timestamp:bigint>>, 
    Channel: struct<Cultura: bigint, Deportes: array<bigint>>>""" 
df.withColumn("_History", $"_History".cast(schema)) 

您也可以与case类模拟这种:

import org.apache.spark.sql.Row 

case class ChannelRecord(Cultura: Option[Long], Deoprtes: Option[Seq[Long]]) 

val rename = udf((row: Row) => 
    ChannelRecord(Option(row.getLong(0)), Option(row.getSeq[Long](1)))) 

df.withColumn("_History", 
    struct($"_History.Article", rename($"_History.channel").alias("channel")))