Search in sources :

Example 6 with HasDefinitionMetadata

use of com.netflix.metacat.common.dto.HasDefinitionMetadata in project metacat by Netflix.

the class BaseUserMetadataService method populateMetadata.

/**
 * Populate metadata.
 *
 * @param holder             metadata
 * @param definitionMetadata definition metadata
 * @param dataMetadata       data metadata
 */
@Override
public void populateMetadata(final HasMetadata holder, final ObjectNode definitionMetadata, final ObjectNode dataMetadata) {
    if (holder instanceof HasDefinitionMetadata) {
        final HasDefinitionMetadata defDto = (HasDefinitionMetadata) holder;
        defDto.setDefinitionMetadata(definitionMetadata);
    }
    if (holder instanceof HasDataMetadata) {
        final HasDataMetadata dataDto = (HasDataMetadata) holder;
        // data Metadata can be populated from iceberg metrics directly
        if (dataDto.isDataExternal() || dataMetadata != null) {
            dataDto.setDataMetadata(dataMetadata);
        }
    }
}
Also used : HasDataMetadata(com.netflix.metacat.common.dto.HasDataMetadata) HasDefinitionMetadata(com.netflix.metacat.common.dto.HasDefinitionMetadata)

Example 7 with HasDefinitionMetadata

use of com.netflix.metacat.common.dto.HasDefinitionMetadata in project metacat by Netflix.

the class BaseUserMetadataService method saveMetadata.

/**
 * Saves user metadata.
 *
 * @param userId user name
 * @param holder metadata
 * @param merge  true if the metadata should be merged with existing metadata
 */
@Override
public void saveMetadata(final String userId, final HasMetadata holder, final boolean merge) {
    if (holder instanceof HasDefinitionMetadata) {
        final HasDefinitionMetadata defDto = (HasDefinitionMetadata) holder;
        // If the user is updating the definition metadata do a merge on the existing metadata
        final ObjectNode newMetadata = defDto.getDefinitionMetadata();
        if (newMetadata != null) {
            saveDefinitionMetadata(defDto.getDefinitionName(), userId, Optional.of(newMetadata), merge);
        }
    }
    if (holder instanceof HasDataMetadata) {
        final HasDataMetadata dataDto = (HasDataMetadata) holder;
        // If the user is updating the data metadata and a separate data location exists,
        // do a merge on the existing metadata
        final ObjectNode newMetadata = dataDto.getDataMetadata();
        if (newMetadata != null && dataDto.isDataExternal()) {
            saveDataMetadata(dataDto.getDataUri(), userId, Optional.of(newMetadata), merge);
        }
    }
}
Also used : HasDataMetadata(com.netflix.metacat.common.dto.HasDataMetadata) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HasDefinitionMetadata(com.netflix.metacat.common.dto.HasDefinitionMetadata)

Example 8 with HasDefinitionMetadata

use of com.netflix.metacat.common.dto.HasDefinitionMetadata 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()) {
                    throwIfPartitionDefinitionMetadataDisabled();
                    jdbcTemplate.batchUpdate(SQL.INSERT_PARTITION_DEFINITION_METADATA, insertPartitionDefinitionMetadatas, new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
                }
                if (!updatePartitionDefinitionMetadatas.isEmpty()) {
                    throwIfPartitionDefinitionMetadataDisabled();
                    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) MetacatBadRequestException(com.netflix.metacat.common.exception.MetacatBadRequestException) 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)

Aggregations

HasDefinitionMetadata (com.netflix.metacat.common.dto.HasDefinitionMetadata)8 HasDataMetadata (com.netflix.metacat.common.dto.HasDataMetadata)7 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 QualifiedName (com.netflix.metacat.common.QualifiedName)3 HasMetadata (com.netflix.metacat.common.dto.HasMetadata)3 UserMetadataServiceException (com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)3 List (java.util.List)3 MetacatJsonException (com.netflix.metacat.common.json.MetacatJsonException)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 QueryRunner (org.apache.commons.dbutils.QueryRunner)2 Charsets (com.google.common.base.Charsets)1 Joiner (com.google.common.base.Joiner)1 Preconditions (com.google.common.base.Preconditions)1 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)1 BaseDto (com.netflix.metacat.common.dto.BaseDto)1 DefinitionMetadataDto (com.netflix.metacat.common.dto.DefinitionMetadataDto)1