简体   繁体   English

Spark:使用 UDF 在 Dataframe 中创建数组列

[英]Spark: Using a UDF to create an Array column in a Dataframe

I have a simple function that takes some XML in a field, parses the values, and returns a list:我有一个简单的 function,它在一个字段中使用一些 XML,解析值,并返回一个列表:

<data>
   <datas a="1" b="2" c="3">
   <datas a="2" b="3" c="2">
</data>

becomes a nested list [[1,2,3],[2,3,2]]成为嵌套列表 [[1,2,3],[2,3,2]]

I've made this a udf, and I'm making this call on my dataframe:我已将其设为 udf,我正在拨打我的 dataframe:

myudf=udf(myparser)
df2=df1.withColumn("newDataColumn",myudf(df1["xmldatafield"]))

this works.这有效。 Except that newDataColumn is type STRING instead of Array.除了 newDataColumn 的类型是 STRING 而不是 Array。 So I can't use any of the sql Array functions on it to access or work with individual elements.所以我不能在其上使用任何 sql 数组函数来访问或处理单个元素。

I've confirmed in python that the function is returning a List type.我已经在 python 中确认 function 正在返回一个列表类型。

Any idea what I'm doing wrong or how I could get this to be an array column type?知道我做错了什么或者我怎么能把它变成数组列类型?

A friend of mine just told me, the solution is passing the datatype to the UDF function. Duh我的一个朋友刚刚告诉我,解决方案是将数据类型传递给 UDF function。Duh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM