Search in sources :

Example 21 with HookNotification

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

the class AlterDatabase method getNotificationMessages.

@Override
public List<HookNotification> getNotificationMessages() throws Exception {
    List<HookNotification> ret = null;
    AtlasEntitiesWithExtInfo entities = getEntities();
    if (entities != null && CollectionUtils.isNotEmpty(entities.getEntities())) {
        ret = Collections.singletonList(new EntityUpdateRequestV2(getUserName(), entities));
    }
    return ret;
}
Also used : HookNotification(org.apache.atlas.model.notification.HookNotification) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) EntityUpdateRequestV2(org.apache.atlas.model.notification.HookNotification.EntityUpdateRequestV2)

Example 22 with HookNotification

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

the class AlterTable method getNotificationMessages.

@Override
public List<HookNotification> getNotificationMessages() throws Exception {
    List<HookNotification> ret = null;
    AtlasEntitiesWithExtInfo entities = getEntities();
    if (entities != null && CollectionUtils.isNotEmpty(entities.getEntities())) {
        ret = Collections.singletonList(new EntityUpdateRequestV2(getUserName(), entities));
    }
    return ret;
}
Also used : HookNotification(org.apache.atlas.model.notification.HookNotification) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) EntityUpdateRequestV2(org.apache.atlas.model.notification.HookNotification.EntityUpdateRequestV2)

Example 23 with HookNotification

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

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

Example 25 with HookNotification

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

the class KafkaConsumerTest method testNextVersionMismatch.

@Test
public void testNextVersionMismatch() throws Exception {
    Referenceable entity = getEntity(TRAIT_NAME);
    EntityUpdateRequest message = new EntityUpdateRequest("user1", entity);
    String json = AtlasType.toV1Json(new AtlasNotificationMessage<>(new MessageVersion("2.0.0"), message));
    TopicPartition tp = new TopicPartition("ATLAS_HOOK", 0);
    List<ConsumerRecord<String, String>> klist = Collections.singletonList(new ConsumerRecord<>("ATLAS_HOOK", 0, 0L, "mykey", json));
    Map mp = Collections.singletonMap(tp, klist);
    ConsumerRecords records = new ConsumerRecords(mp);
    kafkaConsumer.assign(Collections.singletonList(tp));
    when(kafkaConsumer.poll(100L)).thenReturn(records);
    AtlasKafkaConsumer consumer = new AtlasKafkaConsumer(NotificationType.HOOK, kafkaConsumer, false, 100L);
    try {
        List<AtlasKafkaMessage<HookNotification>> messageList = consumer.receive();
        assertTrue(messageList.size() > 0);
        HookNotification consumedMessage = messageList.get(0).getMessage();
        fail("Expected VersionMismatchException!");
    } catch (IncompatibleVersionException e) {
        e.printStackTrace();
    }
}
Also used : MessageVersion(org.apache.atlas.model.notification.MessageVersion) IncompatibleVersionException(org.apache.atlas.notification.IncompatibleVersionException) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) EntityUpdateRequest(org.apache.atlas.v1.model.notification.HookNotificationV1.EntityUpdateRequest) HookNotification(org.apache.atlas.model.notification.HookNotification) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) TopicPartition(org.apache.kafka.common.TopicPartition) Map(java.util.Map) Test(org.testng.annotations.Test) EntityNotificationTest(org.apache.atlas.notification.entity.EntityNotificationTest)

Aggregations

HookNotification (org.apache.atlas.model.notification.HookNotification)31 Test (org.testng.annotations.Test)18 ArrayList (java.util.ArrayList)13 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)9 Referenceable (org.apache.atlas.v1.model.instance.Referenceable)9 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)8 EntityCreateRequest (org.apache.atlas.v1.model.notification.HookNotificationV1.EntityCreateRequest)8 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)6 EntityNotificationTest (org.apache.atlas.notification.entity.EntityNotificationTest)6 EntityUpdateRequest (org.apache.atlas.v1.model.notification.HookNotificationV1.EntityUpdateRequest)6 Map (java.util.Map)5 EntityCreateRequestV2 (org.apache.atlas.model.notification.HookNotification.EntityCreateRequestV2)5 NotificationException (org.apache.atlas.notification.NotificationException)5 EntityUpdateRequestV2 (org.apache.atlas.model.notification.HookNotification.EntityUpdateRequestV2)4 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)3 EntityDeleteRequestV2 (org.apache.atlas.model.notification.HookNotification.EntityDeleteRequestV2)3 EntityPartialUpdateRequestV2 (org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2)3 MessageVersion (org.apache.atlas.model.notification.MessageVersion)2 Table (org.apache.hadoop.hive.ql.metadata.Table)2 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)2