Jau*_*mer 6 scala explode dataframe apache-spark-sql
我在Scala中使用spark库.我已经创建了一个DataFrame
val searchArr = Array(
StructField("log",IntegerType,true),
StructField("user", StructType(Array(
StructField("date",StringType,true),
StructField("ua",StringType,true),
StructField("ui",LongType,true))),true),
StructField("what",StructType(Array(
StructField("q1",ArrayType(IntegerType, true),true),
StructField("q2",ArrayType(IntegerType, true),true),
StructField("sid",StringType,true),
StructField("url",StringType,true))),true),
StructField("where",StructType(Array(
StructField("o1",IntegerType,true),
StructField("o2",IntegerType,true))),true)
)
val searchSt = new StructType(searchArr)
val searchData = sqlContext.jsonFile(searchPath, searchSt)
Run Code Online (Sandbox Code Playgroud)
我现在要爆炸什么东西what.q1,它应该包含一个整数数组,但文档是有限的:http://spark.apache.org/docs/1.4.0/api/java/org/apache/ 火花/ SQL/DataFrame.html#爆炸(java.lang.String中,%20java.lang.String,%20scala.Function1,%20scala.reflect.api.TypeTags.TypeTag)
到目前为止,我尝试了一些没有太多运气的东西
val searchSplit = searchData.explode("q1", "rb")(q1 => q1.getList[Int](0).toArray())
Run Code Online (Sandbox Code Playgroud)
有关如何使用爆炸的任何想法/示例?
您是否尝试过在“什么”字段上使用 UDF ?类似的东西可能有用:
val explode = udf {
(aStr: GenericRowWithSchema) =>
aStr match {
case null => ""
case _ => aStr.getList(0).get(0).toString()
}
}
val newDF = df.withColumn("newColumn", explode(col("what")))
Run Code Online (Sandbox Code Playgroud)
在哪里:
我不确定,但您可以尝试使用getAs[T](fieldName: String)而不是getList(index: Int)。