Search in sources :

Example 1 with StructuredTableId

use of io.cdap.cdap.spi.data.table.StructuredTableId in project cdap by caskdata.

the class SqlStructuredTableRegistry method registerSpecification.

@Override
public void registerSpecification(StructuredTableSpecification specification) throws TableAlreadyExistsException {
    initIfNeeded();
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable registry = context.getTable(REGISTRY);
        StructuredTableId tableId = specification.getTableId();
        Optional<StructuredRow> optional = registry.read(Collections.singleton(Fields.stringField(TABLE_NAME_FIELD, tableId.getName())));
        if (optional.isPresent()) {
            throw new TableAlreadyExistsException(tableId);
        }
        LOG.debug("Registering table specification {}", specification);
        registry.upsert(Arrays.asList(Fields.stringField(TABLE_NAME_FIELD, tableId.getName()), Fields.stringField(TABLE_SPEC_FIELD, GSON.toJson(specification))));
    }, TableAlreadyExistsException.class);
}
Also used : TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) StructuredRow(io.cdap.cdap.spi.data.StructuredRow)

Example 2 with StructuredTableId

use of io.cdap.cdap.spi.data.table.StructuredTableId in project cdap by caskdata.

the class NoSqlStructuredTableRegistry method registerSpecification.

@Override
public void registerSpecification(StructuredTableSpecification specification) throws TableAlreadyExistsException {
    LOG.debug("Registering table specification {}", specification);
    StructuredTableId tableId = specification.getTableId();
    MetricsTable table = getRegistryTable();
    try {
        byte[] rowKeyBytes = getRowKeyBytes(tableId);
        byte[] serialized = table.get(rowKeyBytes, SCHEMA_COL_BYTES);
        if (serialized != null) {
            throw new TableAlreadyExistsException(tableId);
        }
        serialized = Bytes.toBytes(GSON.toJson(specification));
        if (!table.swap(rowKeyBytes, SCHEMA_COL_BYTES, null, serialized)) {
            throw new TableAlreadyExistsException(tableId);
        }
    } finally {
        closeRegistryTable(table);
    }
}
Also used : TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) MetricsTable(io.cdap.cdap.data2.dataset2.lib.table.MetricsTable) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId)

Example 3 with StructuredTableId

use of io.cdap.cdap.spi.data.table.StructuredTableId 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)

Example 4 with StructuredTableId

use of io.cdap.cdap.spi.data.table.StructuredTableId in project cdap by caskdata.

the class TransactionRunnersTest method testTwoExceptionCheckedPropagation.

@Test
public void testTwoExceptionCheckedPropagation() throws Exception {
    try {
        TransactionRunners.run(MOCK, (TxRunnable) context -> {
            throw new TableAlreadyExistsException(new StructuredTableId("id"));
        }, TableNotFoundException.class, TableAlreadyExistsException.class);
        Assert.fail("runnable should have thrown an exception");
    } catch (TableAlreadyExistsException e) {
    // expected
    }
    try {
        TransactionRunners.run(MOCK, (TxCallable<Object>) context -> {
            throw new TableAlreadyExistsException(new StructuredTableId("id"));
        }, TableNotFoundException.class, TableAlreadyExistsException.class);
        Assert.fail("runnable should have thrown an exception");
    } catch (TableAlreadyExistsException e) {
    // expected
    }
}
Also used : StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) Test(org.junit.Test) IOException(java.io.IOException) Assert(org.junit.Assert) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) Test(org.junit.Test)

Example 5 with StructuredTableId

use of io.cdap.cdap.spi.data.table.StructuredTableId in project cdap by caskdata.

the class TransactionRunnersTest method testSingleExceptionCheckedPropagation.

@Test
public void testSingleExceptionCheckedPropagation() {
    try {
        TransactionRunners.run(MOCK, (TxRunnable) context -> {
            throw new TableNotFoundException(new StructuredTableId("id"));
        }, TableNotFoundException.class);
        Assert.fail("runnable should have thrown an exception");
    } catch (TableNotFoundException e) {
    // expected
    }
    try {
        TransactionRunners.run(MOCK, (TxCallable<Object>) context -> {
            throw new TableNotFoundException(new StructuredTableId("id"));
        }, TableNotFoundException.class);
        Assert.fail("runnable should have thrown an exception");
    } catch (TableNotFoundException e) {
    // expected
    }
}
Also used : StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) Test(org.junit.Test) IOException(java.io.IOException) Assert(org.junit.Assert) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) Test(org.junit.Test)

Aggregations

StructuredTableId (io.cdap.cdap.spi.data.table.StructuredTableId)7 TableAlreadyExistsException (io.cdap.cdap.spi.data.TableAlreadyExistsException)6 IOException (java.io.IOException)3 Test (org.junit.Test)3 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)2 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)2 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)2 StructuredTableSpecification (io.cdap.cdap.spi.data.table.StructuredTableSpecification)2 Assert (org.junit.Assert)2 MetricsTable (io.cdap.cdap.data2.dataset2.lib.table.MetricsTable)1 StructuredTableAdmin (io.cdap.cdap.spi.data.StructuredTableAdmin)1 StructuredTableRegistry (io.cdap.cdap.spi.data.common.StructuredTableRegistry)1 Field (io.cdap.cdap.spi.data.table.field.Field)1 FieldType (io.cdap.cdap.spi.data.table.field.FieldType)1 TransactionRunner (io.cdap.cdap.spi.data.transaction.TransactionRunner)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1