Show Schema Model Problems
Updated: Jan 1, 2023
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
- SchemaDef with the name
world_statsis created and entities are added to it. - Duplicate field
codeis added to thecountryentity while duplicate entitycountry_populationis added to the schema. - The two
codefields are given the same primary key position which should not be the case. schema.getEntity("country").getSchemaProblems()will get problems only present incountryentity.schema.getSchemaProblems()will get problems only present in the schema not including those in its child elements such as the entities.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"
}]
------------------
