GridGain Developers Hub

Creating Tables from Java Classes

Overview

While SQL DDL supports a comprehensive set of table manipulation commands, you can also create tables and build indexes directly from POJO using a simple Java API. This API supports custom annotations and simple builders; it works seamlessly with the Mapper interface, thus facilitating keyValueView and recordView.

The Java API lets you perform the following operations:

  • CREATE ZONE

  • CREATE TABLE

  • CREATE INDEX

  • DROP ZONE

  • DROP TABLE

  • DROP INDEX

You use the @Table and other annotations that are located in the org.apache.ignite.catalog.annotations package.

Examples

KV POJO Compatible with keyValueView

The example below created a table called kv_pojo from java code:

class ZoneTest {}

class PojoKey {
    @Id
    Integer id;

    @Id(sort = DESC)
    @Column(value = "id_str", length = 20)
    String idStr;
}

@Table(
    value = "kv_pojo",
    zone = @Zone(
		value = "zone_test",
		replicas = 2,
		storageProfiles = "default"
   ),
    colocateBy = { @ColumnRef("id"), @ColumnRef("id_str") },
    indexes = { @Index(value = "ix", columns = {
                    @ColumnRef(value = "f_name"),
                    @ColumnRef(value = "l_name") })
    }
)
class PojoValue {
    @Column("f_name")
    String firstName;

    @Column("l_name")
    String lastName;

    String str;
}

Table myTable = ignite.catalog().create(PojoKey.class, PojoValue.class);

ignite.tables().table(myTable).keyValueView(PojoKey.class, PojoValue.class);

The result is equivalent to the following SQL multi-statement:

CREATE ZONE IF NOT EXISTS zone_test WITH PARTITIONS=2, STORAGE_PROFILES='default';

CREATE TABLE IF NOT EXISTS kv_pojo (
	id int,
	id_str varchar(20),
	f_name varchar,
	l_name varchar,
	str varchar,
	PRIMARY KEY (id, id_str)
)
COLOCATE BY (id, id_str)
WITH PRIMARY_ZONE='ZONE';

CREATE INDEX ix (f_name, l_name desc nulls last);

Single POJO Compatible with recordView

The example below creates the pojo_sample table by using the pojo compatible with recordView:

@Table(
    value = "pojo_sample",
    zone = zone = @Zone(
		value = "zone_test",
		replicas = 2,
		storageProfiles = "default"
   ),
    colocateBy = { @ColumnRef("id"), @ColumnRef("id_str") },
    indexes = { @Index(value = "ix_sample", columns = {
                      @ColumnRef(value = "f_name"),
                      @ColumnRef(value = "l_name")}
    }
)
class Pojo {
    @Id
    Integer id;

    @Id(sort = DESC)
    @Column(value = "id_str", length = 20)
    String idStr;

    @Column("f_name")
    String firstName;

    @Column("l_name")
    String lastName;

    String str;
}

Table myTable = ignite.catalog().create(Pojo.class);

ignite.tables().table(myTable).recordView(Pojo.class)

The Builder Alternative to the @Table Annotation

The example below uses a builder to create a table:

class Pojo {
    @Id
    Integer id;

    @Id(sort = DESC)
    @Column(value = "id_str", length = 20)
    String idStr;

    @Column("f_name")
    String firstName;

    @Column("l_name")
    String lastName;

    String str;
}

ignite.catalog()
  .create(ZoneDefinition.builder("zone_test")
    .partitions(2))
  .execute();

ignite.catalog()
  .create(TableDefinition.builder("pojo_test")
    .ifNotExists()
  	.colocateBy("id", "id_str")
  	.zone("zone_test")
    .record(Pojo.class) // .key(Key.class).value(Value.class)
    .build())

Next Steps

Once you have created a table using the Java API, you can manipulate it using the SQL commands.