在 spark scala 中将 1 列拆分为 3 列

编程入门 行业动态 更新时间:2024-10-26 10:39:44
本文介绍了在 spark scala 中将 1 列拆分为 3 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 Spark 中有一个使用 Scala 的数据框,其中有一列我需要拆分.

I have a dataframe in Spark using scala that has a column that I need split.

scala> test.show +-------------+ |columnToSplit| +-------------+ | a.b.c| | d.e.f| +-------------+

我需要将此列拆分为如下所示:

I need this column split out to look like this:

+--------------+ |col1|col2|col3| | a| b| c| | d| e| f| +--------------+

我使用的是 Spark 2.0.0

I'm using Spark 2.0.0

谢谢

推荐答案

尝试:

import sparkObject.spark.implicits._ import org.apache.spark.sql.functions.split df.withColumn("_tmp", split($"columnToSplit", "\.")).select( $"_tmp".getItem(0).as("col1"), $"_tmp".getItem(1).as("col2"), $"_tmp".getItem(2).as("col3") )

这里要注意的重点是 sparkObject 是您可能已经初始化的 SparkSession 对象.因此,(1) import 语句必须强制内联在代码中,而不是在类定义之前.

The important point to note here is that the sparkObject is the SparkSession object you might have already initialized. So, the (1) import statement has to be compulsorily put inline within the code, not before the class definition.

更多推荐

在 spark scala 中将 1 列拆分为 3 列

本文发布于:2023-07-09 15:00:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1087151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   spark   scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!