datahub/docs/what/relationship.md

107 lines
5.5 KiB
Markdown
Raw Normal View History

# What is a relationship?
2019-12-18 18:57:18 -08:00
2020-01-15 23:22:28 -08:00
A relationship is a named associate between exactly two [entities](entity.md), a source and a destination.
2019-12-18 18:57:18 -08:00
![metadata-modeling](../imgs/metadata-modeling.png)
From the above graph, a `Group` entity can be linked to a `User` entity via a `HasMember` relationship.
2020-01-15 23:22:28 -08:00
Note that the name of the relationship reflects the direction, i.e. pointing from `Group` to `User`.
2019-12-18 18:57:18 -08:00
This is due to the fact that the actual metadata aspect holding this information is associated with `Group`, rather than User.
2019-12-18 20:28:47 -08:00
Had the direction been reversed, the relationship would have been named `IsMemberOf` instead.
2019-12-18 18:57:18 -08:00
See [Direction of Relationships](#direction-of-relationships) for more discussions on relationship directionality.
A specific instance of a relationship, e.g. `urn:li:corpGroup:group1` has a member `urn:li:corpuser:user1`,
2019-12-18 18:57:18 -08:00
corresponds to an edge in the metadata graph.
2020-01-15 23:22:28 -08:00
Similar to an entity, a relationship can also be associated with optional attributes that are derived from the metadata.
For example, from the `Membership` metadata aspect shown below, were able to derive the `HasMember` relationship that links a specific `Group` to a specific `User`. We can also include additional attribute to the relationship, e.g. importance, which corresponds to the position of the specific member in the original membership array. This allows complex graph query that travel only relationships that match certain criteria, e.g. "returns only the top-5 most important members of this group."
Similar to the entity attributes, relationship attributes should only be added based on the expected query patterns to reduce the indexing cost.
2019-12-18 18:57:18 -08:00
```
namespace: com.linkedin.group
import com.linkedin.common.AuditStamp
import com.linkedin.common.CorpuserUrn
/**
* The membership metadata for a group
*/
record Membership {
/** Audit stamp for the last change */
modified: AuditStamp
/** Admin of the group */
admin: CorpuserUrn
/** Members of the group, ordered in descending importance */
members: array[CorpuserUrn]
2019-12-18 18:57:18 -08:00
}
```
2020-01-15 23:22:28 -08:00
Relationships are meant to be "entity-neutral". In other words, one would expect to use the same `OwnedBy` relationship to link a `Dataset` to a `User` and to link a `Dashboard` to a `User`. As Pegasus doesnt allow typing a field using multiple URNs (because theyre all essentially strings), we resort to using generic URN type for the source and destination.
We also introduce a `@pairings` [annotation](https://linkedin.github.io/rest.li/pdl_migration#shorthand-for-custom-properties) to limit the allowed source and destination URN types.
2019-12-18 18:57:18 -08:00
2020-01-15 23:22:28 -08:00
While its possible to model relationships in rest.li as [association resources](https://linkedin.github.io/rest.li/modeling/modeling#association), which often get stored as mapping tables, it is far more common to model them as "foreign keys" field in a metadata aspect. For instance, the `Ownership` aspect is likely to contain an array of owners corpuser URNs.
2019-12-18 18:57:18 -08:00
Below is an example of how a relationship is modeled in PDL. Note that:
2019-12-18 18:57:18 -08:00
1. As the `source` and `destination` are of generic URN type, were able to factor them out to a common `BaseRelationship` model.
2. Each model is expected to have a `@pairings` annotation that is an array of all allowed source-destination URN pairs.
2020-01-15 23:22:28 -08:00
3. Unlike entity attributes, theres no requirement on making all relationship attributes optional since relationships do not support partial updates.
2019-12-18 18:57:18 -08:00
```
namespace com.linkedin.metadata.relationship
import com.linkedin.common.Urn
/**
* Common fields that apply to all relationships
*/
record BaseRelationship {
/**
* Urn for the source of the relationship
*/
source: Urn
/**
* Urn for the destination of the relationship
*/
destination: Urn
2019-12-18 18:57:18 -08:00
}
```
```
namespace com.linkedin.metadata.relationship
/**
* Data model for a has-member relationship
*/
@pairings = [ {
"destination" : "com.linkedin.common.urn.CorpGroupUrn",
"source" : "com.linkedin.common.urn.CorpUserUrn"
} ]
record HasMembership includes BaseRelationship
2019-12-18 18:57:18 -08:00
{
/**
* The importance of the membership
*/
importance: int
2019-12-18 18:57:18 -08:00
}
```
## Direction of Relationships
As relationships are modeled as directed edges between nodes, its natural to ask which way should it be pointing,
2020-01-15 23:22:28 -08:00
or should there be edges going both ways? The answer is, "doesnt really matter." Its rather an aesthetic choice than technical one.
2019-12-18 18:57:18 -08:00
2020-01-15 23:22:28 -08:00
For one, the actual direction doesnt really impact the execution of graph queries. Most graph DBs are fully capable of traversing edges in reverse direction efficiently.
2019-12-18 20:28:47 -08:00
2020-01-15 23:22:28 -08:00
That being said, generally theres a more "natural way" to specify the direction of a relationship, which closely relate to how the metadata is stored. For example, the membership information for an LDAP group is generally stored as a list in groups metadata. As a result, its more natural to model a `HasMember` relationship that points from a group to a member, instead of a `IsMemberOf` relationship pointing from member to group.
2019-12-18 18:57:18 -08:00
Since all relationships are explicitly declared, its fairly easy for a user to discover what relationships are available and their directionality by inspecting
2020-01-15 23:22:28 -08:00
the [relationships directory](../../metadata-models/src/main/pegasus/com/linkedin/metadata/relationship). Its also possible to provide a UI for the catalog of entities and relationships for analysts who are interested in building complex graph queries to gain insights into the metadata.
2020-08-18 05:13:01 -07:00
## High Cardinality Relationships
See [this doc](../advanced/high-cardinality.md) for suggestions on how to best model relationships with high cardinality.