Search in sources :

Example 26 with ControllerServiceDTO

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

the class FeedHistoryDataReindexingService method checkAndCreateDistributedMapCacheServerControllerService.

private void checkAndCreateDistributedMapCacheServerControllerService() {
    boolean doesControllerServiceExist = false;
    String existingOrNewControllerServiceId = "";
    String stateOfExistingOrNewControllerService = "DISABLED";
    // Check if controller service already exists
    log.info("Checking if controller service already exists.");
    Set<ControllerServiceDTO> existingControllerServicesInNiFi = niFiRestClient.processGroups().getControllerServices("root");
    for (ControllerServiceDTO existingControllerService : existingControllerServicesInNiFi) {
        if (existingControllerService.getType().equals(DISTRIBUTED_MAP_CACHE_SERVER_CONTROLLER_SERVICE_TYPE)) {
            doesControllerServiceExist = true;
            existingOrNewControllerServiceId = existingControllerService.getId();
            stateOfExistingOrNewControllerService = existingControllerService.getState();
            log.info("Existing controller service found with id: {}, in state: {}", existingOrNewControllerServiceId, stateOfExistingOrNewControllerService);
            break;
        }
    }
    if (!doesControllerServiceExist) {
        log.info("Did not find existing controller service. Creating it.");
        ControllerServiceDTO distributedMapCacheServerControllerService = new ControllerServiceDTO();
        distributedMapCacheServerControllerService.setType(DISTRIBUTED_MAP_CACHE_SERVER_CONTROLLER_SERVICE_TYPE);
        ControllerServiceDTO createdDistributedMapCacheServerControllerService = niFiRestClient.controllerServices().create(distributedMapCacheServerControllerService);
        existingOrNewControllerServiceId = createdDistributedMapCacheServerControllerService.getId();
        stateOfExistingOrNewControllerService = createdDistributedMapCacheServerControllerService.getState();
        log.info("Created controller service with id: {}, in state: {}", existingOrNewControllerServiceId, stateOfExistingOrNewControllerService);
    }
    if (!stateOfExistingOrNewControllerService.equals("ENABLED")) {
        // rest client throws exception if trying to enable an already enabled service, hence trying only 1 time
        int numRetries = 1;
        int retryCount = 0;
        boolean serviceEnabled = false;
        String lastControllerServiceState = stateOfExistingOrNewControllerService;
        while ((retryCount < numRetries) && (!serviceEnabled)) {
            log.info("Enabling controller service with id: {}", existingOrNewControllerServiceId);
            ControllerServiceDTO controllerServiceDTO = new ControllerServiceDTO();
            controllerServiceDTO.setId(existingOrNewControllerServiceId);
            controllerServiceDTO.setState("ENABLED");
            ControllerServiceDTO updatedControllerServiceDTO = niFiRestClient.controllerServices().update(controllerServiceDTO);
            retryCount++;
            lastControllerServiceState = updatedControllerServiceDTO.getState();
            log.info("Updated status controller service with id: {} is: {}", updatedControllerServiceDTO.getId(), lastControllerServiceState);
            if (!lastControllerServiceState.equals("ENABLED")) {
                log.debug("Tried {} time/s to enable controller service, it is still in {} state in NiFi. Will try for total {} times.", retryCount, lastControllerServiceState, numRetries);
                serviceEnabled = false;
            } else {
                log.info("Controller service detected as enabled in NiFi.");
                serviceEnabled = true;
            }
        }
        if (!serviceEnabled) {
            log.warn("Distributed Map Cache Server Controller Service in NiFi with id {} detected in {} state in NiFi. Verify that it has enabled successfully in NiFi.", existingOrNewControllerServiceId, lastControllerServiceState);
        }
    }
}
Also used : ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO)

Example 27 with ControllerServiceDTO

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

the class DBCPConnectionPoolService method getTableNamesForControllerService.

/**
 * Returns a list of table names matching a pattern
 *
 * @param serviceId   a NiFi controller service id
 * @param serviceName a NiFi controller service name
 * @param schema      A schema pattern to look for
 * @param tableName   A table pattern to look for
 * @return a list of schema.table names matching the pattern for the database
 */
public List<String> getTableNamesForControllerService(String serviceId, String serviceName, String schema, String tableName) {
    ControllerServiceDTO controllerService = getControllerService(serviceId, serviceName);
    if (controllerService != null) {
        DescribeTableControllerServiceRequestBuilder builder = new DescribeTableControllerServiceRequestBuilder(controllerService);
        DescribeTableControllerServiceRequest serviceProperties = builder.schemaName(schema).tableName(tableName).build();
        return getTableNamesForControllerService(serviceProperties);
    } else {
        log.error("Cannot getTable Names for Controller Service. Unable to obtain Controller Service for serviceId or Name ({} , {})", serviceId, serviceName);
    }
    return null;
}
Also used : ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) DescribeTableControllerServiceRequestBuilder(com.thinkbiganalytics.feedmgr.nifi.controllerservice.DescribeTableControllerServiceRequest.DescribeTableControllerServiceRequestBuilder)

Example 28 with ControllerServiceDTO

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

the class DBCPConnectionPoolService method executePreviewQueryForControllerService.

/**
 * Executes 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 tableName the name of the table
 * @param schema the schema determining the SQL dialect, or null/empty for the default dialect
 * @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 schema/tableName is empty, or the limit is less than zero
 */
public QueryResult executePreviewQueryForControllerService(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 executeQueryForControllerService(dataSourceProperties, previewRequest);
    } 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 29 with ControllerServiceDTO

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

the class DBCPConnectionPoolService method generatePreviewQueryForDatasource.

/**
 * Generates a preview query on a table that is appropriate for the type of specified data source.
 *
 * @param datasource the JDBC datasource
 * @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 string
 * @throws IllegalArgumentException if the datasource is invalid
 */
@Nonnull
public String generatePreviewQueryForDatasource(@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()).previewQuery(dbType, schema, tableName, limit).build();
        return serviceProperties.getQuery();
    } 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) DatabaseType(com.thinkbiganalytics.jdbc.util.DatabaseType) ExecuteQueryControllerServiceRequestBuilder(com.thinkbiganalytics.feedmgr.nifi.controllerservice.ExecuteQueryControllerServiceRequest.ExecuteQueryControllerServiceRequestBuilder) Nonnull(javax.annotation.Nonnull)

Example 30 with ControllerServiceDTO

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

the class DBCPConnectionPoolService method getTableNamesForDatasource.

/**
 * Returns a list of table names for the specified data source.
 *
 * @param datasource the data source
 * @param schema     the schema name, or {@code null} for all schemas
 * @param tableName  a table pattern to look for
 * @return a list of schema.table names, or {@code null} if not accessible
 */
@Nullable
public List<String> getTableNamesForDatasource(@Nonnull final JdbcDatasource datasource, @Nullable final String schema, @Nullable final String tableName) {
    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.schemaName(schema).tableName(tableName).password(datasource.getPassword()).useEnvironmentProperties(false).build();
        return getTableNamesForControllerService(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)

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