Search in sources :

Example 1 with Dataset

use of io.cdap.cdap.api.dataset.Dataset in project cdap by caskdata.

the class EntityTableDatasetContext method close.

@Override
public void close() throws Exception {
    Exception ex = null;
    for (Dataset entry : entityTables.values()) {
        try {
            entry.close();
        } catch (IOException e) {
            if (ex == null) {
                ex = e;
            } else {
                ex.addSuppressed(e);
            }
        }
    }
    entityTables.clear();
    if (ex != null) {
        throw ex;
    }
}
Also used : Dataset(io.cdap.cdap.api.dataset.Dataset) IOException(java.io.IOException) DatasetInstantiationException(io.cdap.cdap.api.data.DatasetInstantiationException) IOException(java.io.IOException)

Example 2 with Dataset

use of io.cdap.cdap.api.dataset.Dataset in project cdap by caskdata.

the class ExternalDatasets method makeTrackable.

/**
 * If the input is an external source then an external dataset is created for tracking purpose and returned.
 * If the input is a regular dataset or a stream then it is already trackable, hence same input is returned.
 *
 * @param admin {@link Admin} used to create external dataset
 * @param input input to be tracked
 * @return an external dataset if input is an external source, otherwise the same input that is passed-in is returned
 */
public static Input makeTrackable(Admin admin, Input input) {
    // If input is not an external source, return the same input as it can be tracked by itself.
    if (!(input instanceof Input.InputFormatProviderInput)) {
        return input;
    }
    // Input is an external source, create an external dataset so that it can be tracked.
    String inputName = input.getName();
    InputFormatProvider inputFormatProvider = ((Input.InputFormatProviderInput) input).getInputFormatProvider();
    Map<String, String> inputFormatConfiguration = inputFormatProvider.getInputFormatConfiguration();
    // this too can be tracked by itself without creating an external dataset
    if (inputFormatProvider instanceof Dataset) {
        return input;
    }
    try {
        // Create an external dataset for the input format for lineage tracking
        Map<String, String> arguments = new HashMap<>();
        arguments.put("input.format.class", inputFormatProvider.getInputFormatClassName());
        arguments.putAll(inputFormatConfiguration);
        if (!admin.datasetExists(inputName)) {
            // Note: the dataset properties are the same as the arguments since we cannot identify them separately
            // since they are mixed up in a single configuration object (CDAP-5674)
            // Also, the properties of the external dataset created will contain runtime arguments for the same reason.
            admin.createDataset(inputName, EXTERNAL_DATASET_TYPE, DatasetProperties.of(arguments));
        } else {
            // Check if the external dataset name clashes with an existing CDAP Dataset
            String datasetType = admin.getDatasetType(inputName);
            if (!EXTERNAL_DATASET_TYPE.equals(datasetType)) {
                throw new IllegalArgumentException("An external source cannot have the same name as an existing CDAP Dataset instance " + inputName);
            }
        }
        return Input.ofDataset(inputName, Collections.unmodifiableMap(arguments)).alias(input.getAlias());
    } catch (DatasetManagementException e) {
        throw Throwables.propagate(e);
    }
}
Also used : DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) InputFormatProvider(io.cdap.cdap.api.data.batch.InputFormatProvider) HashMap(java.util.HashMap) Dataset(io.cdap.cdap.api.dataset.Dataset)

Example 3 with Dataset

use of io.cdap.cdap.api.dataset.Dataset in project cdap by caskdata.

the class ExternalDatasets method makeTrackable.

/**
 * If the output is an external sink then an external dataset is created for tracking purpose and returned.
 * If the output is a regular dataset then it is already trackable, hence same output is returned.
 *
 * @param admin {@link Admin} used to create external dataset
 * @param output output to be tracked
 * @return an external dataset if output is an external sink, otherwise the same output is returned
 */
public static Output makeTrackable(Admin admin, Output output) {
    // If output is not an external sink, return the same output as it can be tracked by itself.
    if (!(output instanceof Output.OutputFormatProviderOutput)) {
        return output;
    }
    // Output is an external sink, create an external dataset so that it can be tracked.
    String outputName = output.getName();
    OutputFormatProvider outputFormatProvider = ((Output.OutputFormatProviderOutput) output).getOutputFormatProvider();
    Map<String, String> outputFormatConfiguration = outputFormatProvider.getOutputFormatConfiguration();
    // this can be tracked by itself without creating an external dataset
    if (outputFormatProvider instanceof Dataset) {
        return output;
    }
    // Output is an external sink, create an external dataset so that it can be tracked.
    try {
        // Create an external dataset for the output format for lineage tracking
        Map<String, String> arguments = new HashMap<>();
        arguments.put("output.format.class", outputFormatProvider.getOutputFormatClassName());
        arguments.putAll(outputFormatConfiguration);
        if (!admin.datasetExists(outputName)) {
            // Note: the dataset properties are the same as the arguments since we cannot identify them separately
            // since they are mixed up in a single configuration object (CDAP-5674)
            // Also, the properties of the external dataset created will contain runtime arguments for the same reason.
            admin.createDataset(outputName, EXTERNAL_DATASET_TYPE, DatasetProperties.of(arguments));
        } else {
            // Check if the external dataset name clashes with an existing CDAP Dataset
            String datasetType = admin.getDatasetType(outputName);
            if (!EXTERNAL_DATASET_TYPE.equals(datasetType)) {
                throw new IllegalArgumentException("An external sink cannot have the same name as an existing CDAP Dataset instance " + outputName);
            }
        }
        return Output.ofDataset(outputName, Collections.unmodifiableMap(arguments)).alias(output.getAlias());
    } catch (DatasetManagementException e) {
        throw Throwables.propagate(e);
    }
}
Also used : DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) HashMap(java.util.HashMap) Dataset(io.cdap.cdap.api.dataset.Dataset) OutputFormatProvider(io.cdap.cdap.api.data.batch.OutputFormatProvider)

Example 4 with Dataset

use of io.cdap.cdap.api.dataset.Dataset in project cdap by caskdata.

the class ExploreTableManager method generateDisableStatement.

private String generateDisableStatement(DatasetId datasetId, DatasetSpecification spec) throws ExploreException {
    String tableName = tableNaming.getTableName(datasetId, spec.getProperties());
    String databaseName = ExploreProperties.getExploreDatabaseName(spec.getProperties());
    // If table does not exist, nothing to be done
    try {
        exploreService.getTableInfo(datasetId.getNamespace(), databaseName, tableName);
    } catch (TableNotFoundException e) {
        // Ignore exception, since this means table was not found.
        return null;
    }
    try (SystemDatasetInstantiator datasetInstantiator = datasetInstantiatorFactory.create()) {
        Dataset dataset = datasetInstantiator.getDataset(datasetId);
        try {
            if (dataset instanceof FileSet || dataset instanceof PartitionedFileSet) {
                // do not drop the explore table that dataset is reusing an existing table
                if (FileSetProperties.isUseExisting(spec.getProperties())) {
                    return null;
                }
            }
            return generateDeleteStatement(dataset, databaseName, tableName);
        } finally {
            Closeables.closeQuietly(dataset);
        }
    } catch (IOException e) {
        LOG.error("Exception creating dataset classLoaderProvider for dataset {}.", datasetId, e);
        throw new ExploreException("Exception instantiating dataset " + datasetId);
    }
}
Also used : PartitionedFileSet(io.cdap.cdap.api.dataset.lib.PartitionedFileSet) FileSet(io.cdap.cdap.api.dataset.lib.FileSet) SystemDatasetInstantiator(io.cdap.cdap.data.dataset.SystemDatasetInstantiator) Dataset(io.cdap.cdap.api.dataset.Dataset) PartitionedFileSet(io.cdap.cdap.api.dataset.lib.PartitionedFileSet) IOException(java.io.IOException)

Example 5 with Dataset

use of io.cdap.cdap.api.dataset.Dataset in project cdap by caskdata.

the class ExploreTableManager method updateDataset.

/**
 * Update ad-hoc exploration on the given dataset by altering the corresponding Hive table. If exploration has
 * not been enabled on the dataset, this will fail. Assumes the dataset actually exists.
 *
 * @param datasetId the ID of the dataset to enable
 * @param spec the specification for the dataset to enable
 * @return query handle for creating the Hive table for the dataset
 * @throws IllegalArgumentException if some required dataset property like schema is not set
 * @throws UnsupportedTypeException if the schema of the dataset is not compatible with Hive
 * @throws ExploreException if there was an exception submitting the create table statement
 * @throws SQLException if there was a problem with the create table statement
 * @throws DatasetNotFoundException if the dataset had to be instantiated, but could not be found
 * @throws ClassNotFoundException if the was a missing class when instantiating the dataset
 */
public QueryHandle updateDataset(DatasetId datasetId, DatasetSpecification spec, DatasetSpecification oldSpec) throws IllegalArgumentException, ExploreException, SQLException, UnsupportedTypeException, DatasetNotFoundException, ClassNotFoundException {
    String tableName = tableNaming.getTableName(datasetId, spec.getProperties());
    String databaseName = ExploreProperties.getExploreDatabaseName(spec.getProperties());
    String oldTableName = tableNaming.getTableName(datasetId, oldSpec.getProperties());
    String oldDatabaseName = ExploreProperties.getExploreDatabaseName(oldSpec.getProperties());
    try {
        exploreService.getTableInfo(datasetId.getNamespace(), oldDatabaseName, oldTableName);
    } catch (TableNotFoundException e) {
        // but the new spec may be explorable, so attempt to enable it
        return enableDataset(datasetId, spec, false);
    }
    List<String> alterStatements;
    if (!(oldTableName.equals(tableName) && Objects.equals(oldDatabaseName, databaseName))) {
        alterStatements = new ArrayList<>();
        // database/table name changed. All we can do is disable the old table and enable the new one
        String disableStatement = generateDisableStatement(datasetId, oldSpec);
        if (disableStatement != null) {
            alterStatements.add(disableStatement);
        }
        String enableStatement = generateEnableStatement(datasetId, spec, false);
        if (enableStatement != null) {
            alterStatements.add(enableStatement);
        }
    } else {
        try (SystemDatasetInstantiator datasetInstantiator = datasetInstantiatorFactory.create()) {
            Dataset dataset = datasetInstantiator.getDataset(datasetId);
            try {
                alterStatements = generateAlterStatements(datasetId, tableName, dataset, spec, oldSpec);
            } finally {
                Closeables.closeQuietly(dataset);
            }
        } catch (IOException e) {
            LOG.error("Exception instantiating dataset {}.", datasetId, e);
            throw new ExploreException("Exception while trying to instantiate dataset " + datasetId);
        }
    }
    LOG.trace("alter statements for update: {}", alterStatements);
    if (alterStatements == null || alterStatements.isEmpty()) {
        return QueryHandle.NO_OP;
    }
    if (alterStatements.size() == 1) {
        return exploreService.execute(datasetId.getParent(), alterStatements.get(0));
    }
    return exploreService.execute(datasetId.getParent(), alterStatements.toArray(new String[alterStatements.size()]));
}
Also used : SystemDatasetInstantiator(io.cdap.cdap.data.dataset.SystemDatasetInstantiator) Dataset(io.cdap.cdap.api.dataset.Dataset) IOException(java.io.IOException)

Aggregations

Dataset (io.cdap.cdap.api.dataset.Dataset)20 IOException (java.io.IOException)10 DatasetInstantiationException (io.cdap.cdap.api.data.DatasetInstantiationException)5 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)5 SystemDatasetInstantiator (io.cdap.cdap.data.dataset.SystemDatasetInstantiator)4 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)4 UnsupportedTypeException (io.cdap.cdap.api.data.schema.UnsupportedTypeException)2 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)2 PartitionedFileSet (io.cdap.cdap.api.dataset.lib.PartitionedFileSet)2 MeteredDataset (io.cdap.cdap.api.dataset.metrics.MeteredDataset)2 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)2 BadRequestException (io.cdap.cdap.common.BadRequestException)2 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)2 CustomDatasetApp (io.cdap.cdap.data2.dataset2.customds.CustomDatasetApp)2 CustomOperations (io.cdap.cdap.data2.dataset2.customds.CustomOperations)2 DefaultTopLevelExtendsDataset (io.cdap.cdap.data2.dataset2.customds.DefaultTopLevelExtendsDataset)2 DelegatingDataset (io.cdap.cdap.data2.dataset2.customds.DelegatingDataset)2 TopLevelDataset (io.cdap.cdap.data2.dataset2.customds.TopLevelDataset)2 TopLevelDirectDataset (io.cdap.cdap.data2.dataset2.customds.TopLevelDirectDataset)2 TopLevelExtendsDataset (io.cdap.cdap.data2.dataset2.customds.TopLevelExtendsDataset)2