Add Attributes and Tags to Schemas

This example will show how to add arbitrary data and classifiers to your objects using Attributes and Tags.

Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;

import java.time.LocalDateTime;

import com.northconcepts.datapipeline.core.FieldType;
import com.northconcepts.datapipeline.foundations.schema.BooleanFieldDef;
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 AddAttributesAndTagsToSchemas {

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

        EntityDef entity = new EntityDef("user_information")
                .addField(new NumericFieldDef("USER_ID", FieldType.INT))
                .addField(new TextFieldDef("USER_NAME", FieldType.STRING))
                .addField(new TextFieldDef("PASSWORD", FieldType.STRING))
                .addField(new TextFieldDef("FIRST_NAME", FieldType.STRING))
                .addField(new TextFieldDef("LAST_NAME", FieldType.STRING))
                .addField(new TextFieldDef("EMAIL", FieldType.STRING))
                .addField(new NumericFieldDef("USER_ROLE_ID", FieldType.INT))
                .addField(new BooleanFieldDef("ACTIVE", FieldType.BOOLEAN))
                ;

        schema.getAttributes()
            .setValue("department", "Sales")
            .setValue("modified", LocalDateTime.now());
        schema.getTags().add("Draft");

        entity.getAttributes()
            .setValue("modified", LocalDateTime.now());
        entity.getTags().add("Draft").add("User Management").add("Security");
        
        System.out.println(schema.getAttributes());
        System.out.println("------------------");
        System.out.println(schema.getAttributes().toRecord());
        System.out.println("------------------");
        System.out.println(schema.getAttributes().toXml());
        System.out.println("------------------");
        System.out.println(schema.getAttributes().toJson());
        System.out.println("================================================");
        System.out.println(entity.getTags());
        System.out.println("------------------");
        System.out.println(entity.getTags().toRecord());
        System.out.println("------------------");
        System.out.println(entity.getTags().toXml());
        System.out.println("------------------");
        System.out.println(entity.getTags().toJson());

    }

}


Console Output

{
  "names" : [ "department", "modified" ]
}
------------------
Record (MODIFIED) {
    0:[department]:STRING=[Sales]:String
    1:[modified]:DATETIME=[Sat Jan 28 16:33:08 EST 2023]:Date
}

------------------
<attributes>
  <attribute name="department" type="STRING" value="Sales"/>
  <attribute name="modified" type="DATETIME" value="2023-01-28T16:33:08.368Z"/>
</attributes>

------------------
{"department":"Sales","modified":"2023-01-28T16:33:08.368Z"}
================================================
{
  "all" : [ "Draft", "Security", "User Management" ]
}
------------------
Record (MODIFIED) {
    0:[tags]:ARRAY of STRING=[[Draft, Security, User Management]]:ArrayValue
}

------------------
<tags>
  <tag value="Draft"/>
  <tag value="Security"/>
  <tag value="User Management"/>
</tags>

------------------
{"tags":["Draft","Security","User Management"]}
Mobile Analytics