How to create a dataset in Apache Spark with a complex schema

I have a java spark application where I need to create a dataset object. The trick is that it has multiple layers to it, creating issues when using createDataFrame(...) method. The (dummy) schema for my dataset can be defined as below:

        StructType objectEntities = new StructType(new StructField[]{
                new StructField("id",DataTypes.StringType,true, Metadata.empty()),
                new StructField("type",DataTypes.StringType,true,Metadata.empty())
        });
        StructType subjectEntities = new StructType(new StructField[]{
                new StructField("id",DataTypes.StringType,true, Metadata.empty()),
                new StructField("type",DataTypes.StringType,true,Metadata.empty())
        });

        StructType innerLevelStruct = new StructType();
        innerLevelStruct = innerLevelStruct.add("f1",DataTypes.StringType,false);
        innerLevelStruct = innerLevelStruct.add("f2",DataTypes.StringType,false);
        innerLevelStruct = innerLevelStruct.add("f3",DataTypes.StringType,false);
        innerLevelStruct = innerLevelStruct.add("f4",DataTypes.StringType,false);
        innerLevelStruct = innerLevelStruct.add("f5",objectEntities,true);
        innerLevelStruct = innerLevelStruct.add("f6",subjectEntities,true);

        StructType outerLayerStruct = new StructType();
        outerLayerStruct = outerLayerStruct.add("id",DataTypes.StringType,false);
        outerLayerStruct = outerLayerStruct.add("type",DataTypes.StringType,false);
        outerLayerStruct = outerLayerStruct.add("state",DataTypes.StringType,false);
        outerLayerStruct = outerLayerStruct.add("items",innerLevelStruct,false);

        StructType entityDetailsSchema = new StructType();
        entityDetailsSchema = entityDetailsSchema.add("id",DataTypes.StringType,false);
        entityDetailsSchema = entityDetailsSchema.add("cols",outerLayerStruct,false);

With schema defined, I thought I can define the data as so

        List<Row> entries = new ArrayList<>();
        entries.add(RowFactory.create("id",List.of(
                "testId","testType","testState",
                List.of(
                        "f1Val","f2Val","f3Val","f4Val",List.of("a","b"),List.of("c","v")
                )
        )));
        Dataset<Row> output = sparkSession.createDataFrame(entries,entityDetailsSchema);

Where sparkSession is a standard spark sessions defined as so:

sparkSession = SparkSession.builder()
                .master("local[1]")
                .appName("TestSpark")
                .getOrCreate();

Normally, that would work for datasets with simple 1 layered schema. However, when I run my app with the schema I mentioned at the start, I get:

java.lang.IllegalArgumentException: The value ([testId, testType, testState, [f1Val, f2Val, f3Val, f4Val, [a, b], [c, v]]]) of the type (java.util.ImmutableCollections.ListN) cannot be converted to struct<id:string,type:string,state:string,innerLevelStruct:struct<f1:string,f2:string,f3:string,f4:string,objectEntities:struct<id:string,type:string>,subjectEntities:struct<id:string,type:string>>>

I suspect it is because I make use of List.of(...). The issue goes away by replacing List.of(...) with RowFactory.create(...) but it is not a suitable solution seeing as it creates problems when I try to convert dataset to POJO via encoder as so:

output.as(Encoders.bean(MyPojoClass.class))
                    .foreachPartition(partition -> {
                        // do work
                    });

Giving me an exception stating that it expects Arrays instead of structType:

org.apache.spark.sql.AnalysisException: [UNSUPPORTED_DESERIALIZER.DATA_TYPE_MISMATCH] The deserializer is not supported: need a(n) "ARRAY" field but got "STRUCT<...>

So the question is as follows: how can I create dataset using the schema provided? How can I define a row that would populate each layer with data? Is RowFactory even capable of that – from what I read, it is a linear simple method, so not sure that it is even correct to use it?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật