Add Indexes to Entities

This example will show you how you can add indexes to entities. You can create indexes for one field or for a combination of more than one field. Also, you can define the order (ascending or descending) for the index. Default order is ascending.

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

public class AddIndexesToEntities {

    public static void main(String[] args) {
        SchemaDef schema = new SchemaDef("E-Commerce Schema");
        
        schema.addEntity(new EntityDef("suppliers")
                // Primary Key
                .addField(new NumericFieldDef("supplier_id", FieldType.LONG).setPrimaryKeyPosition(0))
                .addField(new TextFieldDef("supplier_name", FieldType.STRING))
                .addField(new TextFieldDef("city", FieldType.STRING))
                .addField(new TextFieldDef("country", FieldType.STRING))
                .addField(new TextFieldDef("contact_details", FieldType.STRING))
                // A unique index for supplier_name
                .addIndex(new IndexDef().setName("idx_supplier_name")
                        // index for supplier_name with ascending order.
                        .addIndexField(new IndexFieldDef("supplier_name", true))
                        .setUnique(true))
                .addIndex(new IndexDef("idx_city_supplier_name", "city", "supplier_name"))
                );

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

Code walkthrough

  1. Create a SchemaDef named E-Commerce Schema with one entity.
  2. This entity has several necessary fields for which indexes can be created.
  3. Create an index named idx_supplier_name for supplier_name field. This index is defined as ascending.
  4. Create another index named idx_city_supplier_name for combination of city and supplier_name field.
  5. Print this schema as XML.

Console Output

=========================================================
<schema name="E-Commerce Schema">
  <entities>
    <entity addMissingOptionalFields="false" allowExtraFieldsInMapping="true" allowExtraFieldsInValidation="true" name="suppliers">
      <fields>
        <field array="false" name="supplier_id" primaryKeyPosition="0" required="false" strictArrays="true" type="LONG"/>
        <field allowBlank="true" array="false" name="supplier_name" required="false" strictArrays="true" type="STRING"/>
        <field allowBlank="true" array="false" name="city" required="false" strictArrays="true" type="STRING"/>
        <field allowBlank="true" array="false" name="country" required="false" strictArrays="true" type="STRING"/>
        <field allowBlank="true" array="false" name="contact_details" required="false" strictArrays="true" type="STRING"/>
      </fields>
      <indexes>
        <index name="idx_supplier_name" unique="true">
          <index-field ascending="true" fieldName="supplier_name"/>
        </index>
        <index name="idx_city_supplier_name" unique="false">
          <index-field ascending="true" fieldName="city"/>
          <index-field ascending="true" fieldName="supplier_name"/>
        </index>
      </indexes>
    </entity>
  </entities>
</schema>

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