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 NoSQL table with at least one JSON column in it.
  • Freeze the schema of the table. You can freeze the table while creating it by adding with schema frozen in the DDL 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.
/* 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);

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);