Skip to content

Commit 3dba9d3

Browse files
ATLAS-5097 : Support for fetching multiple types which are referenced by same relationship attribute name in export operation
1 parent 18d7f9d commit 3dba9d3

2 files changed

Lines changed: 106 additions & 1 deletion

File tree

distro/src/conf/atlas-application.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,8 @@ atlas.search.gremlin.enable=false
281281

282282
######### Skip check for the same attribute name in Parent type and Child type #########
283283

284-
#atlas.skip.check.for.parent.child.attribute.name=true
284+
#atlas.skip.check.for.parent.child.attribute.name=true
285+
286+
#atlas.relationship.types.for.<type name>=
287+
#atlas.relationship.attr.for.<type name>=<relationship attribute name>
288+
#atlas.relationship.edge.label.between.<type name>.<type name>=

repository/src/main/java/org/apache/atlas/repository/impexp/RelationshipAttributesExtractor.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,28 @@
1717
*/
1818
package org.apache.atlas.repository.impexp;
1919

20+
import org.apache.atlas.ApplicationProperties;
21+
import org.apache.atlas.AtlasException;
2022
import org.apache.atlas.model.instance.AtlasEntity;
2123
import org.apache.atlas.model.instance.AtlasRelatedObjectId;
24+
import org.apache.atlas.model.instance.AtlasRelationship;
25+
import org.apache.atlas.model.instance.AtlasStruct;
2226
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
2327
import org.apache.atlas.model.typedef.AtlasEntityDef;
28+
import org.apache.atlas.repository.graph.GraphHelper;
29+
import org.apache.atlas.repository.graphdb.AtlasEdge;
30+
import org.apache.atlas.repository.graphdb.AtlasVertex;
2431
import org.apache.atlas.repository.impexp.ExportService.TraversalDirection;
32+
import org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2;
2533
import org.apache.atlas.type.AtlasTypeRegistry;
2634
import org.apache.atlas.type.AtlasTypeUtil;
35+
import org.apache.commons.collections.CollectionUtils;
2736
import org.slf4j.Logger;
2837
import org.slf4j.LoggerFactory;
2938

3039
import java.util.ArrayList;
3140
import java.util.Collection;
41+
import java.util.Iterator;
3242
import java.util.List;
3343

3444
import static org.apache.atlas.repository.impexp.ExportService.ExportContext;
@@ -40,6 +50,10 @@
4050
public class RelationshipAttributesExtractor implements ExtractStrategy {
4151
private static final Logger LOG = LoggerFactory.getLogger(RelationshipAttributesExtractor.class);
4252

53+
private static final String DISPLAY_NAME_PROPERTY = "Asset.__s_name";
54+
private static final String RELATIONSHIP_GUID_PROPERTY = "_r__guid";
55+
private static final String QUALIFIED_NAME_PROPERTY = "Referenceable.qualifiedName";
56+
4357
private final AtlasTypeRegistry typeRegistry;
4458

4559
public RelationshipAttributesExtractor(AtlasTypeRegistry typeRegistry) {
@@ -148,6 +162,93 @@ private List<AtlasRelatedObjectId> getRelatedObjectIds(AtlasEntity entity) {
148162
}
149163
}
150164

165+
String typeName = entity.getTypeName();
166+
List<String> relatedTypes = checkForRelationshipTypes(typeName);
167+
if (CollectionUtils.isNotEmpty(relatedTypes)) {
168+
String alreadyPopulatedType = checkAlreadyPopulatedRelationshipType(entity);
169+
relatedTypes.remove(alreadyPopulatedType);
170+
}
171+
String entityGuid = entity.getGuid();
172+
List<AtlasRelatedObjectId> relatedObjectIdList = populateRelatedObjectIds(typeName, entityGuid, relatedTypes);
173+
relatedObjectIds.addAll(relatedObjectIdList);
174+
return relatedObjectIds;
175+
}
176+
177+
private String checkAlreadyPopulatedRelationshipType(AtlasEntity entity) {
178+
String typeName = entity.getTypeName();
179+
String relationshipAttr;
180+
try {
181+
relationshipAttr = ApplicationProperties.get().getString("atlas.relationship.attr.for." + typeName);
182+
} catch (AtlasException e) {
183+
throw new RuntimeException(e);
184+
}
185+
Object o = entity.getRelationshipAttribute(relationshipAttr);
186+
String relatedTypeName = null;
187+
if (o instanceof AtlasRelatedObjectId) {
188+
AtlasRelatedObjectId relatedObjectId = (AtlasRelatedObjectId) o;
189+
relatedTypeName = relatedObjectId.getTypeName();
190+
} else if (o instanceof Collection) {
191+
List<AtlasRelatedObjectId> relatedObjectIds = (List<AtlasRelatedObjectId>) o;
192+
if (!CollectionUtils.isEmpty(relatedObjectIds)) {
193+
relatedTypeName = relatedObjectIds.get(0).getTypeName();
194+
}
195+
}
196+
return relatedTypeName;
197+
}
198+
199+
private List<String> checkForRelationshipTypes(String typeName) {
200+
List<String> relatedTypeList = null;
201+
try {
202+
String[] relatedTypes = ApplicationProperties.get().getStringArray("atlas.relationship.types.for." + typeName);
203+
if (relatedTypes != null && relatedTypes.length > 0) {
204+
relatedTypeList = new ArrayList<>();
205+
for (String relatedType : relatedTypes) {
206+
relatedTypeList.add(relatedType);
207+
}
208+
}
209+
} catch (AtlasException e) {
210+
throw new RuntimeException(e);
211+
}
212+
return relatedTypeList;
213+
}
214+
215+
private List<AtlasRelatedObjectId> populateRelatedObjectIds(String typeName, String entityGuid, List<String> relatedTypes) {
216+
List<AtlasRelatedObjectId> relatedObjectIds = new ArrayList<>();
217+
AtlasVertex vertex = AtlasGraphUtilsV2.findByGuid(entityGuid);
218+
if (CollectionUtils.isEmpty(relatedTypes)) {
219+
return relatedObjectIds;
220+
}
221+
for (String relatedType : relatedTypes) {
222+
String edgeLabel;
223+
try {
224+
edgeLabel = ApplicationProperties.get().getString("atlas.relationship.edge.label.between." + typeName + "." + relatedType);
225+
} catch (AtlasException e) {
226+
throw new RuntimeException(e);
227+
}
228+
Iterator<AtlasEdge> edges = GraphHelper.getIncomingEdgesByLabel(vertex, edgeLabel);
229+
while (edges.hasNext()) {
230+
AtlasEdge relationshipEdge = edges.next();
231+
AtlasVertex relationshipVertex = relationshipEdge.getOutVertex();
232+
AtlasRelatedObjectId relatedObjectId = new AtlasRelatedObjectId();
233+
AtlasEntity.Status vertexStatus = GraphHelper.getStatus(relationshipVertex);
234+
relatedObjectId.setEntityStatus(vertexStatus);
235+
String displayText = relationshipVertex.getProperty(DISPLAY_NAME_PROPERTY, String.class);
236+
relatedObjectId.setDisplayText(displayText);
237+
relatedObjectId.setRelationshipType(GraphHelper.getTypeName(relationshipEdge));
238+
String relationshipGuid = relationshipEdge.getProperty(RELATIONSHIP_GUID_PROPERTY, String.class);
239+
relatedObjectId.setRelationshipGuid(relationshipGuid);
240+
AtlasRelationship.Status edgeStatus = GraphHelper.getEdgeStatus(relationshipEdge);
241+
relatedObjectId.setRelationshipStatus(edgeStatus);
242+
AtlasStruct relationshipAttributes = new AtlasStruct();
243+
relationshipAttributes.setTypeName(GraphHelper.getTypeName(relationshipEdge));
244+
relatedObjectId.setRelationshipAttributes(relationshipAttributes);
245+
String qualifiedName = relationshipVertex.getProperty(QUALIFIED_NAME_PROPERTY, String.class);
246+
relatedObjectId.setQualifiedName(qualifiedName);
247+
relatedObjectId.setGuid(GraphHelper.getGuid(relationshipVertex));
248+
relatedObjectId.setTypeName(GraphHelper.getTypeName(relationshipVertex));
249+
relatedObjectIds.add(relatedObjectId);
250+
}
251+
}
151252
return relatedObjectIds;
152253
}
153254
}

0 commit comments

Comments
 (0)