当前位置: 首页 > 面试题库 >

如何使用selectExpr在Spark数据帧中强制转换结构数组?

陈琪
2023-03-14
问题内容

如何在spark数据帧中强制转换结构数组?

让我通过一个例子来说明我要做什么。我们将从创建一个数据框开始,该数据框包含行和嵌套行的数组。我的整数尚未在数据框中强制转换,它们已创建为字符串:

import org.apache.spark.sql._
import org.apache.spark.sql.types._
val rows1 = Seq(
  Row("1", Row("a", "b"), "8.00", Seq(Row("1","2"), Row("12","22"))),
  Row("2", Row("c", "d"), "9.00", Seq(Row("3","4"), Row("33","44")))
)

val rows1Rdd = spark.sparkContext.parallelize(rows1, 4)

val schema1 = StructType(
  Seq(
    StructField("id", StringType, true),
    StructField("s1", StructType(
      Seq(
        StructField("x", StringType, true),
        StructField("y", StringType, true)
      )
    ), true),
    StructField("d", StringType, true),
    StructField("s2", ArrayType(StructType(
      Seq(
        StructField("u", StringType, true),
        StructField("v", StringType, true)
      )
    )), true)
  )
)

val df1 = spark.createDataFrame(rows1Rdd, schema1)

这是创建的数据框的架构:

       df1.printSchema
       root
       |-- id: string (nullable = true)
       |-- s1: struct (nullable = true)
       |    |-- x: string (nullable = true)
       |    |-- y: string (nullable = true)
       |-- d: string (nullable = true)
       |-- s2: array (nullable = true)
       |    |-- element: struct (containsNull = true)
       |    |    |-- u: string (nullable = true)
       |    |    |-- v: string (nullable = true)

我想做的是将所有可以为整数的字符串都转换为整数。我尝试执行以下操作,但没有成功:

df1.selectExpr("CAST (id AS INTEGER) as id",
  "STRUCT (s1.x, s1.y) AS s1",
  "CAST (d AS DECIMAL) as d",
  "Array (Struct(CAST (s2.u AS INTEGER), CAST (s2.v AS INTEGER))) as s2").show()

我有以下异常:

cannot resolve 'CAST(`s2`.`u` AS INT)' due to data type mismatch: cannot cast array<string> to int; line 1 pos 14;

任何人都有正确的查询将所有值转换为INTEGER吗?我将不胜感激。

非常感谢,


问题答案:

您应该匹配一个完整的结构:

val result = df1.selectExpr(
  "CAST(id AS integer) id",
  "s1",
  "CAST(d AS decimal) d",
  "CAST(s2 AS array<struct<u:integer,v:integer>>) s2"
)

它应该为您提供以下架构:

result.printSchema



root
 |-- id: integer (nullable = true)
 |-- s1: struct (nullable = true)
 |    |-- x: string (nullable = true)
 |    |-- y: string (nullable = true)
 |-- d: decimal(10,0) (nullable = true)
 |-- s2: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- u: integer (nullable = true)
 |    |    |-- v: integer (nullable = true)

和数据:

result.show



+---+-----+---+----------------+
| id|   s1|  d|              s2|
+---+-----+---+----------------+
|  1|[a,b]|  8|[[1,2], [12,22]]|
|  2|[c,d]|  9|[[3,4], [33,44]]|
+---+-----+---+----------------+


 类似资料:
  • 有时(例如用于测试和bechmark)我想强制执行在DataFrame上定义的转换。AFAIK调用像这样的操作并不能确保所有都被实际计算,可能只计算所有的子集(参见下面的示例) 我的解决方案是使用df将数据帧写入HDFS。写saveAsTable,但这会将我的系统与我不想再保留的表“混在一起”。 那么,触发数据帧评估的最佳方式是什么呢? 编辑: 请注意,最近还讨论了火花开发者列表:http://a

  • 我有一个如下的CSV文件。 我想把这个转化成下面。 基本上,我想在输出数据帧中创建一个名为idx的新列,该列将填充与键=idx,value=“n”后面的行相同的值“n”。

  • 我正在运行Spark 2.3。我想将以下数据帧中的列<code>features 我已经写了下面的< code>UDF,但它似乎不工作: 我希望提取以下特征作为向量,以便对其执行聚类。 我也注册UDF,然后继续调用它如下: 在运行此代码段时,我面临以下错误: ReadProcessData$1不能强制转换为org.apache.spark.sql.expressions。用户定义聚合函数

  • 我需要展平一个数据帧,以便将其与Spark(Scala)中的另一个数据帧连接起来。 基本上,我的2个数据帧有以下模式: 数据流1 DF2 老实说,我不知道如何使DF2变平。最后,我需要连接DF.field4 = DF2.field9上的2个数据帧 我用的是2.1.0 我的第一个想法是使用爆炸,但在Spark 2.1.0中已经被否决了,有人能给我一点提示吗?

  • 我正在尝试将熊猫DF转换为Spark one。测向头: 代码: 我得到了一个错误:

  • RDD是以数组[数组[字符串]的格式创建的,具有以下值: 我想用模式创建一个数据帧: 接下来的步骤: 给出以下错误: