Add Array Fields to Entities

This example will show you how you can define fields with an array and with/without a specific number of minimum or maximum elements.

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.SchemaDef;
import com.northconcepts.datapipeline.foundations.schema.TextFieldDef;

public class AddArrayFieldsToEntities {

    public static void main(String[] args) {
        SchemaDef schema = new SchemaDef("E-Commerce Schema");
        
        schema.addEntity(new EntityDef("products")
                .addField(new NumericFieldDef("product_id", FieldType.LONG).setPrimaryKeyPosition(0))
                .addField(new NumericFieldDef("supplier_id", FieldType.LONG))
                .addField(new TextFieldDef("product_name", FieldType.STRING))
                // An array with no limit on minimum or maximum elements.
                .addField(new TextFieldDef("color", FieldType.STRING).setArray(true))
                // An array of countries with minimum 2 elements.
                .addField(new TextFieldDef("countries", FieldType.STRING).setMinimumElements(2))
                // An array of cities with minimum 2 elements and maximum 100 elements.
                .addField(new TextFieldDef("cities", FieldType.STRING).setMinimumElements(2).setMaximumElements(100))
                );

        System.out.println("=========================================================");
        System.out.println(schema.toXml());
        System.out.println("=========================================================");
    }
}

Code walkthrough

  1. Create a SchemaDef named E-Commerce Schema with an entity.
  2. Create a field named color which is an array with no limit on minimum or maximum elements.
  3. Create a field named countries which is an array with 2 minimum elements, but no limit on maximum elements.
  4. Create a field named cities which is an array with 2 minimum and 100 maximum elements.
  5. Print this schema as XML.

Console Output

=========================================================
<schema name="E-Commerce Schema">
  <entities>
    <entity addMissingOptionalFields="false" allowExtraFieldsInMapping="true" allowExtraFieldsInValidation="true" name="products">
      <fields>
        <field array="false" name="product_id" primaryKeyPosition="0" required="false" strictArrays="true" type="LONG"/>
        <field array="false" name="supplier_id" required="false" strictArrays="true" type="LONG"/>
        <field allowBlank="true" array="false" name="product_name" required="false" strictArrays="true" type="STRING"/>
        <field allowBlank="true" array="true" minimumElements="0" name="color" required="false" strictArrays="true" type="STRING"/>
        <field allowBlank="true" array="true" minimumElements="2" name="countries" required="false" strictArrays="true" type="STRING"/>
        <field allowBlank="true" array="true" maximumElements="100" minimumElements="2" name="cities" required="false" strictArrays="true" type="STRING"/>
      </fields>
    </entity>
  </entities>
</schema>

=========================================================
Mobile Analytics