Add Nested Entities as Fields
Updated: Jan 31, 2023
This example will show you how you can create nested entities using RecordFieldDef.
Java code listing
package com.northconcepts.datapipeline.foundations.examples.schema; import com.northconcepts.datapipeline.core.FieldType; import com.northconcepts.datapipeline.foundations.schema.EntityDef; import com.northconcepts.datapipeline.foundations.schema.NumericFieldDef; import com.northconcepts.datapipeline.foundations.schema.PrimaryKeyType; import com.northconcepts.datapipeline.foundations.schema.RecordFieldDef; import com.northconcepts.datapipeline.foundations.schema.SchemaDef; import com.northconcepts.datapipeline.foundations.schema.TextFieldDef; public class AddNestedEntitiesAsFields { public static void main(String[] args) { SchemaDef schema = new SchemaDef("E-Commerce Schema"); schema.addEntity(new EntityDef("orders") .addField(new NumericFieldDef("order_id", FieldType.LONG).setPrimaryKeyPosition(0).setPrimaryKeyType(PrimaryKeyType.TABLE)) .addField(new RecordFieldDef("customer", "customers")) .addField(new TextFieldDef("product_name", FieldType.STRING)) .addField(new NumericFieldDef("quantity", FieldType.LONG)) ); schema.addEntity(new EntityDef("customers") .addField(new NumericFieldDef("customer_id", FieldType.LONG).setPrimaryKeyPosition(0).setPrimaryKeyType(PrimaryKeyType.AUTO)) .addField(new TextFieldDef("customer_name", FieldType.STRING)) .addField(new TextFieldDef("address", FieldType.STRING)) ); System.out.println("========================================================="); System.out.println(schema.toXml()); System.out.println("========================================================="); } }
Code walkthrough
- Create a SchemaDef named E-Commerce Schema with two entities.
- One entity is customers.
- Another entity is orders which has a RecordFieldDef named, customer. This is a nested entity.
- Print this schema as XML.
Console Output
========================================================= <schema name="E-Commerce Schema"> <entities> <entity addMissingOptionalFields="false" allowExtraFieldsInMapping="true" allowExtraFieldsInValidation="true" name="orders"> <fields> <field array="false" name="order_id" primaryKeyPosition="0" primaryKeyType="TABLE" required="false" strictArrays="true" type="LONG"/> <field array="false" name="customer" referencedEntityName="customers" required="false" strictArrays="true" type="RECORD"/> <field allowBlank="true" array="false" name="product_name" required="false" strictArrays="true" type="STRING"/> <field array="false" name="quantity" required="false" strictArrays="true" type="LONG"/> </fields> </entity> <entity addMissingOptionalFields="false" allowExtraFieldsInMapping="true" allowExtraFieldsInValidation="true" name="customers"> <fields> <field array="false" name="customer_id" primaryKeyPosition="0" primaryKeyType="AUTO" required="false" strictArrays="true" type="LONG"/> <field allowBlank="true" array="false" name="customer_name" required="false" strictArrays="true" type="STRING"/> <field allowBlank="true" array="false" name="address" required="false" strictArrays="true" type="STRING"/> </fields> </entity> </entities> </schema> =========================================================