简体   繁体   English

处理依赖于 Spark 数据集中另一列的逗号分隔列

[英]Handling comma delimited columns with dependency on another column in Spark dataset

I have the below spark dataframe/dataset.我有以下火花数据框/数据集。

column_1 column_2     column_3 column_4
A,B      NameA,NameB  F        NameF
C        NameC        NULL     NULL
NULL     NULL         D,E      NameD,NULL
G        NULL         H        NameH
I        NameI        J        NULL

All the above 4 columns are comma delimited.以上 4 列均以逗号分隔。 I have to convert this into a new dataframe/dataset which has only 2 columns and without any comma delimiters.我必须将其转换为只有 2 列且没有任何逗号分隔符的新数据框/数据集。 The value in column_1 and its corresponding name in Column_2 should be written to output. column_1 中的值及其在 Column_2 中的对应名称应写入 output。 Similarly for column_3 and column_4.对于 column_3 和 column_4 也是如此。 If both are column_1 and column_2 are null, they are not required in output.如果 column_1 和 column_2 都是 null,则在 output 中不需要它们。

Expected output:预期 output:

out_column_1 out_column_2
A            NameA
B            NameB
F            NameF
C            NameC
D            NameD
E            NULL
G            NULL
H            NameH
I            NameI
J            NULL

Is there a way to achieve this in Java spark without using UDF's?有没有办法在不使用 UDF 的情况下在 Java spark 中实现这一点?

Scala solution - I think should work in Java. Scala 解决方案 - 我认为应该在 Java 中工作。 Basically just handle col1, col2 separately from col3, col4, and union the results.基本上只需将 col1、col2 与 col3、col4 分开处理,然后合并结果。 Lots of wrangling with arrays.与 arrays 有很多争论。

// maybe replace this with Dataset<Row> result = ... in Java
val result = df.select(
    split(col("column_1"), ",").alias("column_1"),
    split(col("column_2"), ",").alias("column_2")
).filter(
    "column_1 is not null"
).select(
    explode(
        arrays_zip(
            col("column_1"),
            coalesce(col("column_2"), array(lit(null)))
        )
    )
).select(
    "col.*"
).union(
    df.select(
        split(col("column_3"), ",").alias("column_3"),
        split(col("column_4"), ",").alias("column_4")
    ).filter(
        "column_3 is not null"
    ).select(
        explode(
            arrays_zip(
                col("column_3"), 
                coalesce(col("column_4"), array(lit(null)))
            )
        )
    ).select("col.*")
).toDF(
    "out_column_1", "out_column_2"
)
result.show
+------------+------------+
|out_column_1|out_column_2|
+------------+------------+
|           A|       NameA|
|           B|       NameB|
|           C|       NameC|
|           G|        null|
|           I|       NameI|
|           F|       NameF|
|           D|       NameD|
|           E|        null|
|           H|       NameH|
|           J|        null|
+------------+------------+

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

相关问题 Spark:将数据集的 2 列合并为一列 - Spark: Merging 2 columns of a DataSet into a single column 使用来自另一个数据集的值搜索和更新 Spark 数据集列 - Searching and updating a Spark Dataset column with values from another Dataset 火花与列和聚合 function 删除数据集中的其他列 - spark with column and aggregate function dropping other columns in the dataset 从 Spark 数据集中选择一些列和特定列的最大值 - Selecting Some Columns and a Max Value of A specific Column From Spark Dataset Spark Java 中的多个逗号分隔值分隔行 - Multiple comma delimited values to separate row in Spark Java Spark:忽略或处理DataSet选择错误 - Spark: Ignoring or handling DataSet select errors 使用Java通过将列名作为数组元素传递的数组来连接Apache Spark中的数据集列? - Concatenating Dataset columns in Apache Spark using Java by passing an array with column names as array elements? 判断一个字符串是否包含在另一个逗号分隔列表中的简单方法 - Easy way to tell if a string is contained in another string that is a comma delimited list 数据集上的拆分字符串列 <Row> 使用逗号并获取新的数据集 <Row> - Split String Column on the Dataset<Row> with comma and get new Dataset<Row> spark scala:将DataFrame OR Dataset转换为单个逗号分隔的字符串 - spark scala : Convert DataFrame OR Dataset to single comma separated string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM