use of com.thinkbiganalytics.kylo.catalog.rest.model.DefaultDataSetTemplate in project kylo by Teradata.
the class AbstractCatalogDataSetProvider method mergeTemplates.
/**
* Merges the data set, data source, and connector templates for the specified data set.
*/
@Nonnull
protected DefaultDataSetTemplate mergeTemplates(@Nonnull final DataSet dataSet) {
final DefaultDataSetTemplate template;
if (dataSet.getDataSource() != null) {
final DataSource dataSource = dataSet.getDataSource();
template = new DefaultDataSetTemplate();
if (dataSource.getConnector() != null && dataSource.getConnector().getTemplate() != null) {
mergeTemplates(template, dataSource.getConnector().getTemplate());
}
if (dataSource.getTemplate() != null) {
mergeTemplates(template, dataSource.getTemplate());
}
mergeTemplates(template, dataSet);
} else {
template = new DefaultDataSetTemplate(dataSet);
}
return template;
}
use of com.thinkbiganalytics.kylo.catalog.rest.model.DefaultDataSetTemplate in project kylo by Teradata.
the class DataSourceController method createJdbcTableDataSet.
/**
* Gets the schema of the specified table using the specified data source.
*
* @param dataSourceId the data source id
* @param tableName the table name
* @param schema the schema name, or {@code null} to search all schemas
* @return the table and field details
*/
@POST
@Path("{id}/tables/{tableName}/dataset")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Gets the schema of the specified table.", notes = "Connects to the database specified by the data source.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the table schema.", response = DataSetWithTableSchema.class), @ApiResponse(code = 403, message = "Access denied.", response = RestResponseStatus.class), @ApiResponse(code = 404, message = "A JDBC data source with that id does not exist.", response = RestResponseStatus.class), @ApiResponse(code = 500, message = "NiFi or the database are unavailable.", response = RestResponseStatus.class) })
public Response createJdbcTableDataSet(@PathParam("id") final String dataSourceId, @PathParam("tableName") final String tableName, @QueryParam("schema") final String schema) {
// TODO Verify user has access to data source
// Require admin permission if the results should include unencrypted credentials.
final boolean encryptCredentials = true;
accessController.checkPermission(AccessController.SERVICES, encryptCredentials ? FeedServicesAccessControl.ACCESS_DATASOURCES : FeedServicesAccessControl.ADMIN_DATASOURCES);
DataSetWithTableSchema dataSetWithTableSchema = null;
boolean hasAccess = false;
try {
// ensure the user can read teh datasource
DataSource encryptedSource = metadataService.read(() -> {
return findDataSource(dataSourceId, true);
});
hasAccess = encryptedSource != null;
if (hasAccess) {
// fetch the datasource as a service account to get the creds.
DataSource dataSource = metadataService.read(() -> {
return findDataSource(dataSourceId, false);
}, MetadataAccess.SERVICE);
// describe the datasource and table
CatalogTableSchema tableSchema = tableManager.describeTable(dataSource, schema, tableName);
if (tableSchema != null) {
DataSet dataSet = new DataSet();
// assign the datasource to this dataset with encrypted credentials
dataSet.setDataSource(encryptedSource);
String fullTableName = (tableSchema.getTable() != null) ? tableSchema.getTable().getQualifiedIdentifier() : HiveUtils.quoteIdentifier(tableSchema.getSchemaName(), tableSchema.getName());
dataSet.setTitle(tableSchema.getSchemaName() + "." + tableSchema.getName());
DefaultDataSetTemplate defaultDataSetTemplate = DataSetUtil.mergeTemplates(dataSet);
List<String> paths = defaultDataSetTemplate.getPaths();
String format = defaultDataSetTemplate.getFormat();
Map<String, String> options = defaultDataSetTemplate.getOptions();
if (options == null) {
options = new HashMap<>();
}
if ("hive".equalsIgnoreCase(format.toLowerCase())) {
if (paths == null) {
paths = new ArrayList<>();
}
paths.add(fullTableName);
}
options.put("dbtable", fullTableName);
dataSet.setFormat(format);
dataSet.setPaths(paths);
dataSet.setOptions(options);
DataSet dataSet1 = dataSetService.findOrCreateDataSet(dataSet, encryptCredentials);
dataSetWithTableSchema = new DataSetWithTableSchema(dataSet1, tableSchema);
} else {
if (log.isErrorEnabled()) {
log.error("Failed to describe tables for schema [" + schema + "], table [" + tableName + "], dataSource [" + dataSourceId + "] ");
}
final RestResponseStatus status = new RestResponseStatus.ResponseStatusBuilder().message(getMessage("catalog.datasource.describeTable.error", tableName, schema)).url(request.getRequestURI()).buildError();
throw new InternalServerErrorException(Response.serverError().entity(status).build());
}
// });
} else {
// no acceess
final RestResponseStatus status = new RestResponseStatus.ResponseStatusBuilder().message(getMessage("catalog.datasource.forbidden")).url(request.getRequestURI()).buildError();
throw new InternalServerErrorException(Response.serverError().entity(status).build());
}
} catch (Exception e) {
if (exceptionTransformer.causesInChain(e)) {
throw new ThriftConnectionException(e);
}
final RestResponseStatus status = new RestResponseStatus.ResponseStatusBuilder().message(getMessage("catalog.datasource.describeTable.error", tableName, schema)).url(request.getRequestURI()).buildError();
throw new InternalServerErrorException(Response.serverError().entity(status).build());
}
return Response.ok(dataSetWithTableSchema).build();
}
use of com.thinkbiganalytics.kylo.catalog.rest.model.DefaultDataSetTemplate in project kylo by Teradata.
the class DataSourceUtilTest method getPaths.
/**
* Verify merging paths for a data source.
*/
@Test
public void getPaths() {
// Create mock connector
final DefaultDataSetTemplate connectorTemplate = new DefaultDataSetTemplate();
connectorTemplate.setOptions(Collections.singletonMap("path", "connector1.txt"));
connectorTemplate.setPaths(Collections.singletonList("connector2.txt"));
final Connector connector = new Connector();
connector.setTemplate(connectorTemplate);
// Create mock data source
final DefaultDataSetTemplate dataSourceTemplate = new DefaultDataSetTemplate();
dataSourceTemplate.setOptions(Collections.singletonMap("path", "datasource1.txt"));
dataSourceTemplate.setPaths(Collections.singletonList("datasource2.txt"));
final DataSource dataSource = new DataSource();
dataSource.setConnector(connector);
dataSource.setTemplate(dataSourceTemplate);
// Test retrieving data source paths
Assert.assertEquals(Arrays.asList("datasource1.txt", "datasource2.txt"), DataSourceUtil.getPaths(dataSource).orElse(null));
// Test retrieving connector paths
dataSourceTemplate.setOptions(null);
Assert.assertEquals(Arrays.asList("connector1.txt", "datasource2.txt"), DataSourceUtil.getPaths(dataSource).orElse(null));
dataSourceTemplate.setPaths(null);
Assert.assertEquals(Arrays.asList("connector1.txt", "connector2.txt"), DataSourceUtil.getPaths(dataSource).orElse(null));
// Test retrieving empty paths
connectorTemplate.setOptions(null);
connectorTemplate.setPaths(null);
Assert.assertEquals(Optional.empty(), DataSourceUtil.getPaths(dataSource));
}
use of com.thinkbiganalytics.kylo.catalog.rest.model.DefaultDataSetTemplate in project kylo by Teradata.
the class DataSetUtilTest method getPaths.
/**
* Verify merging paths for a data set.
*/
@Test
public void getPaths() {
// Create mock connector
final DefaultDataSetTemplate connectorTemplate = new DefaultDataSetTemplate();
connectorTemplate.setOptions(Collections.singletonMap("path", "connector1.txt"));
connectorTemplate.setPaths(Collections.singletonList("connector2.txt"));
final Connector connector = new Connector();
connector.setTemplate(connectorTemplate);
// Create mock data source
final DefaultDataSetTemplate dataSourceTemplate = new DefaultDataSetTemplate();
dataSourceTemplate.setOptions(Collections.singletonMap("path", "datasource1.txt"));
dataSourceTemplate.setPaths(Collections.singletonList("datasource2.txt"));
final DataSource dataSource = new DataSource();
dataSource.setConnector(connector);
dataSource.setTemplate(dataSourceTemplate);
// Create mock data set
final DataSet dataSet = new DataSet();
dataSet.setDataSource(dataSource);
dataSet.setOptions(Collections.singletonMap("path", "dataset1.txt"));
dataSet.setPaths(Collections.singletonList("dataset2.txt"));
// Test retrieving data set paths
Assert.assertEquals(Arrays.asList("dataset1.txt", "dataset2.txt"), DataSetUtil.getPaths(dataSet).orElse(null));
// Test retrieving data source paths
dataSet.setOptions(null);
Assert.assertEquals(Arrays.asList("datasource1.txt", "dataset2.txt"), DataSetUtil.getPaths(dataSet).orElse(null));
dataSet.setPaths(null);
Assert.assertEquals(Arrays.asList("datasource1.txt", "datasource2.txt"), DataSetUtil.getPaths(dataSet).orElse(null));
// Test retrieving connector paths
dataSourceTemplate.setOptions(null);
Assert.assertEquals(Arrays.asList("connector1.txt", "datasource2.txt"), DataSetUtil.getPaths(dataSet).orElse(null));
dataSourceTemplate.setPaths(null);
Assert.assertEquals(Arrays.asList("connector1.txt", "connector2.txt"), DataSetUtil.getPaths(dataSet).orElse(null));
// Test retrieving empty paths
connectorTemplate.setOptions(null);
connectorTemplate.setPaths(null);
Assert.assertEquals(Optional.empty(), DataSetUtil.getPaths(dataSet));
}
use of com.thinkbiganalytics.kylo.catalog.rest.model.DefaultDataSetTemplate in project kylo by Teradata.
the class CatalogFileManagerTest method createDataSet.
/**
* Creates a new data set.
*/
@Nonnull
private DataSet createDataSet() {
final DefaultDataSetTemplate dataSourceTemplate = new DefaultDataSetTemplate();
dataSourceTemplate.setPaths(Collections.singletonList(datasetsFolder.getRoot().toURI().toString()));
final DataSource dataSource = new DataSource();
dataSource.setId(UUID.randomUUID().toString());
dataSource.setTemplate(dataSourceTemplate);
final DataSet dataSet = new DataSet();
dataSet.setId(UUID.randomUUID().toString());
dataSet.setDataSource(dataSource);
return dataSet;
}
Aggregations