Criando Tabelas e Índices
Saiba como criar tabelas e índices.
A criação de uma tabela é a primeira etapa para desenvolver seu aplicativo. Você usa a classe
TableRequest
e os métodos para executar todas as instruções DDL, como criar, modificar e eliminar tabelas.Você também define limites de tabela usando o método TableRequest.setTableLimits
.
Antes de criar uma tabela, consulte:
A classe TableRequest
permite passar uma instrução DDL para o método TableRequest.setStatement
. Exemplos de instruções DDL são:
/* 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);
Observação
O exemplo a seguir considera que o compartimento padrão seja especificado em
O exemplo a seguir considera que o compartimento padrão seja especificado em
NoSQLHandleConfig
durante a obtenção do NoSQL. Obtendo um NoSQL Handle. Para explorar outras opções de especificação de um compartimento para as tabelas NoSQL, consulte Sobre Compartimentos.
Para criar uma tabela e um índice usando TableRequest
e seus 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
Tópicos Relacionados