spark 是否按字母顺序在内部重新排列数据框中字段的顺序?

Does spark rearranges the order of fields in a dataframe internally in alphabetical order?

我正在创建一个 spark streaming 作业,它从 Kafka Topic.For 读取 JSON 消息,我从 Dstream 获得的每个 RDD,我正在创建一个 dataframe.My 要求是写这个数据帧到 hdfs path.before 写入,我需要检查此消息的模式是否格式正确。 所以我创建了一个 StructType customSchema,其预期字段的顺序与 Kafka JSON 消息中的顺序相同 topic.I 我试图比较两者,但是当所有字段都正确存在时,这并不 work.Even订单,结果为假。

我有一个 json 文件,其格式与 Kafka 主题中的格式相同。

{"transactionId":"12345","accountName":"XXX1","sessionKey":"WEB","description":"INR"}

我围绕它创建了一个数据框

val df=spark.read.json("/data/path/sample/")

scala> df.printSchema
root
 |-- accountName: string (nullable = true)
 |-- description: string (nullable = true)
 |-- sessionKey: string (nullable = true)
 |-- transactionId: string (nullable = true)

注意:当我打印我创建的 df 的架构时,它是按字母顺序打印的。

我创建了一个 StructType 的自定义架构

scala> val schema1=  StructType( Array (StructField("transactionId",StringType, true),StructField("accountName",StringType, true),StructField("sessionKey",StringType, true),StructField("description",StringType, true)))
schema1: org.apache.spark.sql.types.StructType = StructType(StructField(transactionId,StringType,true), StructField(accountName,StringType,true), StructField(sessionKey,StringType,true), StructField(description,StringType,true))

当我尝试匹配它时,结果为假

scala> val d=df.schema==schema1
            d: Boolean = false

.equals 方法的结果也是假的 现在,如果我以与打印 printSchema 相同的方式创建我的 customSchema,

scala> val schema2=  StructType( Array (StructField("accountName",StringType, true),StructField("description",StringType, true),StructField("sessionKey",StringType, true),StructField("transactionId",StringType, true)))
schema2: org.apache.spark.sql.types.StructType = StructType(StructField(accountName,StringType,true), StructField(description,StringType,true), StructField(sessionKey,StringType,true), StructField(transactionId,StringType,true))

如果我现在比较两者,它按预期工作正常。

scala> val j=df.schema==schema2
            j: Boolean = true

所以根据我的观察,spark 是否在内部按字母顺序重新排列了字段的顺序,因为我在文档中找不到它。

Spark Json 如果未提供模式,则推断模式。 如果需要特定顺序的列,select 是更好的选择。

val colsArr = Array("col1","col2","col3")
val df = df.select(colsArr.head,colsArr.tail:_*)