Search in sources :

Example 6 with EntityUpdateRequestV2

use of org.apache.atlas.model.notification.HookNotification.EntityUpdateRequestV2 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 7 with EntityUpdateRequestV2

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

the class HookNotificationTest method testEntityUpdateV2SerDe.

@Test
public void testEntityUpdateV2SerDe() 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()));
    AtlasEntitiesWithExtInfo entities = new AtlasEntitiesWithExtInfo();
    entities.addEntity(entity1);
    entities.addEntity(entity2);
    entities.addReferredEntity(entity3);
    String user = "user";
    EntityUpdateRequestV2 request = new EntityUpdateRequestV2(user, entities);
    String notificationJson = AtlasJson.toJson(request);
    HookNotification actualNotification = deserializer.deserialize(notificationJson);
    assertEquals(actualNotification.getType(), HookNotificationType.ENTITY_FULL_UPDATE_V2);
    assertEquals(actualNotification.getUser(), user);
    EntityUpdateRequestV2 updateRequest = (EntityUpdateRequestV2) actualNotification;
    assertEquals(updateRequest.getEntities().getEntities().size(), 2);
    AtlasEntity actualEntity1 = updateRequest.getEntities().getEntities().get(0);
    AtlasEntity actualEntity2 = updateRequest.getEntities().getEntities().get(1);
    AtlasEntity actualEntity3 = updateRequest.getEntities().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 : HookNotification(org.apache.atlas.model.notification.HookNotification) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) EntityUpdateRequestV2(org.apache.atlas.model.notification.HookNotification.EntityUpdateRequestV2) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

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