Search in sources :

Example 21 with UserMetadataServiceException

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

the class MysqlUserMetadataService method searchDefinitionMetadatas.

@Override
public List<DefinitionMetadataDto> searchDefinitionMetadatas(final Set<String> propertyNames, final String type, final String name, final String sortBy, final String sortOrder, final Integer offset, final Integer limit) {
    final List<DefinitionMetadataDto> result = Lists.newArrayList();
    final StringBuilder query = new StringBuilder(SQL.SEARCH_DEFINITION_METADATAS);
    final List<Object> paramList = Lists.newArrayList();
    if (type != null) {
        String typeRegex = null;
        switch(type) {
            case "database":
                typeRegex = "^[^/]*/[^/]*$";
                break;
            case "table":
                typeRegex = "^[^/]*/[^/]*/[^/]*$";
                break;
            case "partition":
                typeRegex = "^[^/]*/[^/]*/[^/]*/.*$";
                break;
            default:
        }
        if (typeRegex != null) {
            query.append(" and name rlike ?");
            paramList.add(typeRegex);
        }
    }
    if (propertyNames != null && !propertyNames.isEmpty()) {
        propertyNames.forEach(propertyName -> {
            query.append(" and data like ?");
            paramList.add("%\"" + propertyName + "\":%");
        });
    }
    if (!Strings.isNullOrEmpty(name)) {
        query.append(" and name like ?");
        paramList.add(name);
    }
    if (!Strings.isNullOrEmpty(sortBy)) {
        query.append(" order by ").append(sortBy);
        if (!Strings.isNullOrEmpty(sortOrder)) {
            query.append(" ").append(sortOrder);
        }
    }
    if (limit != null) {
        query.append(" limit ");
        if (offset != null) {
            query.append(offset).append(",");
        }
        query.append(limit);
    }
    final Object[] params = new Object[paramList.size()];
    final Connection connection = DBUtil.getReadConnection(poolingDataSource);
    try {
        // Handler for reading the result set
        final ResultSetHandler<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;
        };
        new QueryRunner().query(connection, query.toString(), handler, paramList.toArray(params));
    } catch (SQLException e) {
        log.error("Sql exception", e);
        throw new UserMetadataServiceException("Failed to get definition data", e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    return result;
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) URL(java.net.URL) Date(java.util.Date) HasDataMetadata(com.netflix.metacat.common.dto.HasDataMetadata) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DefinitionMetadataDto(com.netflix.metacat.common.dto.DefinitionMetadataDto) Strings(com.google.common.base.Strings) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) ResultSet(java.sql.ResultSet) Map(java.util.Map) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) DataSource(javax.sql.DataSource) Config(com.netflix.metacat.common.server.properties.Config) BaseUserMetadataService(com.netflix.metacat.common.server.usermetadata.BaseUserMetadataService) Nonnull(javax.annotation.Nonnull) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) Charsets(com.google.common.base.Charsets) DataSourceManager(com.netflix.metacat.common.server.util.DataSourceManager) Properties(java.util.Properties) QueryRunner(org.apache.commons.dbutils.QueryRunner) MetacatJson(com.netflix.metacat.common.json.MetacatJson) Files(java.nio.file.Files) HasDefinitionMetadata(com.netflix.metacat.common.dto.HasDefinitionMetadata) Set(java.util.Set) QualifiedName(com.netflix.metacat.common.QualifiedName) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) Maps(com.google.common.collect.Maps) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Collectors(java.util.stream.Collectors) HasMetadata(com.netflix.metacat.common.dto.HasMetadata) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) DBUtil(com.netflix.metacat.common.server.util.DBUtil) Paths(java.nio.file.Paths) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ColumnListHandler(org.apache.commons.dbutils.handlers.ColumnListHandler) Collections(java.util.Collections) ResultSetHandler(org.apache.commons.dbutils.ResultSetHandler) FileSystems(java.nio.file.FileSystems) Joiner(com.google.common.base.Joiner) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryRunner(org.apache.commons.dbutils.QueryRunner) DefinitionMetadataDto(com.netflix.metacat.common.dto.DefinitionMetadataDto)

Example 22 with UserMetadataServiceException

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

the class MySqlLookupService method get.

/**
     * Returns the lookup for the given <code>name</code>.
     *
     * @param name lookup name
     * @return lookup
     */
@Override
public Lookup get(final String name) {
    Lookup result;
    final Connection connection = DBUtil.getReadConnection(getDataSource());
    try {
        final ResultSetHandler<Lookup> handler = new BeanHandler<>(Lookup.class);
        result = new QueryRunner().query(connection, SQL_GET_LOOKUP, handler, name);
        if (result != null) {
            result.setValues(getValues(result.getId()));
        }
    } catch (Exception e) {
        final String message = String.format("Failed to get the lookup for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    return result;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Connection(java.sql.Connection) BeanHandler(org.apache.commons.dbutils.handlers.BeanHandler) Lookup(com.netflix.metacat.common.server.model.Lookup) QueryRunner(org.apache.commons.dbutils.QueryRunner) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)

Example 23 with UserMetadataServiceException

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

the class MySqlLookupService method addValues.

/**
     * Saves the lookup value.
     *
     * @param name   lookup name
     * @param values multiple values
     * @return returns the lookup with the given name.
     */
@Override
public Lookup addValues(final String name, final Set<String> values) {
    Lookup lookup;
    try {
        final Connection conn = getDataSource().getConnection();
        try {
            lookup = findOrCreateLookupByName(name, conn);
            final Set<String> inserts;
            final Set<String> lookupValues = lookup.getValues();
            if (lookupValues == null || lookupValues.isEmpty()) {
                inserts = values;
                lookup.setValues(values);
            } else {
                inserts = Sets.difference(values, lookupValues);
            }
            if (!inserts.isEmpty()) {
                insertLookupValues(lookup.getId(), inserts, conn);
            }
            conn.commit();
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.close();
        }
    } catch (SQLException e) {
        final String message = String.format("Failed to set the lookup values for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    }
    return lookup;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Lookup(com.netflix.metacat.common.server.model.Lookup)

Example 24 with UserMetadataServiceException

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

the class MySqlTagService method get.

/**
     * Returns the TagItem for the given <code>name</code>.
     *
     * @param name tag name
     * @return TagItem
     */
public TagItem get(final String name) {
    TagItem result = null;
    final Connection connection = DBUtil.getReadConnection(getDataSource());
    try {
        final ResultSetHandler<TagItem> handler = new BeanHandler<>(TagItem.class);
        result = new QueryRunner().query(connection, SQL_GET_TAG_ITEM, handler, name);
        if (result != null) {
            result.setValues(getValues(result.getId()));
        }
    } catch (Exception e) {
        final String message = String.format("Failed to get the tag item for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    return result;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) TagItem(com.netflix.metacat.common.server.model.TagItem) Connection(java.sql.Connection) BeanHandler(org.apache.commons.dbutils.handlers.BeanHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)

Example 25 with UserMetadataServiceException

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

the class MySqlTagService method setTableTags.

/**
     * Tags the given table with the given <code>tags</code>.
     *
     * @param name table name
     * @param tags list of tags
     * @return return the complete list of tags associated with the table
     */
@Override
public Set<String> setTableTags(final QualifiedName name, final Set<String> tags, final boolean updateUserMetadata) {
    addTags(tags);
    try {
        final Connection conn = getDataSource().getConnection();
        try {
            final TagItem tagItem = findOrCreateTagItemByName(name.toString(), conn);
            final Set<String> inserts;
            Set<String> deletes = Sets.newHashSet();
            Set<String> values = tagItem.getValues();
            if (values == null || values.isEmpty()) {
                inserts = tags;
            } else {
                inserts = Sets.difference(tags, values).immutableCopy();
                deletes = Sets.difference(values, tags).immutableCopy();
            }
            values = tags;
            if (!inserts.isEmpty()) {
                insertTagItemTags(tagItem.getId(), inserts, conn);
            }
            if (!deletes.isEmpty()) {
                removeTagItemTags(tagItem.getId(), deletes, conn);
            }
            if (updateUserMetadata) {
                // Set the tags in user metadata
                final Map<String, Set<String>> data = Maps.newHashMap();
                data.put(NAME_TAGS, values);
                userMetadataService.saveDefinitionMetadata(name, "admin", Optional.of(metacatJson.toJsonObject(data)), true);
            }
            conn.commit();
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.close();
        }
    } catch (SQLException e) {
        final String message = String.format("Failed to remove tags for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    }
    return tags;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) TagItem(com.netflix.metacat.common.server.model.TagItem) Set(java.util.Set) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

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