use of org.springframework.jdbc.core.ResultSetExtractor in project metacat by Netflix.
the class DirectSqlGetPartition method getPartitionUris.
/**
* Gets the partition uris based on a filter expression for the specified table.
*
* @param requestContext The Metacat request context
* @param tableName table handle to get partition for
* @param partitionsRequest The metadata for what kind of partitions to get from the table
* @return filtered list of partition names
*/
@Transactional(readOnly = true)
public List<String> getPartitionUris(final ConnectorRequestContext requestContext, final QualifiedName tableName, final PartitionListRequest partitionsRequest) {
final long start = registry.clock().wallTime();
final List<String> result;
final List<String> partitionNames = partitionsRequest.getPartitionNames();
final Sort sort = partitionsRequest.getSort();
final Pageable pageable = partitionsRequest.getPageable();
final String filterExpression = partitionsRequest.getFilter();
if (filterExpression != null) {
return filterPartitionsColumn(tableName.getDatabaseName(), tableName.getTableName(), partitionNames, PARTITION_URI, filterExpression, sort, pageable, partitionsRequest.getIncludeAuditOnly());
} else {
final ResultSetExtractor<List<String>> handler = rs -> {
final List<String> uris = Lists.newArrayList();
while (rs.next()) {
uris.add(rs.getString(PARTITION_URI));
}
return uris;
};
result = getHandlerResults(tableName.getDatabaseName(), tableName.getTableName(), null, partitionNames, SQL.SQL_GET_PARTITIONS_URI, handler, sort, pageable, partitionsRequest.getIncludeAuditOnly());
}
this.fastServiceMetric.recordTimer(HiveMetrics.TagGetPartitionKeys.getMetricName(), registry.clock().wallTime() - start);
return result;
}
use of org.springframework.jdbc.core.ResultSetExtractor in project metacat by Netflix.
the class DirectSqlTable method getTableNames.
/**
* Returns all the table names referring to the given <code>uris</code>.
*
* @param uris locations
* @param prefixSearch if true, we look for tables whose location starts with the given <code>uri</code>
* @return map of uri to list of partition names
* @throws UnsupportedOperationException If the connector doesn't implement this method
*/
@Transactional(readOnly = true)
public Map<String, List<QualifiedName>> getTableNames(final List<String> uris, final boolean prefixSearch) {
final long start = registry.clock().wallTime();
// Create the sql
final StringBuilder queryBuilder = new StringBuilder(SQL.GET_TABLE_NAMES_BY_URI);
final List<SqlParameterValue> params = Lists.newArrayList();
if (prefixSearch) {
queryBuilder.append(" and (1=0");
uris.forEach(uri -> {
queryBuilder.append(" or location like ?");
params.add(new SqlParameterValue(Types.VARCHAR, uri + "%"));
});
queryBuilder.append(" )");
} else {
queryBuilder.append(" and location in (");
uris.forEach(uri -> {
queryBuilder.append("?,");
params.add(new SqlParameterValue(Types.VARCHAR, uri));
});
queryBuilder.deleteCharAt(queryBuilder.length() - 1).append(")");
}
ResultSetExtractor<Map<String, List<QualifiedName>>> handler = rs -> {
final Map<String, List<QualifiedName>> result = Maps.newHashMap();
while (rs.next()) {
final String schemaName = rs.getString("schema_name");
final String tableName = rs.getString("table_name");
final String uri = rs.getString("location");
final List<QualifiedName> names = result.computeIfAbsent(uri, k -> Lists.newArrayList());
names.add(QualifiedName.ofTable(catalogName, schemaName, tableName));
}
return result;
};
try {
return jdbcTemplate.query(queryBuilder.toString(), params.toArray(), handler);
} finally {
this.fastServiceMetric.recordTimer(HiveMetrics.TagGetTableNames.getMetricName(), registry.clock().wallTime() - start);
}
}
use of org.springframework.jdbc.core.ResultSetExtractor in project metacat by Netflix.
the class MysqlUserMetadataService method searchDefinitionMetadata.
@Override
@Transactional(readOnly = true)
public List<DefinitionMetadataDto> searchDefinitionMetadata(@Nullable final Set<String> propertyNames, @Nullable final String type, @Nullable final String name, @Nullable final HasMetadata holder, @Nullable final String sortBy, @Nullable final String sortOrder, @Nullable final Integer offset, @Nullable final Integer limit) {
final List<DefinitionMetadataDto> result = Lists.newArrayList();
final SearchMetadataQuery queryObj = new SearchMetadataQuery(SQL.SEARCH_DEFINITION_METADATAS).buildSearchMetadataQuery(propertyNames, type, name, sortBy, sortOrder, offset, limit);
try {
// Handler for reading the result set
final ResultSetExtractor<Void> handler = rs -> {
while (rs.next()) {
final String definitionName = rs.getString("name");
final String data = rs.getString("data");
final DefinitionMetadataDto definitionMetadataDto = new DefinitionMetadataDto();
definitionMetadataDto.setName(QualifiedName.fromString(definitionName));
definitionMetadataDto.setDefinitionMetadata(metacatJson.parseJsonObject(data));
result.add(definitionMetadataDto);
}
return null;
};
jdbcTemplate.query(queryObj.getSearchQuery().toString(), queryObj.getSearchParamList().toArray(), handler);
} catch (Exception e) {
log.error("Failed to search definition data", e);
throw new UserMetadataServiceException("Failed to search definition data", e);
}
return result;
}
use of org.springframework.jdbc.core.ResultSetExtractor in project metacat by Netflix.
the class MysqlUserMetadataService method searchByOwners.
@Override
@Transactional(readOnly = true)
public List<QualifiedName> searchByOwners(final Set<String> owners) {
final List<QualifiedName> result = Lists.newArrayList();
final StringBuilder query = new StringBuilder(SQL.SEARCH_DEFINITION_METADATA_NAMES);
final List<SqlParameterValue> paramList = Lists.newArrayList();
query.append(" where 1=0");
owners.forEach(s -> {
query.append(" or data like ?");
paramList.add(new SqlParameterValue(Types.VARCHAR, "%\"userId\":\"" + s.trim() + "\"%"));
});
final SqlParameterValue[] params = new SqlParameterValue[paramList.size()];
try {
// Handler for reading the result set
final ResultSetExtractor<Void> handler = rs -> {
while (rs.next()) {
final String definitionName = rs.getString("name");
result.add(QualifiedName.fromString(definitionName, false));
}
return null;
};
jdbcTemplate.query(query.toString(), paramList.toArray(params), handler);
} catch (Exception e) {
log.error("Failed to search by owners", e);
throw new UserMetadataServiceException("Failed to search by owners", e);
}
return result;
}
use of org.springframework.jdbc.core.ResultSetExtractor in project metacat by Netflix.
the class MysqlUserMetadataService method _getMetadataMap.
/**
* get Metadata Map.
*
* @param keys list of keys
* @param sql query string
* @return map of the metadata
*/
@SuppressWarnings("checkstyle:methodname")
private Map<String, ObjectNode> _getMetadataMap(@Nullable final List<?> keys, final String sql) {
final Map<String, ObjectNode> result = Maps.newHashMap();
if (keys == null || keys.isEmpty()) {
return result;
}
final List<String> paramVariables = keys.stream().map(s -> "?").collect(Collectors.toList());
final SqlParameterValue[] aKeys = keys.stream().map(o -> new SqlParameterValue(Types.VARCHAR, o.toString())).toArray(SqlParameterValue[]::new);
final String query = String.format(sql, Joiner.on("," + "").join(paramVariables));
try {
final ResultSetExtractor<Void> handler = resultSet -> {
while (resultSet.next()) {
final String json = resultSet.getString("data");
final String name = resultSet.getString("name");
if (json != null) {
try {
result.put(name, metacatJson.parseJsonObject(json));
} catch (MetacatJsonException e) {
log.error("Invalid json '{}' for name '{}'", json, name);
throw new UserMetadataServiceException(String.format("Invalid json %s for name %s", json, name), e);
}
}
}
return null;
};
jdbcTemplate.query(query, aKeys, handler);
} catch (Exception e) {
final String message = String.format("Failed to get data for %s", keys);
log.error(message, e);
throw new UserMetadataServiceException(message, e);
}
return result;
}
Aggregations