Search in sources :

Example 26 with UserMetadataServiceException

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

the class MysqlUserMetadataService method _getMetadataMap.

@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 String[] aKeys = keys.stream().map(Object::toString).toArray(String[]::new);
    final String query = String.format(sql, Joiner.on(",").join(paramVariables));
    final Connection connection = DBUtil.getReadConnection(poolingDataSource);
    try {
        final ResultSetHandler<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;
        };
        new QueryRunner().query(connection, query, handler, (Object[]) aKeys);
    } catch (SQLException e) {
        log.error("Sql exception", e);
        throw new UserMetadataServiceException(String.format("Failed to get data for %s", keys), 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) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SQLException(java.sql.SQLException) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Connection(java.sql.Connection) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 27 with UserMetadataServiceException

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

the class MysqlUserMetadataService method searchByOwners.

@Override
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<Object> paramList = Lists.newArrayList();
    query.append(" where 1=0");
    owners.forEach(s -> {
        query.append(" or data like ?");
        paramList.add("%\"userId\":\"" + s.trim() + "\"%");
    });
    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");
                result.add(QualifiedName.fromString(definitionName, false));
            }
            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) QualifiedName(com.netflix.metacat.common.QualifiedName) Connection(java.sql.Connection) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 28 with UserMetadataServiceException

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

the class MysqlUserMetadataService method saveMetadatas.

@Override
public void saveMetadatas(final String user, final List<? extends HasMetadata> metadatas, final boolean merge) {
    try {
        final Connection conn = poolingDataSource.getConnection();
        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.stream().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[]> insertDataMetadatas = Lists.newArrayList();
                    final List<Object[]> updateDataMetadatas = Lists.newArrayList();
                    definitionMetadatas.stream().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) {
                                insertDefinitionMetadatas.add(new Object[] { metacatJson.toJsonString(oDef.getDefinitionMetadata()), user, user, name });
                            } else {
                                metacatJson.mergeIntoPrimary(oNode, oDef.getDefinitionMetadata());
                                updateDefinitionMetadatas.add(new Object[] { metacatJson.toJsonString(oNode), user, name });
                            }
                        }
                    });
                    dataMetadatas.stream().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 });
                            }
                        }
                    });
                    //Now run the queries
                    final QueryRunner runner = new QueryRunner();
                    if (!insertDefinitionMetadatas.isEmpty()) {
                        runner.batch(conn, SQL.INSERT_DEFINITION_METADATA, insertDefinitionMetadatas.toArray(new Object[insertDefinitionMetadatas.size()][4]));
                    }
                    if (!updateDefinitionMetadatas.isEmpty()) {
                        runner.batch(conn, SQL.UPDATE_DEFINITION_METADATA, updateDefinitionMetadatas.toArray(new Object[updateDefinitionMetadatas.size()][3]));
                    }
                    if (!insertDataMetadatas.isEmpty()) {
                        runner.batch(conn, SQL.INSERT_DATA_METADATA, insertDataMetadatas.toArray(new Object[insertDataMetadatas.size()][4]));
                    }
                    if (!updateDataMetadatas.isEmpty()) {
                        runner.batch(conn, SQL.UPDATE_DATA_METADATA, updateDataMetadatas.toArray(new Object[updateDataMetadatas.size()][3]));
                    }
                }
            }
            conn.commit();
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.close();
        }
    } catch (SQLException e) {
        log.error("Sql exception", 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) SQLException(java.sql.SQLException) QualifiedName(com.netflix.metacat.common.QualifiedName) Connection(java.sql.Connection) QueryRunner(org.apache.commons.dbutils.QueryRunner) List(java.util.List) HasDefinitionMetadata(com.netflix.metacat.common.dto.HasDefinitionMetadata)

Example 29 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
@Transactional(readOnly = true)
public Lookup get(final String name) {
    try {
        return jdbcTemplate.queryForObject(SQL_GET_LOOKUP, new Object[] { name }, new int[] { Types.VARCHAR }, (rs, rowNum) -> {
            final Lookup lookup = new Lookup();
            lookup.setId(rs.getLong("id"));
            lookup.setName(rs.getString("name"));
            lookup.setType(rs.getString("type"));
            lookup.setCreatedBy(rs.getString("createdBy"));
            lookup.setLastUpdated(rs.getDate("lastUpdated"));
            lookup.setLastUpdatedBy(rs.getString("lastUpdatedBy"));
            lookup.setDateCreated(rs.getDate("dateCreated"));
            lookup.setValues(getValues(rs.getLong("id")));
            return lookup;
        });
    } catch (EmptyResultDataAccessException e) {
        return null;
    } 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);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Lookup(com.netflix.metacat.common.server.model.Lookup) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 30 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) {
    try {
        final Lookup lookup = findOrCreateLookupByName(name);
        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);
        }
        return lookup;
    } catch (Exception 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);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Lookup(com.netflix.metacat.common.server.model.Lookup) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

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