spark行编码器:空元数据

ff29svar  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(486)

我在java中使用spark,并从row的rdd创建一个row数据集。
我使用

Metadata meta = new MetadataBuilder().putString("type", "categorical").build();
StructField s = new StructField(name, IntegerType, true, meta);
StructType t = new StructType(new StructField[]{s});  
Encoder<Row> encoder = RowEncoder.apply(t);

我在数据集中这样使用它

ds.flatMap((FlatMapFunction<Row, Row>) this::customFlatMapRow, encoder);

由于某些原因,在我编写表并检查模式的字段及其元数据之后,它们是空的(尽管事实上我是这样创建和设置它们的)。不知怎的,我把它们弄丢了

cld4siwp

cld4siwp1#

如果检查数据集的expressionencoder,则元数据可用。
代码

Metadata meta = new MetadataBuilder().putString("type", "categorical").build();
StructField s = new StructField("col", IntegerType, true, meta);
StructType t = new StructType(new StructField[]{s});
Encoder<Row> encoder = RowEncoder.apply(t);

Dataset<Row> df = spark.createDataset(Arrays.asList(1, 2, 3), Encoders.INT()).toDF("col");
Dataset<Row> df2 = df.flatMap((FlatMapFunction<Row, Row>) r -> Collections.singleton(r).iterator(), encoder);
System.out.println(df2.exprEnc().schema().fields()[0].metadata());

印刷品

{"type":"categorical"}

相关问题