Search in sources :

Example 21 with DatasetAdmin

use of co.cask.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);
}
Also used : KeyValueTable(co.cask.cdap.api.dataset.lib.KeyValueTable) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) DatasetDefinition(co.cask.cdap.api.dataset.DatasetDefinition)

Example 22 with DatasetAdmin

use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.

the class IntegrationTestBaseTest method testSQLQuery.

@Test
public void testSQLQuery() throws Exception {
    getTestManager().deployDatasetModule(NamespaceId.DEFAULT.datasetModule("my-kv"), AppUsingCustomModule.Module.class);
    DatasetAdmin dsAdmin = getTestManager().addDatasetInstance("myKeyValueTable", NamespaceId.DEFAULT.dataset("myTable"));
    Assert.assertTrue(dsAdmin.exists());
    ApplicationManager appManager = deployApplication(NamespaceId.DEFAULT, AppUsingCustomModule.class);
    ServiceManager serviceManager = appManager.getServiceManager("MyService").start();
    serviceManager.waitForStatus(true);
    put(serviceManager, "a", "1");
    put(serviceManager, "b", "2");
    put(serviceManager, "c", "1");
    try (Connection connection = getTestManager().getQueryClient(NamespaceId.DEFAULT);
        // the value (character) "1" corresponds to the decimal 49. In hex, that is 31.
        ResultSet results = connection.prepareStatement("select key from dataset_mytable where hex(value) = '31'").executeQuery()) {
        // run a query over the dataset
        Assert.assertTrue(results.next());
        Assert.assertEquals("a", results.getString(1));
        Assert.assertTrue(results.next());
        Assert.assertEquals("c", results.getString(1));
        Assert.assertFalse(results.next());
    }
    dsAdmin.drop();
    Assert.assertFalse(dsAdmin.exists());
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) Test(org.junit.Test)

Example 23 with DatasetAdmin

use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.

the class HBaseTableTest method testColumnFamily.

@Test
public void testColumnFamily() throws Exception {
    DatasetProperties props = TableProperties.builder().setColumnFamily("t").build();
    String tableName = "testcf";
    DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName, props);
    admin.create();
    final BufferingTable table = getTable(CONTEXT1, tableName, props);
    TransactionSystemClient txClient = new DetachedTxSystemClient();
    TransactionExecutor executor = new DefaultTransactionExecutor(txClient, table);
    executor.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            table.put(new Put("row", "column", "testValue"));
        }
    });
    final BufferingTable table2 = getTable(CONTEXT1, tableName, props);
    executor = new DefaultTransactionExecutor(txClient, table2);
    executor.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            Assert.assertEquals("testValue", table2.get(new Get("row", "column")).getString("column"));
        }
    });
    // Verify the column family name
    TableId hTableId = hBaseTableUtil.createHTableId(new NamespaceId(CONTEXT1.getNamespaceId()), tableName);
    HTableDescriptor htd = hBaseTableUtil.getHTableDescriptor(TEST_HBASE.getHBaseAdmin(), hTableId);
    HColumnDescriptor hcd = htd.getFamily(Bytes.toBytes("t"));
    Assert.assertNotNull(hcd);
    Assert.assertEquals("t", hcd.getNameAsString());
}
Also used : TableId(co.cask.cdap.data2.util.TableId) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) TransactionExecutor(org.apache.tephra.TransactionExecutor) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) IOException(java.io.IOException) Put(co.cask.cdap.api.dataset.table.Put) BufferingTable(co.cask.cdap.data2.dataset2.lib.table.BufferingTable) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) Get(co.cask.cdap.api.dataset.table.Get) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) DetachedTxSystemClient(org.apache.tephra.inmemory.DetachedTxSystemClient) NamespaceId(co.cask.cdap.proto.id.NamespaceId) BufferingTableTest(co.cask.cdap.data2.dataset2.lib.table.BufferingTableTest) Test(org.junit.Test)

Example 24 with DatasetAdmin

use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.

the class MetricHBaseTableUtilTest method testGetVersion.

@Test
public void testGetVersion() throws Exception {
    // Verify new metric datasets are properly recognized as 2.8+ version from now on
    HBaseMetricsTableDefinition definition = new HBaseMetricsTableDefinition("foo", TEST_HBASE.getConfiguration(), hBaseTableUtil, new LocalLocationFactory(TMP_FOLDER.newFolder()), cConf);
    DatasetSpecification spec = definition.configure("metricV2.8", DatasetProperties.EMPTY);
    DatasetAdmin admin = definition.getAdmin(DatasetContext.from(NamespaceId.SYSTEM.getNamespace()), spec, null);
    admin.create();
    MetricHBaseTableUtil util = new MetricHBaseTableUtil(hBaseTableUtil);
    HBaseAdmin hAdmin = TEST_HBASE.getHBaseAdmin();
    TableId hTableId = hBaseTableUtil.createHTableId(NamespaceId.SYSTEM, spec.getName());
    HTableDescriptor desc = hBaseTableUtil.getHTableDescriptor(hAdmin, hTableId);
    Assert.assertEquals(MetricHBaseTableUtil.Version.VERSION_2_8_OR_HIGHER, util.getVersion(desc));
    // Verify HBase table without coprocessor is properly recognized as 2.6- version
    TableName table26 = TableName.valueOf("metricV2.6");
    hAdmin.createTable(new HTableDescriptor(table26));
    desc = hAdmin.getTableDescriptor(table26);
    Assert.assertEquals(MetricHBaseTableUtil.Version.VERSION_2_6_OR_LOWER, util.getVersion(desc));
    // Verify HBase table with IncrementHandler coprocessor but without cdap.version on it is properly recognized as
    // 2.7 version
    TableName table27 = TableName.valueOf("metricV2.7");
    desc = new HTableDescriptor(table27);
    desc.addCoprocessor(hBaseTableUtil.getIncrementHandlerClassForVersion().getName());
    hAdmin.createTable(desc);
    desc = hAdmin.getTableDescriptor(table27);
    Assert.assertEquals(MetricHBaseTableUtil.Version.VERSION_2_7, util.getVersion(desc));
}
Also used : TableId(co.cask.cdap.data2.util.TableId) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) TableName(org.apache.hadoop.hbase.TableName) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) Test(org.junit.Test)

Example 25 with DatasetAdmin

use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.

the class DatasetAdminService method createOrUpdate.

/**
   * Configures and creates a Dataset
   *
   * @param datasetInstanceId dataset instance to be created
   * @param typeMeta type meta for the dataset
   * @param props dataset instance properties
   * @param existing if dataset already exists (in case of update), the existing properties
   * @return dataset specification
   */
public DatasetSpecification createOrUpdate(final DatasetId datasetInstanceId, final DatasetTypeMeta typeMeta, final DatasetProperties props, @Nullable final DatasetSpecification existing) throws Exception {
    if (existing == null) {
        LOG.info("Creating dataset instance {}, type meta: {}", datasetInstanceId, typeMeta);
    } else {
        LOG.info("Updating dataset instance {}, type meta: {}, existing: {}", datasetInstanceId, typeMeta, existing);
    }
    try (DatasetClassLoaderProvider classLoaderProvider = new DirectoryClassLoaderProvider(cConf, locationFactory)) {
        final DatasetContext context = DatasetContext.from(datasetInstanceId.getNamespace());
        UserGroupInformation ugi = getUgiForDataset(impersonator, datasetInstanceId);
        final DatasetType type = ImpersonationUtils.doAs(ugi, new Callable<DatasetType>() {

            @Override
            public DatasetType call() throws Exception {
                DatasetType type = dsFramework.getDatasetType(typeMeta, null, classLoaderProvider);
                if (type == null) {
                    throw new BadRequestException(String.format("Cannot instantiate dataset type using provided type meta: %s", typeMeta));
                }
                return type;
            }
        });
        DatasetSpecification spec = ImpersonationUtils.doAs(ugi, new Callable<DatasetSpecification>() {

            @Override
            public DatasetSpecification call() throws Exception {
                DatasetSpecification spec = existing == null ? type.configure(datasetInstanceId.getEntityName(), props) : type.reconfigure(datasetInstanceId.getEntityName(), props, existing);
                DatasetAdmin admin = type.getAdmin(context, spec);
                if (existing != null) {
                    if (admin instanceof Updatable) {
                        ((Updatable) admin).update(existing);
                    } else {
                        admin.upgrade();
                    }
                } else {
                    admin.create();
                }
                return spec;
            }
        });
        // Writing system metadata should be done without impersonation since user may not have access to system tables.
        writeSystemMetadata(datasetInstanceId, spec, props, typeMeta, type, context, existing != null, ugi);
        return spec;
    } catch (Exception e) {
        if (e instanceof IncompatibleUpdateException) {
            // this is expected to happen if user provides bad update properties, so we log this as debug
            LOG.debug("Incompatible update for dataset '{}'", datasetInstanceId, e);
        } else {
            LOG.error("Error {} dataset '{}': {}", existing == null ? "creating" : "updating", datasetInstanceId, e.getMessage(), e);
        }
        throw e;
    }
}
Also used : DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) DatasetType(co.cask.cdap.data2.datafabric.dataset.DatasetType) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) DirectoryClassLoaderProvider(co.cask.cdap.data2.datafabric.dataset.type.DirectoryClassLoaderProvider) Updatable(co.cask.cdap.api.dataset.Updatable) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetClassLoaderProvider(co.cask.cdap.data2.datafabric.dataset.type.DatasetClassLoaderProvider) DatasetContext(co.cask.cdap.api.dataset.DatasetContext) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException)

Aggregations

DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)50 Test (org.junit.Test)27 Table (co.cask.cdap.api.dataset.table.Table)25 Transaction (org.apache.tephra.Transaction)25 TransactionAware (org.apache.tephra.TransactionAware)22 HBaseTable (co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable)20 DatasetProperties (co.cask.cdap.api.dataset.DatasetProperties)8 DatasetSpecification (co.cask.cdap.api.dataset.DatasetSpecification)8 Get (co.cask.cdap.api.dataset.table.Get)8 IOException (java.io.IOException)8 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)6 Put (co.cask.cdap.api.dataset.table.Put)6 Row (co.cask.cdap.api.dataset.table.Row)6 DatasetType (co.cask.cdap.data2.datafabric.dataset.DatasetType)5 IncompatibleUpdateException (co.cask.cdap.api.dataset.IncompatibleUpdateException)4 Updatable (co.cask.cdap.api.dataset.Updatable)4 Scan (co.cask.cdap.api.dataset.table.Scan)4 DatasetDefinition (co.cask.cdap.api.dataset.DatasetDefinition)3 InstanceConflictException (co.cask.cdap.api.dataset.InstanceConflictException)3 ConstantClassLoaderProvider (co.cask.cdap.data2.datafabric.dataset.type.ConstantClassLoaderProvider)3