Search in sources :

Example 41 with ControllerServiceDTO

use of org.apache.nifi.web.api.dto.ControllerServiceDTO in project kylo by Teradata.

the class DerivedDatasourceFactory method parseControllerServiceProperties.

/**
 * Parse the defintion metadata for the {propertyKey:CS Property Key} objects and pick out the values in the controller service
 *
 * @param datasourceDefinition the definition to use
 * @param feedProperties       the feed properties that match this definition
 * @return a Map of the Controller Service Property Key, Value
 */
private Map<String, String> parseControllerServiceProperties(DatasourceDefinition datasourceDefinition, List<NifiProperty> feedProperties) {
    Map<String, String> properties = new HashMap<>();
    try {
        // {Source Database Connection:Database Connection URL}
        List<String> controllerServiceProperties = datasourceDefinition.getDatasourcePropertyKeys().stream().filter(k -> k.matches("\\{(.*):(.*)\\}")).collect(Collectors.toList());
        Map<String, List<String>> serviceProperties = new HashMap<>();
        controllerServiceProperties.stream().forEach(p -> {
            String service = p.substring(1, StringUtils.indexOf(p, ":"));
            String property = p.substring(StringUtils.indexOf(p, ":") + 1, p.length() - 1);
            if (!serviceProperties.containsKey(service)) {
                serviceProperties.put(service, new ArrayList<>());
            }
            serviceProperties.get(service).add(property);
        });
        serviceProperties.entrySet().stream().forEach(e -> {
            String service = e.getKey();
            String controllerServiceId = feedProperties.stream().filter(p -> StringUtils.isNotBlank(p.getValue()) && p.getPropertyDescriptor() != null && p.getPropertyDescriptor().getName().equalsIgnoreCase(service) && StringUtils.isNotBlank(p.getPropertyDescriptor().getIdentifiesControllerService())).map(p -> p.getValue()).findFirst().orElse(null);
            if (controllerServiceId != null) {
                ControllerServiceDTO csDto = nifiControllerServiceProperties.getControllerServiceById(controllerServiceId);
                if (csDto != null) {
                    e.getValue().stream().forEach(propertyKey -> {
                        String value = csDto.getProperties().get(propertyKey);
                        if (value != null) {
                            properties.put(propertyKey, value);
                        }
                    });
                }
            }
        });
    } catch (Exception e) {
        log.warn("An error occurred trying to parse controller service properties when deriving the datasource for {}, {}. {} ", datasourceDefinition.getDatasourceType(), datasourceDefinition.getConnectionType(), e.getMessage(), e);
    }
    return properties;
}
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) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 42 with ControllerServiceDTO

use of org.apache.nifi.web.api.dto.ControllerServiceDTO in project kylo by Teradata.

the class NifiControllerServiceProperties method getPropertiesForServiceIdMergedWithEnvironmentProperties.

/**
 * Call out to Nifi and get the Controller Service Properties and then merge it with any properties in our environment properties file.
 * Env service properties need to start with the ENVIRONMENT_PROPERTY_SERVICE_PREFIX  ("nifi.service.")
 */
public Map<String, String> getPropertiesForServiceIdMergedWithEnvironmentProperties(String serviceId) {
    ControllerServiceDTO controllerService = getControllerServiceById(serviceId);
    if (controllerService != null) {
        String serviceName = controllerService.getName();
        Map<String, String> properties = controllerService.getProperties();
        properties = mergeNifiAndEnvProperties(properties, serviceName);
        return properties;
    }
    return null;
}
Also used : ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO)

Example 43 with ControllerServiceDTO

use of org.apache.nifi.web.api.dto.ControllerServiceDTO in project kylo by Teradata.

the class DBCPConnectionPoolService method getSchemaNamesForDatasource.

/**
 * Returns a list of schema names for the specified data source.
 *
 * @param datasource the data source
 * @return a list of schema names, or {@code null} if not accessible
 */
@Nullable
public List<String> getSchemaNamesForDatasource(@Nonnull final JdbcDatasource datasource) {
    final Optional<ControllerServiceDTO> controllerService = Optional.ofNullable(datasource.getControllerServiceId()).map(id -> getControllerService(id, null));
    if (controllerService.isPresent()) {
        final DescribeTableControllerServiceRequestBuilder builder = new DescribeTableControllerServiceRequestBuilder(controllerService.get());
        final DescribeTableControllerServiceRequest serviceProperties = builder.password(datasource.getPassword()).useEnvironmentProperties(false).build();
        return getSchemaNamesForControllerService(serviceProperties);
    } else {
        log.error("Cannot get table names for data source: {}", datasource);
        return null;
    }
}
Also used : ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) DescribeTableControllerServiceRequestBuilder(com.thinkbiganalytics.feedmgr.nifi.controllerservice.DescribeTableControllerServiceRequest.DescribeTableControllerServiceRequestBuilder) Nullable(javax.annotation.Nullable)

Example 44 with ControllerServiceDTO

use of org.apache.nifi.web.api.dto.ControllerServiceDTO in project kylo by Teradata.

the class DBCPConnectionPoolService method generatePreviewQueryForControllerService.

/**
 * Generates a preview query on a table, in the context of the specified controller service, based
 * on the supplied schema (if provided) and limit.
 *
 * @param serviceId   a NiFi controller service id
 * @param serviceName a NiFi controller service name
 * @param schema the schema determining the SQL dialect, or null/empty for the default dialect
 * @param tableName the name of the table
 * @param limit the result size
 * @return the query results
 * @throws DataAccessException      if the query cannot be executed
 * @throws IllegalArgumentException if the controller service cannot be found, the tableName is empty, or the limit is less than zero
 */
public String generatePreviewQueryForControllerService(final String serviceId, final String serviceName, @Nonnull final String schema, @Nonnull final String tableName, final int limit) {
    Validate.notEmpty(schema, "No schema provided");
    Validate.notEmpty(tableName, "No table name provided");
    Validate.isTrue(limit >= 0, "The query result size must be greater than or equal to 0");
    final ControllerServiceDTO controllerService = getControllerService(serviceId, serviceName);
    if (controllerService != null) {
        final ExecuteQueryControllerServiceRequest serviceProperties = new ExecuteQueryControllerServiceRequestBuilder(controllerService).build();
        final PoolingDataSourceService.DataSourceProperties dataSourceProperties = getDataSourceProperties(serviceProperties);
        final DatabaseType dbType = DatabaseType.fromJdbcConnectionString(dataSourceProperties.getUrl());
        final ExecuteQueryControllerServiceRequest previewRequest = new ExecuteQueryControllerServiceRequestBuilder(controllerService).using(serviceProperties).previewQuery(dbType, schema, tableName, limit).build();
        return previewRequest.getQuery();
    } else {
        log.error("Cannot execute query for controller service. Unable to obtain controller service: {}, {}", serviceId, serviceName);
        throw new IllegalArgumentException("Not a valid controller service: " + serviceId + ", " + serviceName);
    }
}
Also used : ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) PoolingDataSourceService(com.thinkbiganalytics.db.PoolingDataSourceService) DatabaseType(com.thinkbiganalytics.jdbc.util.DatabaseType) ExecuteQueryControllerServiceRequestBuilder(com.thinkbiganalytics.feedmgr.nifi.controllerservice.ExecuteQueryControllerServiceRequest.ExecuteQueryControllerServiceRequestBuilder)

Example 45 with ControllerServiceDTO

use of org.apache.nifi.web.api.dto.ControllerServiceDTO in project kylo by Teradata.

the class DBCPConnectionPoolService method executePreviewQueryForDatasource.

/**
 * Executes the specified SELECT query in the context of the specified data source.
 *
 * @param datasource the JDBC datasource
 * @param query      the query to execute
 * @return the query results
 * @throws DataAccessException      if the query cannot be executed
 * @throws IllegalArgumentException if the datasource is invalid
 */
@Nonnull
public QueryResult executePreviewQueryForDatasource(@Nonnull final JdbcDatasource datasource, @Nonnull final String schema, @Nonnull final String tableName, final int limit) {
    final Optional<ControllerServiceDTO> controllerService = Optional.ofNullable(datasource.getControllerServiceId()).map(id -> getControllerService(id, null));
    if (controllerService.isPresent()) {
        final DatabaseType dbType = DatabaseType.fromJdbcConnectionString(datasource.getDatabaseConnectionUrl());
        final ExecuteQueryControllerServiceRequest serviceProperties = new ExecuteQueryControllerServiceRequestBuilder(controllerService.get()).password(datasource.getPassword()).previewQuery(dbType, schema, tableName, limit).useEnvironmentProperties(false).build();
        final PoolingDataSourceService.DataSourceProperties dataSourceProperties = getDataSourceProperties(serviceProperties);
        return executeQueryForControllerService(dataSourceProperties, serviceProperties);
    } else {
        log.error("Cannot execute query for datasource: {}", datasource);
        throw new IllegalArgumentException("Missing controller service for datasource: " + datasource);
    }
}
Also used : ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) PoolingDataSourceService(com.thinkbiganalytics.db.PoolingDataSourceService) DatabaseType(com.thinkbiganalytics.jdbc.util.DatabaseType) ExecuteQueryControllerServiceRequestBuilder(com.thinkbiganalytics.feedmgr.nifi.controllerservice.ExecuteQueryControllerServiceRequest.ExecuteQueryControllerServiceRequestBuilder) Nonnull(javax.annotation.Nonnull)

Aggregations

ControllerServiceDTO (org.apache.nifi.web.api.dto.ControllerServiceDTO)60 HashSet (java.util.HashSet)20 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)19 Map (java.util.Map)18 ProcessorDTO (org.apache.nifi.web.api.dto.ProcessorDTO)17 List (java.util.List)16 Set (java.util.Set)16 ProcessGroupDTO (org.apache.nifi.web.api.dto.ProcessGroupDTO)15 Collectors (java.util.stream.Collectors)14 FlowSnippetDTO (org.apache.nifi.web.api.dto.FlowSnippetDTO)14 Collections (java.util.Collections)13 ControllerServiceNode (org.apache.nifi.controller.service.ControllerServiceNode)13 ConnectionDTO (org.apache.nifi.web.api.dto.ConnectionDTO)13 ProcessorConfigDTO (org.apache.nifi.web.api.dto.ProcessorConfigDTO)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 Optional (java.util.Optional)12 Nonnull (javax.annotation.Nonnull)11 NifiProperty (com.thinkbiganalytics.nifi.rest.model.NifiProperty)10