Show Schema Model Problems

This example shows you how to detect problems that may be in your schema.

The schema can comprise various child elements (for example the EntityDef). In this example, you will learn how to output the schema, entity and schema child elements problems.

To demonstrate this example, a schema with existing problems has been intentionally created.

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 ShowSchemaProblems {

    public static void main(String[] args) {
        SchemaDef schema = new SchemaDef("world_stats");

        SchemaDef addEntity = schema.addEntity(new EntityDef("country")
                .addField(new TextFieldDef("code", FieldType.STRING).setPrimaryKeyPosition(0))
                .addField(new TextFieldDef("code", FieldType.STRING).setPrimaryKeyPosition(0)));

        schema.addEntity(new EntityDef("country_population")
                .addField(new TextFieldDef("country_code", FieldType.STRING).setPrimaryKeyPosition(0))
                .addField(new NumericFieldDef("year", FieldType.INT).setPrimaryKeyPosition(1))
                .addField(new NumericFieldDef("population", FieldType.BIG_DECIMAL)));

        schema.addEntity(new EntityDef("country_population"));

        System.out.println("----------------Direct Entity Problems------------------");
        System.out.println(schema.getEntity("country").getSchemaProblems());
        System.out.println("----------------Direct Schema Problems------------------");
        System.out.println(schema.getSchemaProblems());
        System.out.println("----------------All Schema Problems------------------");
        System.out.println(schema.getSchemaProblems(true));
        System.out.println("------------------");
    }

}

Code Walkthrough

  1. SchemaDef with the name world_stats is created and entities are added to it.
  2. Duplicate field code is added to the country entity while duplicate entity country_population is added to the schema.
  3. The two code fields are given the same primary key position which should not be the case.
  4. schema.getEntity("country").getSchemaProblems() will get problems only present in country entity.
  5. schema.getSchemaProblems() will get problems only present in the schema not including those in its child elements such as the entities.
  6. schema.getSchemaProblems(true) will get all problems in the schema including those in its child elements such as the entities.

Output

----------------Direct Entity Problems------------------
[{
  "details" : "Duplicate field \"code\""
}, {
  "details" : "\"code\" field has wrong primaryKeyPosition: expected 1, but found 0"
}]
----------------Direct Schema Problems------------------
[{
  "details" : "Duplicate entity \"country_population\""
}]
----------------All Schema Problems------------------
[{
  "details" : "Duplicate entity \"country_population\""
}, {
  "details" : "Duplicate field \"code\""
}, {
  "details" : "\"code\" field has wrong primaryKeyPosition: expected 1, but found 0"
}]
------------------
Mobile Analytics