我有一个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))
在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)