[英]Custom processing on column in Apache Spark (Java)
我在Spark中加载了一个JSON文档,大致如下:
root
|-- datasetid: string (nullable = true)
|-- fields: struct (nullable = true)
...
| |-- type_description: string (nullable = true)
我的DF正在将其转换为:
df = df.withColumn("desc", df.col("fields.type_description"));
一切正常,但type_description
的值类似于:“ 1-我的描述类型”。
理想情况下,我希望我的df只包含文本部分,例如“我的描述类型”。 我知道该怎么做,但是如何通过Spark做到呢?
我希望遵循以下原则:
df = df.withColumn("desc", df.col("fields.type_description").call(/* some kind of transformation class / method*/));
谢谢!
通常,Spark提供了广泛的SQL函数集,从基本的字符串处理实用程序(通过日期/时间处理功能)到不同的统计摘要,不一而足。 这是oassql.functions
一部分。 在这种情况下,您可能想要这样的东西:
import static org.apache.spark.sql.functions.*;
df.withColumn("desc",
regexp_replace(df.col("fields.type_description"), "^[0-9]*\\s*-\\s*", "")
);
通常,在使用Spark SQL时,这些功能应该是您的首选。 有Catalyst表达式支持,通常提供codegen实用程序。 这意味着您可以从不同的Spark SQL优化中完全受益。
另一种但效率较低的方法是实现自定义UDF。 例如,请参见在SQLContext外部用Java创建SparkSQL UDF
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.