我有一个structType模式,我需要根据字段名称对模式进行排序/排序,下面我共享了StructType。
StructType schema = StructType(StructField(zzz,StringType,true),
StructField(kkk,StringType,true),
StructField(aaa,StringType,true),
StructField(lll,StringType,true))
我想获得上面的structType,如下所示
StructType schema = StructType(StructField(aaa,StringType,true),
StructField(kkk,StringType,true),
StructField(lll,StringType,true),
StructField(zzz,StringType,true))
答案 0 :(得分:1)
在Java中,
new StructType(Stream.of(schema.fields())
.sorted(Comparator.comparing(StructField::name))
.collect(Collectors.toList()).toArray(new StructField[schema.fields().length]))
在Scala中 您可以做类似的事情
var schema = StructType(Seq(StructField("zzz",StringType,true),
StructField("kkk",StringType,true),
StructField("aaa",StringType,true),
StructField("lll",StringType,true)))
def reorderSchema: StructType => StructType = {schema => StructType(schema.sortBy(_.name))}
var newSchema = reorderSchema(schema)