use of com.thinkbiganalytics.discovery.model.DefaultDatabaseMetadata in project kylo by Teradata.
the class DatasourceController method getTablesAndColumns.
/**
* Gets the tables and their columns from the specified data source for given schema
*
* @param idStr the data source id
* @param schema the schema name, or {@code null} for all schemas
* @return the list of tables and their columns
*/
@GET
@Path("{id}/table-columns")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Gets the tables and their columns from the data source for given schema", notes = "Connects to the database specified by the data source.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns tables and columns", response = String.class, responseContainer = "List"), @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 getTablesAndColumns(@PathParam("id") final String idStr, @QueryParam("schema") final String schema) {
// Verify user has access to data source
final Optional<com.thinkbiganalytics.metadata.api.datasource.Datasource.ID> id = metadata.read(() -> {
accessController.checkPermission(AccessController.SERVICES, FeedServicesAccessControl.ACCESS_DATASOURCES);
final com.thinkbiganalytics.metadata.api.datasource.Datasource datasource = datasourceProvider.getDatasource(datasourceProvider.resolve(idStr));
return Optional.ofNullable(datasource).map(com.thinkbiganalytics.metadata.api.datasource.Datasource::getId);
});
// Retrieve table names using system user
return metadata.read(() -> {
JdbcDatasource datasource = id.map(datasourceProvider::getDatasource).map(ds -> datasourceTransform.toDatasource(ds, DatasourceModelTransform.Level.ADMIN)).filter(JdbcDatasource.class::isInstance).map(JdbcDatasource.class::cast).orElseThrow(() -> new NotFoundException("No JDBC datasource exists with the given ID: " + idStr));
List<String> tableNamesForDatasource = dbcpConnectionPoolTableInfo.getTableNamesForDatasource(datasource, schema, null);
List<DatabaseMetadata> tables = new ArrayList<>();
if (tableNamesForDatasource != null) {
tables = tableNamesForDatasource.stream().flatMap(schemaNameDotTableName -> {
String tableName = schemaNameDotTableName.substring(schema.length() + 1);
TableSchema tableSchema = dbcpConnectionPoolTableInfo.describeTableForDatasource(datasource, schema, tableName);
return tableSchema.getFields().stream().map(field -> {
DefaultDatabaseMetadata meta = new DefaultDatabaseMetadata();
meta.setDatabaseName(schema);
meta.setColumnName(field.getName());
meta.setTableName(tableName);
return meta;
});
}).collect(Collectors.toList());
}
return Response.ok(tables).build();
}, MetadataAccess.SERVICE);
}
use of com.thinkbiganalytics.discovery.model.DefaultDatabaseMetadata in project kylo by Teradata.
the class HiveMetastoreService method getTableColumns.
public List<DatabaseMetadata> getTableColumns(List<String> tablesFilter) throws DataAccessException {
String query = "SELECT d.NAME as \"DATABASE_NAME\", t.TBL_NAME, c.COLUMN_NAME " + "FROM COLUMNS_V2 c " + "JOIN SDS s on s.CD_ID = c.CD_ID " + "JOIN TBLS t ON s.SD_ID = t.SD_ID " + "JOIN DBS d on d.DB_ID = t.DB_ID " + "ORDER BY d.NAME, t.TBL_NAME";
if (DatabaseType.POSTGRES.equals(getMetastoreDatabaseType())) {
query = "SELECT d.\"NAME\" as \"DATABASE_NAME\", t.\"TBL_NAME\", c.\"COLUMN_NAME\" " + "FROM \"COLUMNS_V2\" c " + "JOIN \"SDS\" s on s.\"CD_ID\" = c.\"CD_ID\" " + "JOIN \"TBLS\" t ON s.\"SD_ID\" = t.\"SD_ID\" " + "JOIN \"DBS\" d on d.\"DB_ID\" = t.\"DB_ID\" " + "ORDER BY d.\"NAME\", t.\"TBL_NAME\"";
}
List<DatabaseMetadata> metadata = hiveMetatoreJdbcTemplate.query(query, new RowMapper<DatabaseMetadata>() {
@Override
public DatabaseMetadata mapRow(ResultSet rs, int i) throws SQLException {
DefaultDatabaseMetadata row = new DefaultDatabaseMetadata();
row.setDatabaseName(rs.getString("DATABASE_NAME"));
row.setColumnName(rs.getString("COLUMN_NAME"));
row.setTableName(rs.getString("TBL_NAME"));
return row;
}
});
return tablesFilter == null ? metadata : filterDatabaseMetadata(metadata, tablesFilter);
}
Aggregations