private static String getKey(AtlasRelationshipEndDef endDef) { return getKey(endDef.getIsLegacyAttribute(), endDef.getType(), endDef.getName()); }
private void createRelationshipTypes() throws AtlasBaseException { AtlasRelationshipDef deptEmployeeRelationDef = new AtlasRelationshipDef(DEPT_EMPLOYEE_RELATION_TYPE, getDescription(DEPT_EMPLOYEE_RELATION_TYPE), "1.0", RelationshipCategory.ASSOCIATION, PropagateTags.ONE_TO_TWO, new AtlasRelationshipEndDef(EMPLOYEE_TYPE, "department", Cardinality.SINGLE), new AtlasRelationshipEndDef(DEPARTMENT_TYPE, "employees", Cardinality.SET)); AtlasRelationshipDef employeeAddrRelationDef = new AtlasRelationshipDef(EMPLOYEE_ADDRESS_RELATION_TYPE, getDescription(EMPLOYEE_ADDRESS_RELATION_TYPE), "1.0", RelationshipCategory.ASSOCIATION, PropagateTags.ONE_TO_TWO, new AtlasRelationshipEndDef(EMPLOYEE_TYPE, "address", Cardinality.SINGLE), new AtlasRelationshipEndDef(ADDRESS_TYPE, "employees", Cardinality.SET)); createTypes(new ArrayList<>(Arrays.asList(deptEmployeeRelationDef, employeeAddrRelationDef))); }
/** * Construct using an existing AtlasRelationshipEndDef * @param other */ public AtlasRelationshipEndDef(AtlasRelationshipEndDef other) { if (other != null) { setType(other.getType()); setName(other.getName()); setIsContainer(other.getIsContainer()); setCardinality(other.getCardinality()); setIsLegacyAttribute(other.isLegacyAttribute); setDescription(other.description); } }
public AtlasRelationshipEndDef(String typeName, String name, Cardinality cardinality, boolean isContainer, boolean isLegacyAttribute) { setType(typeName); setName(name); setCardinality(cardinality); setIsContainer(isContainer); setIsLegacyAttribute(isLegacyAttribute); }
private String getRelationshipEdgeLabel(AtlasVertex fromVertex, AtlasVertex toVertex, String relationshipTypeName) { if (LOG.isDebugEnabled()) { LOG.debug("getRelationshipEdgeLabel({})", relationshipTypeName); } AtlasRelationshipType relationshipType = typeRegistry.getRelationshipTypeByName(relationshipTypeName); String ret = relationshipType.getRelationshipDef().getRelationshipLabel(); AtlasRelationshipEndDef endDef1 = relationshipType.getRelationshipDef().getEndDef1(); AtlasRelationshipEndDef endDef2 = relationshipType.getRelationshipDef().getEndDef2(); Set<String> fromVertexTypes = getTypeAndAllSuperTypes(AtlasGraphUtilsV2.getTypeName(fromVertex)); Set<String> toVertexTypes = getTypeAndAllSuperTypes(AtlasGraphUtilsV2.getTypeName(toVertex)); AtlasAttribute attribute = null; // validate entity type and all its supertypes contains relationshipDefs end type // e.g. [hive_process -> hive_table] -> [Process -> DataSet] if (fromVertexTypes.contains(endDef1.getType()) && toVertexTypes.contains(endDef2.getType())) { String attributeName = endDef1.getName(); attribute = relationshipType.getEnd1Type().getRelationshipAttribute(attributeName); } else if (fromVertexTypes.contains(endDef2.getType()) && toVertexTypes.contains(endDef1.getType())) { String attributeName = endDef2.getName(); attribute = relationshipType.getEnd2Type().getRelationshipAttribute(attributeName); } if (attribute != null) { ret = attribute.getRelationshipEdgeLabel(); } return ret; }
private Object mapVertexToRelationshipAttribute(AtlasVertex entityVertex, AtlasEntityType entityType, AtlasAttribute attribute) throws AtlasBaseException { Object ret = null; AtlasRelationshipDef relationshipDef = graphHelper.getRelationshipDef(entityVertex, entityType, attribute.getName()); if (relationshipDef == null) { throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_INVALID, "relationshipDef is null"); } AtlasRelationshipEndDef endDef1 = relationshipDef.getEndDef1(); AtlasRelationshipEndDef endDef2 = relationshipDef.getEndDef2(); AtlasEntityType endDef1Type = typeRegistry.getEntityTypeByName(endDef1.getType()); AtlasEntityType endDef2Type = typeRegistry.getEntityTypeByName(endDef2.getType()); AtlasRelationshipEndDef attributeEndDef = null; if (endDef1Type.isTypeOrSuperTypeOf(entityType.getTypeName()) && StringUtils.equals(endDef1.getName(), attribute.getName())) { attributeEndDef = endDef1; } else if (endDef2Type.isTypeOrSuperTypeOf(entityType.getTypeName()) && StringUtils.equals(endDef2.getName(), attribute.getName())) { attributeEndDef = endDef2; } if (attributeEndDef == null) { throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_INVALID, relationshipDef.toString()); } switch (attributeEndDef.getCardinality()) { case SINGLE: ret = mapRelatedVertexToObjectId(entityVertex, attribute); break; case LIST: case SET: ret = mapRelationshipArrayAttribute(entityVertex, attribute); break; } return ret; }
private void addRelationshipEdgeDirection() { AtlasRelationshipEndDef endDef1 = relationshipDef.getEndDef1(); AtlasRelationshipEndDef endDef2 = relationshipDef.getEndDef2(); AtlasAttribute end1Attribute = end1Type.getRelationshipAttribute(endDef1.getName()); AtlasAttribute end2Attribute = end2Type.getRelationshipAttribute(endDef2.getName()); //default relationship edge direction is end1 (out) -> end2 (in) AtlasRelationshipEdgeDirection end1Direction = OUT; AtlasRelationshipEdgeDirection end2Direction = IN; if (endDef1.getIsLegacyAttribute() && endDef2.getIsLegacyAttribute()) { end2Direction = OUT; } else if (!endDef1.getIsLegacyAttribute() && endDef2.getIsLegacyAttribute()) { end1Direction = IN; end2Direction = OUT; } end1Attribute.setRelationshipEdgeDirection(end1Direction); end2Attribute.setRelationshipEdgeDirection(end2Direction); }
@Override public int hashCode() { return Objects.hash(type, getName(), description, isContainer, cardinality, isLegacyAttribute); }
private void addRelationshipAttributeToEndType(AtlasRelationshipEndDef endDef, AtlasEntityType entityType, String attrTypeName, AtlasTypeRegistry typeRegistry, String relationshipLabel) throws AtlasBaseException { String attrName = (endDef != null) ? endDef.getName() : null; if (StringUtils.isEmpty(attrName)) { return; } AtlasAttribute attribute = entityType.getAttribute(attrName); // if relationshipLabel is null, then legacyLabel is mentioned at both ends, // use the respective end's legacyLabel as relationshipLabel if (relationshipLabel == null) { relationshipLabel = getLegacyEdgeLabel(entityType, attrName); } if (attribute == null) { //attr doesn't exist in type - is a new relationship attribute if (endDef.getCardinality() == Cardinality.SET) { attrTypeName = AtlasBaseTypeDef.getArrayTypeName(attrTypeName); } attribute = new AtlasAttribute(entityType, new AtlasAttributeDef(attrName, attrTypeName), typeRegistry.getType(attrTypeName), relationshipLabel); } else { // attribute already exists (legacy attribute which is also a relationship attribute) // add relationshipLabel information to existing attribute attribute.setRelationshipEdgeLabel(relationshipLabel); } entityType.addRelationshipAttribute(attrName, attribute); entityType.addRelationshipAttributeType(attrName, this); }
private PropagateTags getRelationshipTagPropagation(AtlasVertex fromVertex, AtlasVertex toVertex, AtlasRelationship relationship) { AtlasRelationshipType relationshipType = typeRegistry.getRelationshipTypeByName(relationship.getTypeName()); AtlasRelationshipEndDef endDef1 = relationshipType.getRelationshipDef().getEndDef1(); AtlasRelationshipEndDef endDef2 = relationshipType.getRelationshipDef().getEndDef2(); Set<String> fromVertexTypes = getTypeAndAllSuperTypes(getTypeName(fromVertex)); Set<String> toVertexTypes = getTypeAndAllSuperTypes(getTypeName(toVertex)); PropagateTags ret = relationshipType.getRelationshipDef().getPropagateTags(); // relationshipDef is defined as end1 (hive_db) and end2 (hive_table) and tagPropagation = ONE_TO_TWO // relationship edge exists from [hive_table --> hive_db] // swap the tagPropagation property for such cases. if (fromVertexTypes.contains(endDef2.getType()) && toVertexTypes.contains(endDef1.getType())) { if (ret == ONE_TO_TWO) { ret = TWO_TO_ONE; } else if (ret == TWO_TO_ONE) { ret = ONE_TO_TWO; } } return ret; }
public AtlasRelationshipType(AtlasRelationshipDef relationshipDef) { super(relationshipDef); AtlasRelationshipEndDef end1Def = relationshipDef != null ? relationshipDef.getEndDef1() : null; AtlasRelationshipEndDef end2Def = relationshipDef != null ? relationshipDef.getEndDef2() : null; this.relationshipDef = relationshipDef; this.hasLegacyAttributeEnd = (end1Def != null && end1Def.getIsLegacyAttribute()) || (end2Def != null && end2Def.getIsLegacyAttribute()); }
AtlasRelationshipEndDef newEnd1 = newRelationshipDef.getEndDef1(); if ( !newEnd1.equals(existingEnd1) ) { throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_INVALID_END1_UPDATE, newRelationshipDef.getName(), newEnd1.toString(), existingEnd1.toString()); AtlasRelationshipEndDef newEnd2 = newRelationshipDef.getEndDef2(); if ( !newEnd2.equals(existingEnd2) ) { throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_INVALID_END2_UPDATE, newRelationshipDef.getName(), newEnd2.toString(), existingEnd2.toString());
RelationshipCategory relationshipCategory = relationshipDef.getRelationshipCategory(); String name = relationshipDef.getName(); boolean isContainer1 = endDef1.getIsContainer(); boolean isContainer2 = endDef2.getIsContainer(); if ((endDef1.getCardinality() == AtlasAttributeDef.Cardinality.LIST) || (endDef2.getCardinality() == AtlasAttributeDef.Cardinality.LIST)) { throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_LIST_ON_END, name); if (endDef1.getCardinality() == AtlasAttributeDef.Cardinality.SET && !endDef1.getIsContainer()) { throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_COMPOSITION_MULTIPLE_PARENTS, name); if ((endDef2.getCardinality() == AtlasAttributeDef.Cardinality.SET) && !endDef2.getIsContainer()) { throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_COMPOSITION_MULTIPLE_PARENTS, name);
@Override public String toString() { return toString(new StringBuilder()).toString(); } }
private String getRelationshipEdgeLabel(AtlasVertex fromVertex, AtlasVertex toVertex, String relationshipTypeName) { if (LOG.isDebugEnabled()) { LOG.debug("getRelationshipEdgeLabel({})", relationshipTypeName); } AtlasRelationshipType relationshipType = typeRegistry.getRelationshipTypeByName(relationshipTypeName); String ret = relationshipType.getRelationshipDef().getRelationshipLabel(); AtlasRelationshipEndDef endDef1 = relationshipType.getRelationshipDef().getEndDef1(); AtlasRelationshipEndDef endDef2 = relationshipType.getRelationshipDef().getEndDef2(); Set<String> fromVertexTypes = getTypeAndAllSuperTypes(AtlasGraphUtilsV2.getTypeName(fromVertex)); Set<String> toVertexTypes = getTypeAndAllSuperTypes(AtlasGraphUtilsV2.getTypeName(toVertex)); AtlasAttribute attribute = null; // validate entity type and all its supertypes contains relationshipDefs end type // e.g. [hive_process -> hive_table] -> [Process -> DataSet] if (fromVertexTypes.contains(endDef1.getType()) && toVertexTypes.contains(endDef2.getType())) { String attributeName = endDef1.getName(); attribute = relationshipType.getEnd1Type().getRelationshipAttribute(attributeName); } else if (fromVertexTypes.contains(endDef2.getType()) && toVertexTypes.contains(endDef1.getType())) { String attributeName = endDef2.getName(); attribute = relationshipType.getEnd2Type().getRelationshipAttribute(attributeName); } if (attribute != null) { ret = attribute.getRelationshipEdgeLabel(); } return ret; }
private Object mapVertexToRelationshipAttribute(AtlasVertex entityVertex, AtlasEntityType entityType, AtlasAttribute attribute) throws AtlasBaseException { Object ret = null; AtlasRelationshipDef relationshipDef = graphHelper.getRelationshipDef(entityVertex, entityType, attribute.getName()); if (relationshipDef == null) { throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_INVALID, "relationshipDef is null"); } AtlasRelationshipEndDef endDef1 = relationshipDef.getEndDef1(); AtlasRelationshipEndDef endDef2 = relationshipDef.getEndDef2(); AtlasEntityType endDef1Type = typeRegistry.getEntityTypeByName(endDef1.getType()); AtlasEntityType endDef2Type = typeRegistry.getEntityTypeByName(endDef2.getType()); AtlasRelationshipEndDef attributeEndDef = null; if (endDef1Type.isTypeOrSuperTypeOf(entityType.getTypeName()) && StringUtils.equals(endDef1.getName(), attribute.getName())) { attributeEndDef = endDef1; } else if (endDef2Type.isTypeOrSuperTypeOf(entityType.getTypeName()) && StringUtils.equals(endDef2.getName(), attribute.getName())) { attributeEndDef = endDef2; } if (attributeEndDef == null) { throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_INVALID, relationshipDef.toString()); } switch (attributeEndDef.getCardinality()) { case SINGLE: ret = mapRelatedVertexToObjectId(entityVertex, attribute); break; case LIST: case SET: ret = mapRelationshipArrayAttribute(entityVertex, attribute); break; } return ret; }
@Override public void resolveReferencesPhase2(AtlasTypeRegistry typeRegistry) throws AtlasBaseException { super.resolveReferencesPhase2(typeRegistry); AtlasRelationshipEndDef endDef1 = relationshipDef.getEndDef1(); AtlasRelationshipEndDef endDef2 = relationshipDef.getEndDef2(); String relationshipLabel = null; // if legacyLabel is not specified at both ends, use relationshipDef name as relationship label. // if legacyLabel is specified in any one end, use it as the relationship label for both ends (legacy case). // if legacyLabel is specified at both ends use the respective end's legacyLabel as relationship label (legacy case). if (!endDef1.getIsLegacyAttribute() && !endDef2.getIsLegacyAttribute()) { relationshipLabel = relationshipDef.getRelationshipLabel(); } else if (endDef1.getIsLegacyAttribute() && !endDef2.getIsLegacyAttribute()) { relationshipLabel = getLegacyEdgeLabel(end1Type, endDef1.getName()); } else if (!endDef1.getIsLegacyAttribute() && endDef2.getIsLegacyAttribute()) { relationshipLabel = getLegacyEdgeLabel(end2Type, endDef2.getName()); } addRelationshipAttributeToEndType(endDef1, end1Type, end2Type.getTypeName(), typeRegistry, relationshipLabel); addRelationshipAttributeToEndType(endDef2, end2Type, end1Type.getTypeName(), typeRegistry, relationshipLabel); // add relationship edge direction information addRelationshipEdgeDirection(); }
/** * * @param typeName * - The name of an entityDef type * @param name * - The name of the new attribute that the entity instance will pick up. * @param cardinality * - whether the end is SINGLE (1) or SET (many) * @param isContainer * - whether the end is a container or not * @param isLegacyAttribute * - whether this is a legacy attribute * @param description * - The description of this end of the relationship. */ public AtlasRelationshipEndDef(String typeName, String name, Cardinality cardinality, boolean isContainer, boolean isLegacyAttribute, String description) { setType(typeName); setName(name); setCardinality(cardinality); setIsContainer(isContainer); setIsLegacyAttribute(isLegacyAttribute); setDescription(description); }
@Override public int hashCode() { return Objects.hash(type, getName(), isContainer, cardinality, isLegacyAttribute); }
private void addRelationshipAttributeToEndType(AtlasRelationshipEndDef endDef, AtlasEntityType entityType, String attrTypeName, AtlasTypeRegistry typeRegistry, String relationshipLabel) throws AtlasBaseException { String attrName = (endDef != null) ? endDef.getName() : null; if (StringUtils.isEmpty(attrName)) { return; } AtlasAttribute attribute = entityType.getAttribute(attrName); // if relationshipLabel is null, then legacyLabel is mentioned at both ends, // use the respective end's legacyLabel as relationshipLabel if (relationshipLabel == null) { relationshipLabel = getLegacyEdgeLabel(entityType, attrName); } if (attribute == null) { //attr doesn't exist in type - is a new relationship attribute if (endDef.getCardinality() == Cardinality.SET) { attrTypeName = AtlasBaseTypeDef.getArrayTypeName(attrTypeName); } attribute = new AtlasAttribute(entityType, new AtlasAttributeDef(attrName, attrTypeName), typeRegistry.getType(attrTypeName), relationshipLabel); } else { // attribute already exists (legacy attribute which is also a relationship attribute) // add relationshipLabel information to existing attribute attribute.setRelationshipEdgeLabel(relationshipLabel); } entityType.addRelationshipAttribute(attrName, attribute); entityType.addRelationshipAttributeType(attrName, this); }