如何在Spark Java中使用StructType为以下数据定义数据类型

sam|mars|1234567|"report": {"Details": [{"subject": "science","grade": "A","remark": "good"},{"subject": "maths","grade": "E","remark": "excellent"},{"subject": "geography","grade": "E","remark": "excellent"}]}
harry|venus|987654|"report": {"Details": [{"subject": "science","grade": "O","remark": "outstanding"},{"subject": "history","grade": "A","remark": "good"}]}


字段是:NAME,ADDRESS,ID,REPORTCARD

我有以下代码:

        JavaRDD<Row> row = javaRDD.map(new Function<String, Row>(){
            @Override
            public Row call(String line) throws Exception {
                return RowFactory.create((line.split("|")));
            }
        });


    where,
    javaRDD is created on top of the above input data.


现在我需要使用下面的代码行将javaRDD转换为Dataframe(Dataset df):

            Dataset<Row> df = spark.createDataFrame(row, <STRUCT TYPE SCHEMA>);


我需要为此创建一个StructType模式。如何在Spark Java中定义它。

我在StructType的以下架构中创建了:

            List<StructField> reportFields = new ArrayList<StructField>();
            reportFields.add(DataTypes.createStructField("subject", DataTypes.StringType, true));
            reportFields.add(DataTypes.createStructField("grade", DataTypes.StringType, true));
            reportFields.add(DataTypes.createStructField("remark", DataTypes.StringType, true));

            List<StructField> schemaFields = new ArrayList<StructField>();
            schemaFields.add(DataTypes.createStructField("NAME", DataTypes.StringType, true));
            schemaFields.add(DataTypes.createStructField("ADDRESS", DataTypes.StringType, true));
            schemaFields.add(DataTypes.createStructField("ID", DataTypes.StringType, true));
            schemaFields.add(DataTypes.createStructField("REPORTCARD", DataTypes.createStructType(reportFields), true));
            StructType schema = DataTypes.createStructType(schemaFields);

            Dataset<Row> df = spark.createDataFrame(row, schema);


但我得到以下例外:

java.lang.RuntimeException: Error while encoding: java.lang.RuntimeException: java.lang.String is not a valid external type for schema of struct<subject:string,grade:string,remark:string>
if (assertnotnull(input[0, org.apache.spark.sql.Row, true]).isNullAt) null else staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, validateexternaltype(getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 0, NAME), StringType), true, false) AS NAME#0
if (assertnotnull(input[0, org.apache.spark.sql.Row, true]).isNullAt) null else staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, validateexternaltype(getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 1, ADDRESS), StringType), true, false) AS ADDRESS#1
if (assertnotnull(input[0, org.apache.spark.sql.Row, true]).isNullAt) null else staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, validateexternaltype(getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 2, ID), StringType), true, false) AS ID#2
if (assertnotnull(input[0, org.apache.spark.sql.Row, true]).isNullAt) null else named_struct(subject, if (validateexternaltype(getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 3, REPORTCARD), StructField(subject,StringType,true), StructField(grade,StringType,true), StructField(remark,StringType,true)).isNullAt) null else staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, validateexternaltype(getexternalrowfield(validateexternaltype(getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 3, REPORTCARD), StructField(subject,StringType,true), StructField(grade,StringType,true), StructField(remark,StringType,true)), 0, subject), StringType), true, false), grade, if (validateexternaltype(getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 3, REPORTCARD), StructField(subject,StringType,true), StructField(grade,StringType,true), StructField(remark,StringType,true)).isNullAt) null else staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, validateexternaltype(getexternalrowfield(validateexternaltype(getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 3, REPORTCARD), StructField(subject,StringType,true), StructField(grade,StringType,true), StructField(remark,StringType,true)), 1, grade), StringType), true, false), remark, if (validateexternaltype(getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 3, REPORTCARD), StructField(subject,StringType,true), StructField(grade,StringType,true), StructField(remark,StringType,true)).isNullAt) null else staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, validateexternaltype(getexternalrowfield(validateexternaltype(getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 3, REPORTCARD), StructField(subject,StringType,true), StructField(grade,StringType,true), StructField(remark,StringType,true)), 2, remark), StringType), true, false)) AS REPORTCARD#3

最佳答案

这个StructType应该足够了

   StructType details = new StructType(new StructField[]{
    new StructField("subject", DataTypes.StringType, false, Metadata.empty()),
    new StructField("grade", DataTypes.StringType, false, Metadata.empty()),
    new StructField("remark", DataTypes.StringType, false, Metadata.empty())
   });

   StructType recordType = new StructType();
   recordType = recordType.add("details", details, false);

   StructType structType = new StructType();
   structType = structType.add("name", DataTypes.StringType, false);
   structType = structType.add("planet", DataTypes.StringType, false);
   structType = structType.add("number", DataTypes.StringType, false);
   structType = structType.add("record", recordType, false);

10-08 00:56