Search in sources :

Example 1 with DerivedDatasource

use of com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource in project kylo by Teradata.

the class HiveColumnsUpgradeAction method upgradeTo.

@Override
public void upgradeTo(final KyloVersion startingVersion) {
    log.info("Upgrading hive columns from version: {}", startingVersion);
    feedService.getFeeds().stream().filter(feed -> Optional.ofNullable(feed.getTable()).map(TableSetup::getTableSchema).map(TableSchema::getFields).isPresent()).forEach(feed -> {
        final TableSchema schema = feed.getTable().getTableSchema();
        final DerivedDatasource datasource = datasourceProvider.findDerivedDatasource("HiveDatasource", feed.getSystemCategoryName() + "." + feed.getSystemFeedName());
        if (datasource != null) {
            log.info("Upgrading schema: {}/{}", schema.getDatabaseName(), schema.getSchemaName());
            datasource.setGenericProperties(Collections.singletonMap("columns", (Serializable) schema.getFields()));
        }
    });
}
Also used : DerivedDatasource(com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource) Logger(org.slf4j.Logger) FeedManagerFeedService(com.thinkbiganalytics.feedmgr.service.feed.FeedManagerFeedService) LoggerFactory(org.slf4j.LoggerFactory) Profile(org.springframework.context.annotation.Profile) Serializable(java.io.Serializable) Inject(javax.inject.Inject) Component(org.springframework.stereotype.Component) TableSchema(com.thinkbiganalytics.discovery.schema.TableSchema) Optional(java.util.Optional) TableSetup(com.thinkbiganalytics.feedmgr.rest.model.schema.TableSetup) KyloVersion(com.thinkbiganalytics.KyloVersion) DatasourceProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider) Collections(java.util.Collections) KyloUpgrader(com.thinkbiganalytics.server.upgrade.KyloUpgrader) UpgradeState(com.thinkbiganalytics.server.upgrade.UpgradeState) Serializable(java.io.Serializable) TableSchema(com.thinkbiganalytics.discovery.schema.TableSchema) TableSetup(com.thinkbiganalytics.feedmgr.rest.model.schema.TableSetup) DerivedDatasource(com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource)

Example 2 with DerivedDatasource

use of com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource in project kylo by Teradata.

the class FeedManagerFeedTest method testFeedDatasource.

@Test
public void testFeedDatasource() {
    String categorySystemName = "my_category";
    String feedName = "my_feed";
    String templateName = "my_template";
    String description = " my feed description";
    setupFeedAndTemplate(categorySystemName, feedName, templateName);
    // boolean isDefineTable = true;
    // boolean isGetFile = false;
    metadata.commit(() -> {
        Feed feed = feedTestUtil.findFeed(categorySystemName, feedName);
        Set<Datasource.ID> sources = new HashSet<Datasource.ID>();
        Set<com.thinkbiganalytics.metadata.api.datasource.Datasource.ID> destinations = new HashSet<>();
        // Add Table Dependencies
        String uniqueName = FeedNameUtil.fullName(categorySystemName, feedName);
        DerivedDatasource srcDatasource = datasourceProvider.ensureDatasource(uniqueName, feed.getDescription(), DerivedDatasource.class);
        sources.add(srcDatasource.getId());
        DerivedDatasource destDatasource = datasourceProvider.ensureDatasource("destination", feed.getDescription(), DerivedDatasource.class);
        destinations.add(destDatasource.getId());
        sources.stream().forEach(sourceId -> feedProvider.ensureFeedSource(feed.getId(), sourceId));
        destinations.stream().forEach(destinationId -> feedProvider.ensureFeedDestination(feed.getId(), destinationId));
    }, MetadataAccess.SERVICE);
    // ensure the sources and dest got created
    metadata.read(() -> {
        Feed feed = feedTestUtil.findFeed(categorySystemName, feedName);
        Assert.assertNotNull(feed.getSources());
        Assert.assertTrue(feed.getSources().size() == 1, "Feed Sources should be 1");
        Assert.assertNotNull(feed.getDestinations());
        Assert.assertTrue(feed.getDestinations().size() == 1, "Feed Destinations should be 1");
        List<? extends FeedDestination> feedDestinations = feed.getDestinations();
        if (feedDestinations != null) {
            FeedDestination feedDestination = feedDestinations.get(0);
            Datasource ds = feedDestination.getDatasource();
            Assert.assertTrue(ds instanceof DerivedDatasource, "Datasource was not expected DerivedDatasource");
        }
    }, MetadataAccess.SERVICE);
}
Also used : DerivedDatasource(com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource) Datasource(com.thinkbiganalytics.metadata.api.datasource.Datasource) DerivedDatasource(com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource) FeedDestination(com.thinkbiganalytics.metadata.api.feed.FeedDestination) Feed(com.thinkbiganalytics.metadata.api.feed.Feed) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with DerivedDatasource

use of com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource in project kylo by Teradata.

the class DerivedDatasourceFactory method ensureDataTransformationSourceDatasources.

/**
 * Builds the list of data sources for the specified data transformation feed.
 *
 * @param feed the feed
 * @return the list of data sources
 * @throws NullPointerException if the feed has no data transformation
 */
@Nonnull
private Set<Datasource.ID> ensureDataTransformationSourceDatasources(@Nonnull final FeedMetadata feed) {
    final Set<Datasource.ID> datasources = new HashSet<>();
    // Extract nodes in chart view model
    @SuppressWarnings("unchecked") final Stream<Map<String, Object>> nodes = Optional.ofNullable(feed.getDataTransformation().getChartViewModel()).map(model -> (List<Map<String, Object>>) model.get("nodes")).map(Collection::stream).orElse(Stream.empty());
    // Create a data source for each node
    final DatasourceDefinition hiveDefinition = datasourceDefinitionProvider.findByProcessorType(DATA_TRANSFORMATION_HIVE_DEFINITION);
    final DatasourceDefinition jdbcDefinition = datasourceDefinitionProvider.findByProcessorType(DATA_TRANSFORMATION_JDBC_DEFINITION);
    nodes.forEach(node -> {
        // Extract properties from node
        final DatasourceDefinition datasourceDefinition;
        final Map<String, String> properties = new HashMap<>();
        if (node.get("datasourceId") == null || node.get("datasourceId").equals("HIVE")) {
            final String name = (String) node.get("name");
            datasourceDefinition = hiveDefinition;
            properties.put(HIVE_SCHEMA_KEY, StringUtils.trim(StringUtils.substringBefore(name, ".")));
            properties.put(HIVE_TABLE_KEY, StringUtils.trim(StringUtils.substringAfterLast(name, ".")));
        } else {
            final Datasource datasource = datasourceProvider.getDatasource(datasourceProvider.resolve((String) node.get("datasourceId")));
            datasourceDefinition = jdbcDefinition;
            properties.put(JDBC_CONNECTION_KEY, datasource.getName());
            properties.put(JDBC_TABLE_KEY, (String) node.get("name"));
            properties.putAll(parseDataTransformControllerServiceProperties(datasourceDefinition, datasource.getName()));
        }
        if (datasourceDefinition != null) {
            // Create the derived data source
            final String identityString = propertyExpressionResolver.resolveVariables(datasourceDefinition.getIdentityString(), properties);
            final String title = datasourceDefinition.getTitle() != null ? propertyExpressionResolver.resolveVariables(datasourceDefinition.getTitle(), properties) : identityString;
            final String desc = propertyExpressionResolver.resolveVariables(datasourceDefinition.getDescription(), properties);
            final DerivedDatasource datasource = datasourceProvider.ensureDerivedDatasource(datasourceDefinition.getDatasourceType(), identityString, title, desc, new HashMap<>(properties));
            datasources.add(datasource.getId());
        }
    });
    // Build the data sources from the data source ids
    final List<String> datasourceIds = Optional.ofNullable(feed.getDataTransformation()).map(FeedDataTransformation::getDatasourceIds).orElse(Collections.emptyList());
    datasourceIds.stream().map(datasourceProvider::resolve).forEach(datasources::add);
    return datasources;
}
Also used : DerivedDatasource(com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource) Datasource(com.thinkbiganalytics.metadata.api.datasource.Datasource) HashMap(java.util.HashMap) DerivedDatasource(com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource) DatasourceDefinition(com.thinkbiganalytics.metadata.api.datasource.DatasourceDefinition) TemplateProcessorDatasourceDefinition(com.thinkbiganalytics.feedmgr.rest.model.TemplateProcessorDatasourceDefinition) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Nonnull(javax.annotation.Nonnull)

Example 4 with DerivedDatasource

use of com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource in project kylo by Teradata.

the class JcrDatasourceProvider method ensureDerivedDatasource.

/**
 * gets or creates the Derived datasource
 */
public DerivedDatasource ensureDerivedDatasource(String datasourceType, String identityString, String title, String desc, Map<String, Object> properties) {
    // ensure the identity String is not null
    if (identityString == null) {
        identityString = "";
    }
    if (datasourceType == null) {
        datasourceType = "Datasource";
    }
    DerivedDatasource derivedDatasource = findDerivedDatasource(datasourceType, identityString);
    if (derivedDatasource == null) {
        try {
            if (!getSession().getRootNode().hasNode("metadata/datasources/derived")) {
                if (!getSession().getRootNode().hasNode("metadata/datasources")) {
                    getSession().getRootNode().addNode("metadata", "datasources");
                }
                getSession().getRootNode().getNode("metadata/datasources").addNode("derived");
            }
            Node parentNode = getSession().getNode(EntityUtil.pathForDerivedDatasource());
            String nodeName = datasourceType + "-" + identityString;
            if (Jsr283Encoder.containsEncodeableCharacters(identityString)) {
                nodeName = new Jsr283Encoder().encode(nodeName);
            }
            JcrDerivedDatasource jcrDerivedDatasource = null;
            try {
                jcrDerivedDatasource = findDerivedDatasourceByNodeName(nodeName);
            } catch (RepositoryException e) {
                log.warn("An exception occurred trying to find the DerivedDatasource by node name {}.  {} ", nodeName, e.getMessage());
            }
            derivedDatasource = jcrDerivedDatasource;
            if (jcrDerivedDatasource == null) {
                Node derivedDatasourceNode = JcrUtil.createNode(parentNode, nodeName, JcrDerivedDatasource.NODE_TYPE);
                jcrDerivedDatasource = new JcrDerivedDatasource(derivedDatasourceNode);
                jcrDerivedDatasource.setSystemName(identityString);
                jcrDerivedDatasource.setDatasourceType(datasourceType);
                jcrDerivedDatasource.setTitle(title);
                jcrDerivedDatasource.setDescription(desc);
                derivedDatasource = jcrDerivedDatasource;
            }
        } catch (RepositoryException e) {
            log.error("Failed to create Derived Datasource for DatasourceType: {}, IdentityString: {}, Error: {}", datasourceType, identityString, e.getMessage(), e);
        }
    }
    if (derivedDatasource != null) {
        // ((JcrDerivedDatasource)derivedDatasource).mergeProperties()
        if (properties != null) {
            derivedDatasource.setProperties(properties);
        }
        derivedDatasource.setTitle(title);
    }
    return derivedDatasource;
}
Also used : Jsr283Encoder(org.modeshape.common.text.Jsr283Encoder) Node(javax.jcr.Node) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) DerivedDatasource(com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource)

Example 5 with DerivedDatasource

use of com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource in project kylo by Teradata.

the class DerivedDatasourceFactory method ensureDatasource.

public Datasource.ID ensureDatasource(TemplateProcessorDatasourceDefinition definition, FeedMetadata feedMetadata, List<NifiProperty> allProperties) {
    return metadataAccess.commit(() -> {
        List<NifiProperty> propertiesToEvalulate = new ArrayList<NifiProperty>();
        // fetch the def
        DatasourceDefinition datasourceDefinition = datasourceDefinitionProvider.findByProcessorType(definition.getProcessorType());
        if (datasourceDefinition != null) {
            // find out if there are any saved properties on the Feed that match the datasourceDef
            List<NifiProperty> feedProperties = feedMetadata.getProperties().stream().filter(property -> matchesDefinition(definition, property) && datasourceDefinition.getDatasourcePropertyKeys().contains(property.getKey())).collect(Collectors.toList());
            // resolve any ${metadata.} properties
            List<NifiProperty> resolvedFeedProperties = propertyExpressionResolver.resolvePropertyExpressions(feedProperties, feedMetadata);
            List<NifiProperty> resolvedAllProperties = propertyExpressionResolver.resolvePropertyExpressions(allProperties, feedMetadata);
            // propetyHash
            propertiesToEvalulate.addAll(feedProperties);
            propertiesToEvalulate.addAll(allProperties);
            propertyExpressionResolver.resolveStaticProperties(propertiesToEvalulate);
            String identityString = datasourceDefinition.getIdentityString();
            String desc = datasourceDefinition.getDescription();
            String title = datasourceDefinition.getTitle();
            PropertyExpressionResolver.ResolvedVariables identityStringPropertyResolution = propertyExpressionResolver.resolveVariables(identityString, propertiesToEvalulate);
            identityString = identityStringPropertyResolution.getResolvedString();
            PropertyExpressionResolver.ResolvedVariables titlePropertyResolution = propertyExpressionResolver.resolveVariables(title, propertiesToEvalulate);
            title = titlePropertyResolution.getResolvedString();
            if (desc != null) {
                PropertyExpressionResolver.ResolvedVariables descriptionPropertyResolution = propertyExpressionResolver.resolveVariables(desc, propertiesToEvalulate);
                desc = descriptionPropertyResolution.getResolvedString();
            }
            // if the identityString still contains unresolved variables then make the title readable and replace the idstring with the feed.id
            if (propertyExpressionResolver.containsVariablesPatterns(identityString)) {
                title = propertyExpressionResolver.replaceAll(title, " {runtime variable} ");
                identityString = propertyExpressionResolver.replaceAll(identityString, feedMetadata.getId());
            }
            // if it is the Source ensure the feed matches this ds
            if (isCreateDatasource(datasourceDefinition, feedMetadata)) {
                Map<String, String> controllerServiceProperties = parseControllerServiceProperties(datasourceDefinition, feedProperties);
                Map<String, Object> properties = new HashMap<String, Object>(identityStringPropertyResolution.getResolvedVariables());
                properties.putAll(controllerServiceProperties);
                DerivedDatasource derivedDatasource = datasourceProvider.ensureDerivedDatasource(datasourceDefinition.getDatasourceType(), identityString, title, desc, properties);
                if (derivedDatasource != null) {
                    if ("HiveDatasource".equals(derivedDatasource.getDatasourceType()) && Optional.ofNullable(feedMetadata.getTable()).map(TableSetup::getTableSchema).map(TableSchema::getFields).isPresent()) {
                        derivedDatasource.setGenericProperties(Collections.singletonMap("columns", (Serializable) feedMetadata.getTable().getTableSchema().getFields()));
                    }
                    return derivedDatasource.getId();
                }
            }
            return null;
        } else {
            return null;
        }
    }, MetadataAccess.SERVICE);
}
Also used : FeedDataTransformation(com.thinkbiganalytics.feedmgr.rest.model.FeedDataTransformation) DerivedDatasource(com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) StringUtils(org.apache.commons.lang3.StringUtils) DatasourceDefinitionProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceDefinitionProvider) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) DatasourceDefinition(com.thinkbiganalytics.metadata.api.datasource.DatasourceDefinition) TemplateProcessorDatasourceDefinition(com.thinkbiganalytics.feedmgr.rest.model.TemplateProcessorDatasourceDefinition) Map(java.util.Map) PropertyExpressionResolver(com.thinkbiganalytics.feedmgr.nifi.PropertyExpressionResolver) TableSetup(com.thinkbiganalytics.feedmgr.rest.model.schema.TableSetup) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) RegisteredTemplate(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate) Nonnull(javax.annotation.Nonnull) FeedManagerTemplateService(com.thinkbiganalytics.feedmgr.service.template.FeedManagerTemplateService) Datasource(com.thinkbiganalytics.metadata.api.datasource.Datasource) Logger(org.slf4j.Logger) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) NifiControllerServiceProperties(com.thinkbiganalytics.feedmgr.nifi.NifiControllerServiceProperties) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) RegisteredTemplateCache(com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateCache) List(java.util.List) Stream(java.util.stream.Stream) TableSchema(com.thinkbiganalytics.discovery.schema.TableSchema) Optional(java.util.Optional) DatasourceProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider) Collections(java.util.Collections) Serializable(java.io.Serializable) TableSchema(com.thinkbiganalytics.discovery.schema.TableSchema) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PropertyExpressionResolver(com.thinkbiganalytics.feedmgr.nifi.PropertyExpressionResolver) DerivedDatasource(com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource) DatasourceDefinition(com.thinkbiganalytics.metadata.api.datasource.DatasourceDefinition) TemplateProcessorDatasourceDefinition(com.thinkbiganalytics.feedmgr.rest.model.TemplateProcessorDatasourceDefinition) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty)

Aggregations

DerivedDatasource (com.thinkbiganalytics.metadata.api.datasource.DerivedDatasource)7 Datasource (com.thinkbiganalytics.metadata.api.datasource.Datasource)5 HashMap (java.util.HashMap)4 Nonnull (javax.annotation.Nonnull)4 TemplateProcessorDatasourceDefinition (com.thinkbiganalytics.feedmgr.rest.model.TemplateProcessorDatasourceDefinition)3 DatasourceDefinition (com.thinkbiganalytics.metadata.api.datasource.DatasourceDefinition)3 DatasourceProvider (com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 TableSchema (com.thinkbiganalytics.discovery.schema.TableSchema)2 TableSetup (com.thinkbiganalytics.feedmgr.rest.model.schema.TableSetup)2 MetadataAccess (com.thinkbiganalytics.metadata.api.MetadataAccess)2 MetadataRepositoryException (com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException)2 Serializable (java.io.Serializable)2 Collections (java.util.Collections)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2