use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class ObjectMappedTableDefinition method getDataset.
@Override
public ObjectMappedTableDataset<?> getDataset(DatasetContext datasetContext, DatasetSpecification spec, Map<String, String> arguments, ClassLoader classLoader) throws IOException {
String keyName = ObjectMappedTableProperties.getRowKeyExploreName(spec.getProperties());
DatasetSpecification tableSpec = spec.getSpecification(TABLE_NAME);
// TODO: remove after CDAP-2122 is done
if (!tableSpec.getProperties().containsKey(Table.PROPERTY_SCHEMA)) {
tableSpec = DatasetSpecification.builder(tableSpec.getName(), tableSpec.getType()).properties(tableSpec.getProperties()).property(Table.PROPERTY_SCHEMA, spec.getProperty(Table.PROPERTY_SCHEMA)).property(Table.PROPERTY_SCHEMA_ROW_FIELD, keyName).datasets(tableSpec.getSpecifications().values()).build();
}
// reconstruct the table schema here because of backwards compatibility
DatasetDefinition<Table, DatasetAdmin> tableDef = getDelegate(TABLE_NAME);
Table table = tableDef.getDataset(datasetContext, tableSpec, arguments, classLoader);
Map<String, String> properties = spec.getProperties();
TypeRepresentation typeRep = GSON.fromJson(ObjectMappedTableProperties.getObjectTypeRepresentation(properties), TypeRepresentation.class);
Schema objSchema = ObjectMappedTableProperties.getObjectSchema(properties);
return new ObjectMappedTableDataset(spec.getName(), table, typeRep, objSchema, classLoader);
}
use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class AbstractDatasetFrameworkTest method testSimpleDataset.
@Test
public void testSimpleDataset() throws Exception {
// Configuring Dataset types
DatasetFramework framework = getFramework();
// system namespace has a module orderedTable-inMemory
Assert.assertTrue(framework.hasSystemType("table"));
// myspace namespace has no modules
Assert.assertFalse(framework.hasType(IN_MEMORY_TYPE));
Assert.assertFalse(framework.hasType(SIMPLE_KV_TYPE));
// add module to namespace 'myspace'
framework.addModule(KEY_VALUE, new SingleTypeModule(SimpleKVTable.class));
// make sure it got added to 'myspace'
Assert.assertTrue(framework.hasType(SIMPLE_KV_TYPE));
// but not to 'system'
Assert.assertFalse(framework.hasSystemType(SimpleKVTable.class.getName()));
Assert.assertFalse(framework.hasInstance(MY_TABLE));
// Creating instance using a type from own namespace
framework.addInstance(SimpleKVTable.class.getName(), MY_TABLE, DatasetProperties.EMPTY);
// verify it got added to the right namespace
Assert.assertTrue(framework.hasInstance(MY_TABLE));
// and not to the system namespace
Assert.assertFalse(framework.hasInstance(NamespaceId.SYSTEM.dataset("my_table")));
// Doing some admin and data ops
DatasetAdmin admin = framework.getAdmin(MY_TABLE, null);
Assert.assertNotNull(admin);
final SimpleKVTable kvTable = framework.getDataset(MY_TABLE, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(kvTable);
TransactionExecutor txnl = new DefaultTransactionExecutor(new MinimalTxSystemClient(), (TransactionAware) kvTable);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
kvTable.put("key1", "value1");
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("value1", kvTable.get("key1"));
}
});
admin.truncate();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertTrue(kvTable.get("key1") == null);
}
});
// cleanup
framework.deleteInstance(MY_TABLE);
framework.deleteModule(KEY_VALUE);
// recreate instance without adding a module in 'myspace'. This should use types from default namespace
framework.addInstance("table", MY_TABLE, DatasetProperties.EMPTY);
// verify it got added to the right namespace
Assert.assertTrue(framework.hasInstance(MY_TABLE));
admin = framework.getAdmin(MY_TABLE, null);
Assert.assertNotNull(admin);
final Table table = framework.getDataset(MY_TABLE, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(table);
txnl = new DefaultTransactionExecutor(new MinimalTxSystemClient(), (TransactionAware) table);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table.put(Bytes.toBytes("key1"), Bytes.toBytes("column1"), Bytes.toBytes("value1"));
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("value1", Bytes.toString(table.get(Bytes.toBytes("key1"), Bytes.toBytes("column1"))));
}
});
// cleanup
framework.deleteInstance(MY_TABLE);
}
use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class AbstractDatasetFrameworkTest method testCompositeDataset.
private void testCompositeDataset(DatasetFramework framework) throws Exception {
// Doing some admin and data ops
DatasetAdmin admin = framework.getAdmin(MY_TABLE, null);
Assert.assertNotNull(admin);
final KeyValueTable table = framework.getDataset(MY_TABLE, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(table);
TransactionExecutor txnl = new DefaultTransactionExecutor(new MinimalTxSystemClient(), (TransactionAware) table);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table.put("key1", "value1");
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("value1", table.get("key1"));
}
});
admin.truncate();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals(null, table.get("key1"));
}
});
}
use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class TestDatasetModule method register.
@Override
public void register(DatasetDefinitionRegistry registry) {
DatasetDefinition<KeyValueTable, DatasetAdmin> kvTableDef = registry.get("keyValueTable");
DatasetDefinition definition = new TestDatasetDefinition("testDataset", kvTableDef);
registry.add(definition);
}
use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class NoTxKeyValueTableTest method test.
@Test
public void test() throws IOException {
DatasetDefinition<? extends NoTxKeyValueTable, ? extends DatasetAdmin> def = getDefinition();
DatasetSpecification spec = def.configure("table", DatasetProperties.EMPTY);
ClassLoader cl = NoTxKeyValueTable.class.getClassLoader();
DatasetContext datasetContext = DatasetContext.from(NAMESPACE_ID.getEntityName());
// create & exists
DatasetAdmin admin = def.getAdmin(datasetContext, spec, cl);
Assert.assertFalse(admin.exists());
admin.create();
Assert.assertTrue(admin.exists());
// put/get
NoTxKeyValueTable table = def.getDataset(datasetContext, spec, NO_ARGS, cl);
Assert.assertNull(table.get(KEY1));
table.put(KEY1, VALUE1);
Assert.assertArrayEquals(VALUE1, table.get(KEY1));
Assert.assertNull(table.get(KEY2));
// override
table.put(KEY1, VALUE2);
Assert.assertArrayEquals(VALUE2, table.get(KEY1));
Assert.assertNull(table.get(KEY2));
// delete & truncate
table.put(KEY2, VALUE1);
Assert.assertArrayEquals(VALUE2, table.get(KEY1));
Assert.assertArrayEquals(VALUE1, table.get(KEY2));
table.put(KEY2, null);
Assert.assertNull(table.get(KEY2));
Assert.assertArrayEquals(VALUE2, table.get(KEY1));
admin.truncate();
Assert.assertNull(table.get(KEY1));
Assert.assertNull(table.get(KEY2));
Assert.assertTrue(admin.exists());
admin.drop();
Assert.assertFalse(admin.exists());
// drop should cleanup data
admin.create();
Assert.assertTrue(admin.exists());
Assert.assertNull(table.get(KEY1));
Assert.assertNull(table.get(KEY2));
table.put(KEY1, VALUE1);
Assert.assertArrayEquals(VALUE1, table.get(KEY1));
admin.drop();
Assert.assertFalse(admin.exists());
admin.create();
Assert.assertTrue(admin.exists());
Assert.assertNull(table.get(KEY1));
}
Aggregations