Creación de tablas e índices

Aprenda a crear tablas e índices.

Crear una tabla es el primer paso del desarrollo de la aplicación. Utilice la clase TableRequest y los métodos para ejecutar todas las sentencias DDL como, por ejemplo, crear, modificar y borrar tablas. También puede establecer límites de tabla con el método TableRequest.setTableLimits.

La clase TableRequest permite transferir una sentencia DDL al método TableRequest.setStatement. Algunos ejemplos de sentencias DDL son:

/* Create a new table called users */
CREATE IF NOT EXISTS users(id INTEGER,
 name STRING,
 PRIMARY KEY(id));

/* Create a new table called users and set the TTL value to 4 days */
CREATE IF NOT EXISTS users(id INTEGER,
 name STRING,
 PRIMARY KEY(id))
USING TTL 4 days;



/* Create a new index called nameIdx on the name field in the users table */
CREATE INDEX IF NOT EXISTS nameIdx ON users(name);
Nota

El siguiente ejemplo considera que el compartimento por defecto se especifica en NoSQLHandleConfig al obtener el identificador de NoSQL. Consulte Obtención de un identificador de NoSQL. Para explorar otras opciones para especificar un compartimento para las tablas NoSQL, consulte Acerca de los compartimentos.

Para crear una tabla e índice con TableRequest y sus métodos:

/* Create a simple table with an integer key and a single json data
 * field  and set your desired table capacity.
 * Set the table TTL value to 3 days.
 */
String createTableDDL = "CREATE TABLE IF NOT EXISTS users " +
 "(id INTEGER, name STRING, " +
 "PRIMARY KEY(id)) USING TTL 3 days";

TableLimits limits = new TableLimits(200, 100, 5);
TableRequest treq = new TableRequest().setStatement(createTableDDL)
 .setTableLimits(limits);

// start the asynchronous operation
TableResult tres = handle.tableRequest(treq);

// wait for completion of the operation
tres.waitForCompletion(handle,
 60000, // wait for 60 sec
 1000); // delay in ms for poll
 

// Create an index called nameIdx on the name field in the users table.
treq = new TableRequest().setStatement("CREATE INDEX 
  IF NOT EXISTS nameIdx ON users(name)
  ");

// start the asynchronous operation
  handle.tableRequest(treq);

// wait for completion of the operation
  tres.waitForCompletion(handle,
   60000, // wait for 60 sec
   1000); // delay in ms for poll

Temas relacionados