Search in sources :

Example 21 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class ConnectionStore method clear.

// clean up all connections
@VisibleForTesting
void clear() {
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = context.getTable(TABLE_ID);
        table.deleteAll(Range.all());
    });
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 22 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class ConnectionStore method saveConnection.

/**
 * Save the connection in the store.
 *
 * @param connectionId the connection id
 * @param connection the connection information
 * @param overWrite flag indicating whether the store should overwrite an existing connection with same connection id
 *                  but different connection name, i.e, a b and a.b both convert to id a_b
 */
public void saveConnection(ConnectionId connectionId, Connection connection, boolean overWrite) {
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = context.getTable(TABLE_ID);
        Connection oldConnection = getConnectionInternal(table, connectionId, false);
        Connection newConnection = connection;
        if (oldConnection != null) {
            if (oldConnection.isPreConfigured()) {
                throw new ConnectionConflictException(String.format("Connection %s in namespace %s has same id %s and is pre-configured. " + "Preconfigured connections cannot be updated or overwritten.", oldConnection.getName(), connectionId.getNamespace(), connectionId.getConnectionId()));
            }
            if (!oldConnection.getName().equals(newConnection.getName()) && !overWrite) {
                throw new ConnectionConflictException(String.format("Connection %s in namespace %s has same id %s. Please choose a different connection name.", oldConnection.getName(), connectionId.getNamespace(), connectionId.getConnectionId()));
            }
            newConnection = new Connection(connection.getName(), oldConnection.getConnectionId(), connection.getConnectionType(), connection.getDescription(), connection.isPreConfigured(), connection.isDefault(), oldConnection.getCreatedTimeMillis(), connection.getUpdatedTimeMillis(), connection.getPlugin());
        }
        Collection<Field<?>> fields = getConnectionKeys(connectionId);
        fields.add(Fields.longField(CREATED_COL, newConnection.getCreatedTimeMillis()));
        fields.add(Fields.longField(UPDATED_COL, newConnection.getUpdatedTimeMillis()));
        fields.add(Fields.stringField(CONNECTION_DATA_FIELD, GSON.toJson(newConnection)));
        table.upsert(fields);
    });
}
Also used : ConnectionConflictException(io.cdap.cdap.etl.proto.connection.ConnectionConflictException) Field(io.cdap.cdap.spi.data.table.field.Field) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Connection(io.cdap.cdap.etl.proto.connection.Connection)

Example 23 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class DraftStore method deleteDraft.

/**
 * Delete the given draft. This is a no-op if the draft does not exist
 *
 * @param id {@link DraftId} that is used to uniquely identify a draft
 * @throws TableNotFoundException if the draft store table is not found
 * @throws InvalidFieldException if the fields in the {@link Draft} object do not match the fields in the
 *   StructuredTable
 */
public void deleteDraft(DraftId id) throws TableNotFoundException, InvalidFieldException {
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = context.getTable(TABLE_ID);
        table.delete(getKey(id));
    }, TableNotFoundException.class, InvalidFieldException.class);
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable)

Example 24 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class DatasetBasedTimeScheduleStore method persistChangeOfState.

private void persistChangeOfState(final TriggerKey triggerKey, final Trigger.TriggerState newTriggerState) {
    try {
        Preconditions.checkNotNull(triggerKey);
        TransactionRunners.run(transactionRunner, context -> {
            StructuredTable table = getTimeScheduleStructuredTable(context);
            TriggerStatusV2 storedTriggerStatus = readTrigger(table, triggerKey);
            if (storedTriggerStatus != null) {
                // its okay to persist the same trigger back again since during pause/resume
                // operation the trigger does not change. We persist it here with just the new trigger state
                persistTrigger(table, storedTriggerStatus.trigger, newTriggerState);
            } else {
                LOG.warn("Trigger key {} was not found while trying to persist its state to {}.", triggerKey, newTriggerState);
            }
        });
    } catch (Throwable th) {
        throw Throwables.propagate(th);
    }
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable)

Example 25 with StructuredTable

use of io.cdap.cdap.spi.data.StructuredTable in project cdap by caskdata.

the class SystemAppTestBaseTest method testTableOperations.

@Test
public void testTableOperations() throws Exception {
    StructuredTableAdmin tableAdmin = getStructuredTableAdmin();
    StructuredTableId id = new StructuredTableId("t0");
    Assert.assertFalse(tableAdmin.exists(id));
    String keyCol = "key";
    String valCol = "val";
    tableAdmin.create(new StructuredTableSpecification.Builder().withId(id).withFields(new FieldType(keyCol, FieldType.Type.STRING), new FieldType(valCol, FieldType.Type.STRING)).withPrimaryKeys(keyCol).build());
    try {
        TransactionRunner transactionRunner = getTransactionRunner();
        String key = "k0";
        String val = "v0";
        transactionRunner.run(context -> {
            StructuredTable table = context.getTable(id);
            List<Field<?>> fields = new ArrayList<>();
            fields.add(Fields.stringField(keyCol, key));
            Optional<StructuredRow> row = table.read(fields);
            Assert.assertFalse(row.isPresent());
            fields.add(Fields.stringField(valCol, val));
            table.upsert(fields);
        });
        transactionRunner.run(context -> {
            StructuredTable table = context.getTable(id);
            List<Field<?>> keyField = Collections.singletonList(Fields.stringField(keyCol, key));
            Optional<StructuredRow> row = table.read(keyField);
            Assert.assertTrue(row.isPresent());
            Assert.assertEquals(val, row.get().getString(valCol));
        });
    } finally {
        tableAdmin.drop(id);
    }
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredTableAdmin(io.cdap.cdap.spi.data.StructuredTableAdmin) ArrayList(java.util.ArrayList) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) FieldType(io.cdap.cdap.spi.data.table.field.FieldType) Field(io.cdap.cdap.spi.data.table.field.Field) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) StructuredTableSpecification(io.cdap.cdap.spi.data.table.StructuredTableSpecification) Test(org.junit.Test)

Aggregations

StructuredTable (io.cdap.cdap.spi.data.StructuredTable)118 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)60 Field (io.cdap.cdap.spi.data.table.field.Field)44 ArrayList (java.util.ArrayList)34 IOException (java.io.IOException)22 Range (io.cdap.cdap.spi.data.table.field.Range)18 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)14 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)12 HashMap (java.util.HashMap)12 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)10 Map (java.util.Map)10 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)8 PluginNotExistsException (io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException)8 List (java.util.List)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 Gson (com.google.gson.Gson)6 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)6 PluginClass (io.cdap.cdap.api.plugin.PluginClass)6 ArtifactAlreadyExistsException (io.cdap.cdap.common.ArtifactAlreadyExistsException)6 Connection (io.cdap.cdap.etl.proto.connection.Connection)6