You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spark中DayTimeIntervalType等间隔数据类型应从哪里导入?

DayTimeIntervalType 与 YearMonthIntervalType 正确导入方式

这两类是Spark 3.2版本正式纳入公共API的ANSI标准间隔类型,Spark 3.1及更早版本中仅作为内部实现类存在,未开放公共使用权限,不同语言的导入路径如下:

Scala 环境(Spark 3.2+ 版本可用)

两个类型实际归属org.apache.spark.sql.types包,只是部分小版本未将其加入通配符导出列表,直接显式导入即可正常使用,无需找其他子包路径:

import org.apache.spark.sql.types.DayTimeIntervalType
import org.apache.spark.sql.types.YearMonthIntervalType

// 用法示例:构建包含间隔类型字段的Schema
import org.apache.spark.sql.types.{StructType, StructField}
val schema = StructType(Seq(
  StructField("time_duration", DayTimeIntervalType()),
  StructField("year_month_period", YearMonthIntervalType())
))
val df = spark.createDataFrame(spark.sparkContext.emptyRDD[org.apache.spark.sql.Row], schema)
df.printSchema()

Spark 3.1及更低版本中,这两个类型存放在org.apache.spark.sql.catalyst.util路径下,属于内部实现类,无稳定API承诺,不建议生产环境调用。

PySpark 环境(Spark 3.2+ 版本可用)

两个类型归属pyspark.sql.types模块,直接显式导入即可,无需从其他子模块查找:

from pyspark.sql.types import (
    DayTimeIntervalType,
    YearMonthIntervalType,
    StructType,
    StructField
)

# 用法示例:构建包含间隔类型字段的Schema
schema = StructType([
    StructField("time_duration", DayTimeIntervalType()),
    StructField("year_month_period", YearMonthIntervalType())
])
df = spark.createDataFrame([], schema=schema)
df.printSchema()

PySpark 3.1及更低版本中,这两个类型是前缀带下划线的内部类(_DayTimeIntervalType、_YearMonthIntervalType),仅服务于框架内部逻辑,不对外提供兼容性保障。

常见导入报错原因

  • 本地Spark版本低于3.2,两类间隔类型尚未开放公共访问权限
  • 仅使用通配符方式导入(Scala的org.apache.spark.sql.types._、Python的from pyspark.sql.types import *),部分3.2早期小版本未将两类加入通配符导出清单,显式写全类名导入即可解决

内容的提问来源于stack exchange,提问作者Vlad Balalaykin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 05:03:39