如何 Order/Sort 基于字段名称的 structType Java Spark 2.1.1
How to Order/Sort the structType based on the field names in Java Spark 2.1.1
我有一个 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)
我有一个 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)