Using APIs to create a Global Active Table

You can use NoSQL Java SDK to create a Global Active table in NDCS

You use the Java API class (AddReplicaRequest) to execute the DDL statement to create a Global Active table.

To create a Global Active table:
  • Create a singleton table.
  • Freeze the schema of the table. You can freeze the table while creating it by adding with schema frozen in the CREATE TABLE statement. If the table does not contain any JSON columns, you can freeze the schema by adding with schema frozen force in the CREATE TABLE statement.
  • A NoSQL Database table becomes a Global Active table only after adding a regional replica table. The regional replica table is created in another region.
Consider the examples given below:
  • In the example for table with JSON columns, the schema is frozen by adding with schema frozen in the CREATE TABLE statement.
  • In the example for table without JSON columns, the schema is frozen by adding with schema frozen force in the CREATE TABLE statement. We also provided the TTL or Time-To-Live value in the CREATE TABLE statement as we want the rows of this table to expire in three days.
/* Initialize NoSQLHandle */
NoSQLHandleConfig config = new NoSQLHandleConfig(endpoint)
                          .setAuthorizationProvider(new SignatureProvider())
                          .setDefaultCompartment(compartmentId);
NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config);
/* Create table */
String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + "(" +
             "id INTEGER, name STRING, info JSON, PRIMARY KEY(id)) " +
             "with schema frozen";
String tableName = "<name_your_table";
String region = "<name_your_sender_region>";
System.output.println("Creating table: " + ddl);
TableRequest tableReq = new TableRequest()
                        .setStatement(ddl)
                        .setTableLimits(new TableLimits(50, 50, 1));
TableResult res = handle.tableRequest(tableReq);
res.waitForCompletion(handle, 90000, 1000);
System.output.println("Table created: " + tableName);
/* add replica ca-montreal-1 */
System.output.println("\nAdding replica: " + region);
AddReplicaRequest addRepReq = new AddReplicaRequest()
                              .setTableName(tableName)
                              .setReplicaName(region);
res = handle.addReplica(addRepReq);
res.waitForCompletion(handle, 90000, 1000);
System.output.println("Added replica: " + region);
/* Initialize NoSQLHandle */
NoSQLHandleConfig config = new NoSQLHandleConfig(endpoint)
                          .setAuthorizationProvider(new SignatureProvider())
                          .setDefaultCompartment(compartmentId);
NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config);
/* Create table */
String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + "(" +
             "id INTEGER, name STRING, PRIMARY KEY(id)) " +
             "with schema frozen force USING TTL 3 days";
String tableName = "<name_your_table";
String region = "<name_your_sender_region>";
System.output.println("Creating table: " + ddl);
TableRequest tableReq = new TableRequest()
                        .setStatement(ddl)
                        .setTableLimits(new TableLimits(50, 50, 1));
TableResult res = handle.tableRequest(tableReq);
res.waitForCompletion(handle, 90000, 1000);
System.output.println("Table created: " + tableName);
/* add replica ca-montreal-1 */
System.output.println("\nAdding replica: " + region);
AddReplicaRequest addRepReq = new AddReplicaRequest()
                              .setTableName(tableName)
                              .setReplicaName(region);
res = handle.addReplica(addRepReq);
res.waitForCompletion(handle, 90000, 1000);
System.output.println("Added replica: " + region);

Creating a child table:

Use the TableRequest class to create a child table. Additionally, to make the child table a Global Active table, you need to freeze the child table's schema and add a regional replica. You can pick from one of the regional replicas of the parent table.
/* Initialize NoSQLHandle */
NoSQLHandleConfig config = new NoSQLHandleConfig(endpoint)
                          .setAuthorizationProvider(new SignatureProvider())
                          .setDefaultCompartment(compartmentId);
NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config);

/* Create table */
String ddl = "CREATE TABLE IF NOT EXISTS " + fulltableName + "(" +
             "cid INTEGER, cname STRING, cinfo JSON, PRIMARY KEY(id)) " +
             "with schema frozen";
System.output.println("Creating child table: " + ddl);
TableRequest tableReq = new TableRequest()
                        .setStatement(ddl));
TableResult res = handle.tableRequest(tableReq);
res.waitForCompletion(handle, 90000, 1000);
System.output.println("Child Table created: " + tableName);

/* add replica ca-montreal-1 */
System.output.println("\nAdding replica: " + region);
AddReplicaRequest addRepReq = new AddReplicaRequest()
                              .setTableName(fulltableName)
                              .setReplicaName(region);
res = handle.addReplica(addRepReq);
res.waitForCompletion(handle, 90000, 1000);
System.output.println("Added replica: " + region);