我试着用schema创建空df:
val sparkConf = new SparkConf()
.setAppName("app")
.setMaster("local")
val sparkSession = SparkSession
.builder()
.config(sparkConf)
.getOrCreate()
val sparkContext = sparkSession.sparkContext
var tmpScheme = StructType(
StructField("source_id", StringType, true) :: Nil)
var df = conf.SparkConf.sparkSession.createDataFrame(tmpScheme)
得到了Schema for type org.apache.spark.sql.types.DataType is not supported ...
我不明白为什么-甚至在Imports中也没有.DataType
:
import org.apache.spark.sql.types.{BooleanType, IntegerType, StringType, StructField, StructType}
这里有什么问题吗?
PS:Spark版
"org.apache.spark" %% "spark-sql" % "3.2.2", // spark
"org.apache.spark" %% "spark-core" % "3.2.2", // spark
1条答案
按热度按时间nbysray51#
如果查看文档,可以看到
StructType
的参数fields
的类型为Array[StructField]
,并且传递的是StructField
。这意味着您应该使用
Array
PackageStructField
,例如:祝你好运!
createDataframe
中有一个参数的情况:createDataframe
中有两个参数e的情况:两种情况的输出是相同的:
在您的例子中,模式试图从类的属性(
StructType
)中推断出来,并试图用StructField: source_id
填充。StructType
扩展了DataType
,这就是错误的来源(Spark不能解析类型)