Search in sources :

Example 31 with UserMetadataServiceException

use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException in project metacat by Netflix.

the class MySqlTagService method delete.

@Override
public void delete(final QualifiedName name, final boolean updateUserMetadata) {
    try {
        jdbcTemplate.update(SQL_DELETE_TAG_ITEM_TAGS_BY_NAME, new SqlParameterValue(Types.VARCHAR, name.toString()));
        jdbcTemplate.update(SQL_DELETE_TAG_ITEM, new SqlParameterValue(Types.VARCHAR, name.toString()));
        if (updateUserMetadata) {
            // Set the tags in user metadata
            final Map<String, Set<String>> data = Maps.newHashMap();
            data.put(NAME_TAGS, Sets.newHashSet());
            userMetadataService.saveDefinitionMetadata(name, "admin", Optional.of(metacatJson.toJsonObject(data)), true);
        }
    } catch (Exception e) {
        final String message = String.format("Failed to delete all tags for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Set(java.util.Set) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 32 with UserMetadataServiceException

use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException in project metacat by Netflix.

the class MySqlTagService method search.

/**
 * Returns the list of <code>QualifiedName</code> of items that have tags containing the given tag text.
 *
 * @param tag          partial text of a tag
 * @param sourceName   source/catalog name
 * @param databaseName database name
 * @param tableName    table name
 * @return list of qualified names of the items
 */
@Override
@Transactional(readOnly = true)
public List<QualifiedName> search(@Nullable final String tag, @Nullable final String sourceName, @Nullable final String databaseName, @Nullable final String tableName) {
    final Set<String> result = Sets.newHashSet();
    try {
        final String wildCardName = QualifiedName.toWildCardString(sourceName, databaseName, tableName);
        // Includes
        final String query = String.format(QUERY_SEARCH, "like ?");
        final Object[] params = { tag == null ? 1 : 0, tag + "%", wildCardName == null ? 1 : 0, wildCardName };
        result.addAll(jdbcTemplate.query(query, params, new int[] { Types.INTEGER, Types.INTEGER, Types.VARCHAR }, (rs, rowNum) -> rs.getString("name")));
    } catch (Exception e) {
        final String message = String.format("Failed getting the list of qualified names for tag %s", tag);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    }
    return result.stream().map(QualifiedName::fromString).collect(Collectors.toList());
}
Also used : Lookup(com.netflix.metacat.common.server.model.Lookup) UserMetadataService(com.netflix.metacat.common.server.usermetadata.UserMetadataService) TagItem(com.netflix.metacat.common.server.model.TagItem) KeyHolder(org.springframework.jdbc.support.KeyHolder) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) SQLException(java.sql.SQLException) TagService(com.netflix.metacat.common.server.usermetadata.TagService) Map(java.util.Map) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) LookupService(com.netflix.metacat.common.server.usermetadata.LookupService) Config(com.netflix.metacat.common.server.properties.Config) Nullable(javax.annotation.Nullable) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) MetacatJson(com.netflix.metacat.common.json.MetacatJson) Set(java.util.Set) QualifiedName(com.netflix.metacat.common.QualifiedName) PreparedStatement(java.sql.PreparedStatement) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Statement(java.sql.Statement) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Joiner(com.google.common.base.Joiner) Transactional(org.springframework.transaction.annotation.Transactional) Types(java.sql.Types) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 33 with UserMetadataServiceException

use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException in project metacat by Netflix.

the class MySqlTagService method rename.

@Override
public void rename(final QualifiedName name, final String newTableName) {
    try {
        final QualifiedName newName = QualifiedName.ofTable(name.getCatalogName(), name.getDatabaseName(), newTableName);
        jdbcTemplate.update(SQL_UPDATE_TAG_ITEM, new String[] { newName.toString(), name.toString() }, new int[] { Types.VARCHAR, Types.VARCHAR });
    } catch (Exception e) {
        final String message = String.format("Failed to rename item name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) QualifiedName(com.netflix.metacat.common.QualifiedName) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 34 with UserMetadataServiceException

use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException in project metacat by Netflix.

the class MysqlUserMetadataService method saveMetadata.

@Override
public void saveMetadata(final String user, final List<? extends HasMetadata> metadatas, final boolean merge) {
    try {
        @SuppressWarnings("unchecked") final List<List<HasMetadata>> subLists = Lists.partition((List<HasMetadata>) metadatas, config.getUserMetadataMaxInClauseItems());
        for (List<HasMetadata> hasMetadatas : subLists) {
            final List<String> uris = Lists.newArrayList();
            final List<QualifiedName> names = Lists.newArrayList();
            // Get the names and uris
            final List<HasDefinitionMetadata> definitionMetadatas = Lists.newArrayList();
            final List<HasDataMetadata> dataMetadatas = Lists.newArrayList();
            hasMetadatas.forEach(hasMetadata -> {
                if (hasMetadata instanceof HasDefinitionMetadata) {
                    final HasDefinitionMetadata oDef = (HasDefinitionMetadata) hasMetadata;
                    names.add(oDef.getDefinitionName());
                    if (oDef.getDefinitionMetadata() != null) {
                        definitionMetadatas.add(oDef);
                    }
                }
                if (hasMetadata instanceof HasDataMetadata) {
                    final HasDataMetadata oData = (HasDataMetadata) hasMetadata;
                    if (oData.isDataExternal() && oData.getDataMetadata() != null && oData.getDataMetadata().size() > 0) {
                        uris.add(oData.getDataUri());
                        dataMetadatas.add(oData);
                    }
                }
            });
            if (!definitionMetadatas.isEmpty() || !dataMetadatas.isEmpty()) {
                // Get the existing metadata based on the names and uris
                final Map<String, ObjectNode> definitionMap = getDefinitionMetadataMap(names);
                final Map<String, ObjectNode> dataMap = getDataMetadataMap(uris);
                // Curate the list of existing and new metadatas
                final List<Object[]> insertDefinitionMetadatas = Lists.newArrayList();
                final List<Object[]> updateDefinitionMetadatas = Lists.newArrayList();
                final List<Object[]> insertPartitionDefinitionMetadatas = Lists.newArrayList();
                final List<Object[]> updatePartitionDefinitionMetadatas = Lists.newArrayList();
                final List<Object[]> insertDataMetadatas = Lists.newArrayList();
                final List<Object[]> updateDataMetadatas = Lists.newArrayList();
                definitionMetadatas.forEach(oDef -> {
                    final QualifiedName qualifiedName = oDef.getDefinitionName();
                    if (qualifiedName != null && oDef.getDefinitionMetadata() != null && oDef.getDefinitionMetadata().size() != 0) {
                        final String name = qualifiedName.toString();
                        final ObjectNode oNode = definitionMap.get(name);
                        if (oNode == null) {
                            final Object[] o = new Object[] { metacatJson.toJsonString(oDef.getDefinitionMetadata()), user, user, name };
                            if (qualifiedName.isPartitionDefinition()) {
                                insertPartitionDefinitionMetadatas.add(o);
                            } else {
                                insertDefinitionMetadatas.add(o);
                            }
                        } else {
                            metacatJson.mergeIntoPrimary(oNode, oDef.getDefinitionMetadata());
                            final Object[] o = new Object[] { metacatJson.toJsonString(oNode), user, name };
                            if (qualifiedName.isPartitionDefinition()) {
                                updatePartitionDefinitionMetadatas.add(o);
                            } else {
                                updateDefinitionMetadatas.add(o);
                            }
                        }
                    }
                });
                dataMetadatas.forEach(oData -> {
                    final String uri = oData.getDataUri();
                    final ObjectNode oNode = dataMap.get(uri);
                    if (oData.getDataMetadata() != null && oData.getDataMetadata().size() != 0) {
                        if (oNode == null) {
                            insertDataMetadatas.add(new Object[] { metacatJson.toJsonString(oData.getDataMetadata()), user, user, uri });
                        } else {
                            metacatJson.mergeIntoPrimary(oNode, oData.getDataMetadata());
                            updateDataMetadatas.add(new Object[] { metacatJson.toJsonString(oNode), user, uri });
                        }
                    }
                });
                if (!insertDefinitionMetadatas.isEmpty()) {
                    jdbcTemplate.batchUpdate(SQL.INSERT_DEFINITION_METADATA, insertDefinitionMetadatas, new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
                }
                if (!updateDefinitionMetadatas.isEmpty()) {
                    jdbcTemplate.batchUpdate(SQL.UPDATE_DEFINITION_METADATA, updateDefinitionMetadatas, new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
                }
                if (!insertPartitionDefinitionMetadatas.isEmpty()) {
                    jdbcTemplate.batchUpdate(SQL.INSERT_PARTITION_DEFINITION_METADATA, insertPartitionDefinitionMetadatas, new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
                }
                if (!updatePartitionDefinitionMetadatas.isEmpty()) {
                    jdbcTemplate.batchUpdate(SQL.UPDATE_PARTITION_DEFINITION_METADATA, updatePartitionDefinitionMetadatas, new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
                }
                if (!insertDataMetadatas.isEmpty()) {
                    jdbcTemplate.batchUpdate(SQL.INSERT_DATA_METADATA, insertDataMetadatas, new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
                }
                if (!updateDataMetadatas.isEmpty()) {
                    jdbcTemplate.batchUpdate(SQL.UPDATE_DATA_METADATA, updateDataMetadatas, new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
                }
            }
        }
    } catch (Exception e) {
        log.error("Failed to save metadata", e);
        throw new UserMetadataServiceException("Failed to save metadata", e);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) HasMetadata(com.netflix.metacat.common.dto.HasMetadata) HasDataMetadata(com.netflix.metacat.common.dto.HasDataMetadata) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) QualifiedName(com.netflix.metacat.common.QualifiedName) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) InvalidMetadataException(com.netflix.metacat.common.server.connectors.exception.InvalidMetadataException) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) List(java.util.List) HasDefinitionMetadata(com.netflix.metacat.common.dto.HasDefinitionMetadata)

Example 35 with UserMetadataServiceException

use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException 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;
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) HasDataMetadata(com.netflix.metacat.common.dto.HasDataMetadata) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DefinitionMetadataDto(com.netflix.metacat.common.dto.DefinitionMetadataDto) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) Map(java.util.Map) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Config(com.netflix.metacat.common.server.properties.Config) BaseUserMetadataService(com.netflix.metacat.common.server.usermetadata.BaseUserMetadataService) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) InvalidMetadataException(com.netflix.metacat.common.server.connectors.exception.InvalidMetadataException) MetacatJson(com.netflix.metacat.common.json.MetacatJson) HasDefinitionMetadata(com.netflix.metacat.common.dto.HasDefinitionMetadata) Set(java.util.Set) QualifiedName(com.netflix.metacat.common.QualifiedName) Maps(com.google.common.collect.Maps) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Collectors(java.util.stream.Collectors) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) HasMetadata(com.netflix.metacat.common.dto.HasMetadata) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Data(lombok.Data) GetMetadataInterceptorParameters(com.netflix.metacat.common.server.usermetadata.GetMetadataInterceptorParameters) MetadataInterceptor(com.netflix.metacat.common.server.usermetadata.MetadataInterceptor) Optional(java.util.Optional) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) Joiner(com.google.common.base.Joiner) Transactional(org.springframework.transaction.annotation.Transactional) Types(java.sql.Types) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) QualifiedName(com.netflix.metacat.common.QualifiedName) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) InvalidMetadataException(com.netflix.metacat.common.server.connectors.exception.InvalidMetadataException) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UserMetadataServiceException (com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)38 SQLException (java.sql.SQLException)33 Connection (java.sql.Connection)23 QualifiedName (com.netflix.metacat.common.QualifiedName)15 List (java.util.List)15 Set (java.util.Set)15 QueryRunner (org.apache.commons.dbutils.QueryRunner)15 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)10 Joiner (com.google.common.base.Joiner)10 Maps (com.google.common.collect.Maps)10 MetacatJson (com.netflix.metacat.common.json.MetacatJson)10 Config (com.netflix.metacat.common.server.properties.Config)10 Map (java.util.Map)10 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)10 Slf4j (lombok.extern.slf4j.Slf4j)10 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)10 HasDataMetadata (com.netflix.metacat.common.dto.HasDataMetadata)9 HasDefinitionMetadata (com.netflix.metacat.common.dto.HasDefinitionMetadata)9 HasMetadata (com.netflix.metacat.common.dto.HasMetadata)9