Search in sources :

Example 1 with EntityPartialUpdateRequestV2

use of org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2 in project atlas by apache.

the class HookNotificationTest method testEntityPartialUpdateV2SerDe.

@Test
public void testEntityPartialUpdateV2SerDe() throws Exception {
    AtlasEntity entity1 = new AtlasEntity("sometype");
    AtlasEntity entity2 = new AtlasEntity("newtype");
    AtlasEntity entity3 = new AtlasEntity("othertype");
    setAttributes(entity1);
    entity1.setAttribute("complex", new AtlasObjectId(entity3.getGuid(), entity3.getTypeName()));
    AtlasEntityWithExtInfo entity = new AtlasEntityWithExtInfo(entity1);
    entity.addReferredEntity(entity2);
    entity.addReferredEntity(entity3);
    String user = "user";
    EntityPartialUpdateRequestV2 request = new EntityPartialUpdateRequestV2(user, AtlasTypeUtil.getAtlasObjectId(entity1), entity);
    String notificationJson = AtlasJson.toJson(request);
    HookNotification actualNotification = deserializer.deserialize(notificationJson);
    assertEquals(actualNotification.getType(), HookNotificationType.ENTITY_PARTIAL_UPDATE_V2);
    assertEquals(actualNotification.getUser(), user);
    EntityPartialUpdateRequestV2 updateRequest = (EntityPartialUpdateRequestV2) actualNotification;
    assertEquals(updateRequest.getEntity().getReferredEntities().size(), 2);
    AtlasEntity actualEntity1 = updateRequest.getEntity().getEntity();
    AtlasEntity actualEntity2 = updateRequest.getEntity().getReferredEntity(entity2.getGuid());
    AtlasEntity actualEntity3 = updateRequest.getEntity().getReferredEntity(entity3.getGuid());
    Map actualComplexAttr = (Map) actualEntity1.getAttribute("complex");
    assertEquals(actualEntity1.getGuid(), entity1.getGuid());
    assertEquals(actualEntity1.getTypeName(), entity1.getTypeName());
    assertAttributes(actualEntity1);
    assertEquals(actualComplexAttr.get(AtlasObjectId.KEY_GUID), entity3.getGuid());
    assertEquals(actualComplexAttr.get(AtlasObjectId.KEY_TYPENAME), entity3.getTypeName());
    assertEquals(actualEntity2.getGuid(), entity2.getGuid());
    assertEquals(actualEntity2.getTypeName(), entity2.getTypeName());
    assertEquals(actualEntity3.getGuid(), entity3.getGuid());
    assertEquals(actualEntity3.getTypeName(), entity3.getTypeName());
}
Also used : EntityPartialUpdateRequestV2(org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2) HookNotification(org.apache.atlas.model.notification.HookNotification) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 2 with EntityPartialUpdateRequestV2

use of org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2 in project atlas by apache.

the class AlterTableRename method getNotificationMessages.

@Override
public List<HookNotification> getNotificationMessages() throws Exception {
    List<HookNotification> ret = new ArrayList<>();
    if (CollectionUtils.isEmpty(getHiveContext().getInputs())) {
        LOG.error("AlterTableRename: old-table not found in inputs list");
        return ret;
    }
    Table oldTable = getHiveContext().getInputs().iterator().next().getTable();
    Table newTable = null;
    if (CollectionUtils.isNotEmpty(getHiveContext().getOutputs())) {
        for (WriteEntity entity : getHiveContext().getOutputs()) {
            if (entity.getType() == Entity.Type.TABLE) {
                newTable = entity.getTable();
                // Hive sends with both old and new table names in the outputs which is weird. So skipping that with the below check
                if (StringUtils.equalsIgnoreCase(newTable.getDbName(), oldTable.getDbName()) && StringUtils.equalsIgnoreCase(newTable.getTableName(), oldTable.getTableName())) {
                    newTable = null;
                    continue;
                }
                break;
            }
        }
    }
    if (newTable == null) {
        LOG.error("AlterTableRename: renamed table not found in outputs list");
        return ret;
    }
    AtlasEntityWithExtInfo oldTableEntity = toTableEntity(oldTable);
    // first update with oldTable info, so that the table will be created if it is not present in Atlas
    ret.add(new EntityUpdateRequestV2(getUserName(), new AtlasEntitiesWithExtInfo(oldTableEntity)));
    // update qualifiedName for all columns, partitionKeys, storageDesc
    String newTableQualifiedName = getQualifiedName(newTable);
    renameColumns((List<AtlasObjectId>) oldTableEntity.getEntity().getAttribute(ATTRIBUTE_COLUMNS), oldTableEntity, newTableQualifiedName, ret);
    renameColumns((List<AtlasObjectId>) oldTableEntity.getEntity().getAttribute(ATTRIBUTE_PARTITION_KEYS), oldTableEntity, newTableQualifiedName, ret);
    renameStorageDesc((AtlasObjectId) oldTableEntity.getEntity().getAttribute(ATTRIBUTE_STORAGEDESC), oldTableEntity, newTableQualifiedName, ret);
    // update qualifiedName and other attributes (like params - which include lastModifiedTime, lastModifiedBy) of the table
    AtlasEntityWithExtInfo newTableEntity = toTableEntity(newTable);
    // set previous name as the alias
    newTableEntity.getEntity().setAttribute(ATTRIBUTE_ALIASES, Collections.singletonList(oldTable.getTableName()));
    // remove columns, partitionKeys and storageDesc - as they have already been updated above
    removeAttribute(newTableEntity, ATTRIBUTE_COLUMNS);
    removeAttribute(newTableEntity, ATTRIBUTE_PARTITION_KEYS);
    removeAttribute(newTableEntity, ATTRIBUTE_STORAGEDESC);
    AtlasObjectId oldTableId = new AtlasObjectId(oldTableEntity.getEntity().getTypeName(), ATTRIBUTE_QUALIFIED_NAME, oldTableEntity.getEntity().getAttribute(ATTRIBUTE_QUALIFIED_NAME));
    ret.add(new EntityPartialUpdateRequestV2(getUserName(), oldTableId, newTableEntity));
    context.removeFromKnownTable((String) oldTableEntity.getEntity().getAttribute(ATTRIBUTE_QUALIFIED_NAME));
    return ret;
}
Also used : HookNotification(org.apache.atlas.model.notification.HookNotification) Table(org.apache.hadoop.hive.ql.metadata.Table) EntityPartialUpdateRequestV2(org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) ArrayList(java.util.ArrayList) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) WriteEntity(org.apache.hadoop.hive.ql.hooks.WriteEntity) EntityUpdateRequestV2(org.apache.atlas.model.notification.HookNotification.EntityUpdateRequestV2)

Example 3 with EntityPartialUpdateRequestV2

use of org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2 in project atlas by apache.

the class AlterTableRename method renameColumns.

private void renameColumns(List<AtlasObjectId> columns, AtlasEntityExtInfo oldEntityExtInfo, String newTableQualifiedName, List<HookNotification> notifications) {
    if (CollectionUtils.isNotEmpty(columns)) {
        for (AtlasObjectId columnId : columns) {
            AtlasEntity oldColumn = oldEntityExtInfo.getEntity(columnId.getGuid());
            AtlasObjectId oldColumnId = new AtlasObjectId(oldColumn.getTypeName(), ATTRIBUTE_QUALIFIED_NAME, oldColumn.getAttribute(ATTRIBUTE_QUALIFIED_NAME));
            AtlasEntity newColumn = new AtlasEntity(oldColumn.getTypeName(), ATTRIBUTE_QUALIFIED_NAME, getColumnQualifiedName(newTableQualifiedName, (String) oldColumn.getAttribute(ATTRIBUTE_NAME)));
            notifications.add(new EntityPartialUpdateRequestV2(getUserName(), oldColumnId, new AtlasEntityWithExtInfo(newColumn)));
        }
    }
}
Also used : EntityPartialUpdateRequestV2(org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId)

Example 4 with EntityPartialUpdateRequestV2

use of org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2 in project atlas by apache.

the class AlterTableRename method renameStorageDesc.

private void renameStorageDesc(AtlasObjectId sdId, AtlasEntityExtInfo oldEntityExtInfo, String newTableQualifiedName, List<HookNotification> notifications) {
    if (sdId != null) {
        AtlasEntity oldSd = oldEntityExtInfo.getEntity(sdId.getGuid());
        AtlasObjectId oldSdId = new AtlasObjectId(oldSd.getTypeName(), ATTRIBUTE_QUALIFIED_NAME, oldSd.getAttribute(ATTRIBUTE_QUALIFIED_NAME));
        AtlasEntity newSd = new AtlasEntity(oldSd.getTypeName(), ATTRIBUTE_QUALIFIED_NAME, getStorageDescQualifiedName(newTableQualifiedName));
        notifications.add(new EntityPartialUpdateRequestV2(getUserName(), oldSdId, new AtlasEntityWithExtInfo(newSd)));
    }
}
Also used : EntityPartialUpdateRequestV2(org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId)

Example 5 with EntityPartialUpdateRequestV2

use of org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2 in project atlas by apache.

the class AlterTableRenameCol method getNotificationMessages.

@Override
public List<HookNotification> getNotificationMessages() throws Exception {
    if (CollectionUtils.isEmpty(getHiveContext().getInputs())) {
        LOG.error("AlterTableRenameCol: old-table not found in inputs list");
        return null;
    }
    if (CollectionUtils.isEmpty(getHiveContext().getOutputs())) {
        LOG.error("AlterTableRenameCol: new-table not found in outputs list");
        return null;
    }
    List<HookNotification> ret = new ArrayList<>(super.getNotificationMessages());
    Table oldTable = getHiveContext().getInputs().iterator().next().getTable();
    Table newTable = getHiveContext().getOutputs().iterator().next().getTable();
    newTable = getHive().getTable(newTable.getDbName(), newTable.getTableName());
    List<FieldSchema> oldColumns = oldTable.getCols();
    List<FieldSchema> newColumns = newTable.getCols();
    FieldSchema changedColumnOld = null;
    FieldSchema changedColumnNew = null;
    for (FieldSchema oldColumn : oldColumns) {
        if (!newColumns.contains(oldColumn)) {
            changedColumnOld = oldColumn;
            break;
        }
    }
    for (FieldSchema newColumn : newColumns) {
        if (!oldColumns.contains(newColumn)) {
            changedColumnNew = newColumn;
            break;
        }
    }
    if (changedColumnOld != null && changedColumnNew != null) {
        AtlasObjectId oldColumnId = new AtlasObjectId(HIVE_TYPE_COLUMN, ATTRIBUTE_QUALIFIED_NAME, getQualifiedName(oldTable, changedColumnOld));
        AtlasEntity newColumn = new AtlasEntity(HIVE_TYPE_COLUMN);
        newColumn.setAttribute(ATTRIBUTE_NAME, changedColumnNew.getName());
        newColumn.setAttribute(ATTRIBUTE_QUALIFIED_NAME, getQualifiedName(newTable, changedColumnNew));
        ret.add(0, new EntityPartialUpdateRequestV2(getUserName(), oldColumnId, new AtlasEntityWithExtInfo(newColumn)));
    } else {
        LOG.error("AlterTableRenameCol: no renamed column detected");
    }
    return ret;
}
Also used : HookNotification(org.apache.atlas.model.notification.HookNotification) Table(org.apache.hadoop.hive.ql.metadata.Table) EntityPartialUpdateRequestV2(org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) ArrayList(java.util.ArrayList) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId)

Aggregations

AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)5 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)5 EntityPartialUpdateRequestV2 (org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2)5 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)4 HookNotification (org.apache.atlas.model.notification.HookNotification)3 ArrayList (java.util.ArrayList)2 Table (org.apache.hadoop.hive.ql.metadata.Table)2 Map (java.util.Map)1 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)1 EntityUpdateRequestV2 (org.apache.atlas.model.notification.HookNotification.EntityUpdateRequestV2)1 FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)1 WriteEntity (org.apache.hadoop.hive.ql.hooks.WriteEntity)1 Test (org.testng.annotations.Test)1