在 Spark 2.4 中使用正则表达式替换向数据帧输出添加空值 [英] Adding nulls to dataframe output with regexp replace in Spark 2.4

查看:27
本文介绍了在 Spark 2.4 中使用正则表达式替换向数据帧输出添加空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用正则表达式替换来添加字符串null";到输出.语言是 aws 胶水中的 Spark Scala 2.40.解决此问题的最佳方法是什么?

I am trying to use regex replace to add a string "null" to the output. Language is Spark Scala 2.40 in aws glue. What is the best approach for this problem?

我正在通过数据框选择和解析需要添加空"的列来创建数据框.到:

I am creating a dataframe by dataframe select and parsing through the columns that I need to add "null" to:

 var select_df = raw_df.select(
          col("example_column_1"),
          col("example_column_2"),
          col("example_column_3")
      )

example_column_1 的输入

Input of example_column_1

#;#;Runner#;#;bob

example_column_1 的期望输出

Desired Output of example_column_1

null#;null#;Runner#;null#;bob

尝试:

select_df.withColumn("example_column_1", regexp_replace(col("example_column_1"), "", "null"))

推荐答案

任务可以分为两部分:

  1. 替换字符串开头的#
  2. 替换所有出现的 ;#

select_df
  .withColumn("example_column_1", regexp_replace('example_column_1, "^#", "null#"))
  .withColumn("example_column_1", regexp_replace('example_column_1, ";#", ";null#"))
  .show(false)

这篇关于在 Spark 2.4 中使用正则表达式替换向数据帧输出添加空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆