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;
}
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;
}
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;
}
}
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);
}
}
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);
}
}
Aggregations