forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatasetIndexBuilder.java
149 lines (135 loc) · 5.84 KB
/
DatasetIndexBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.linkedin.metadata.builders.search;
import com.linkedin.common.DatasetUrnArray;
import com.linkedin.common.GlobalTags;
import com.linkedin.common.Ownership;
import com.linkedin.common.Status;
import com.linkedin.common.urn.DatasetUrn;
import com.linkedin.data.template.RecordTemplate;
import com.linkedin.data.template.StringArray;
import com.linkedin.dataset.DatasetDeprecation;
import com.linkedin.dataset.DatasetProperties;
import com.linkedin.dataset.UpstreamLineage;
import com.linkedin.metadata.search.DatasetDocument;
import com.linkedin.metadata.snapshot.DatasetSnapshot;
import com.linkedin.schema.SchemaField;
import com.linkedin.schema.SchemaMetadata;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class DatasetIndexBuilder extends BaseIndexBuilder<DatasetDocument> {
public DatasetIndexBuilder() {
super(Collections.singletonList(DatasetSnapshot.class), DatasetDocument.class);
}
@Nonnull
private static String buildBrowsePath(@Nonnull DatasetUrn urn) {
return ("/" + urn.getOriginEntity() + "/" + urn.getPlatformEntity().getPlatformNameEntity() + "/" + urn.getDatasetNameEntity())
.replace('.', '/').toLowerCase();
}
/**
* Given dataset urn, this returns a {@link DatasetDocument} model that has urn, dataset name, platform and origin fields set
*
* @param urn {@link DatasetUrn} that needs to be set
* @return {@link DatasetDocument} model with relevant fields set that are extracted from the urn
*/
@Nonnull
private static DatasetDocument setUrnDerivedFields(@Nonnull DatasetUrn urn) {
return new DatasetDocument()
.setName(urn.getDatasetNameEntity())
.setOrigin(urn.getOriginEntity())
.setPlatform(urn.getPlatformEntity().getPlatformNameEntity())
.setUrn(urn)
.setBrowsePaths(new StringArray(Collections.singletonList(buildBrowsePath(urn))));
}
@Nonnull
private DatasetDocument getDocumentToUpdateFromAspect(@Nonnull DatasetUrn urn, @Nonnull Ownership ownership) {
final StringArray owners = BuilderUtils.getCorpUserOwners(ownership);
return new DatasetDocument()
.setUrn(urn)
.setHasOwners(!owners.isEmpty())
.setOwners(owners);
}
@Nonnull
private DatasetDocument getDocumentToUpdateFromAspect(@Nonnull DatasetUrn urn, @Nonnull Status status) {
return new DatasetDocument()
.setUrn(urn)
.setRemoved(status.isRemoved());
}
@Nonnull
private DatasetDocument getDocumentToUpdateFromAspect(@Nonnull DatasetUrn urn, @Nonnull DatasetDeprecation deprecation) {
return new DatasetDocument().setUrn(urn).setDeprecated(deprecation.isDeprecated());
}
@Nonnull
private DatasetDocument getDocumentToUpdateFromAspect(@Nonnull DatasetUrn urn, @Nonnull DatasetProperties datasetProperties) {
final DatasetDocument doc = new DatasetDocument().setUrn(urn);
if (datasetProperties.getDescription() != null) {
doc.setDescription(datasetProperties.getDescription());
}
return doc;
}
@Nonnull
private DatasetDocument getDocumentToUpdateFromAspect(@Nonnull DatasetUrn urn,
@Nonnull SchemaMetadata schemaMetadata) {
final StringArray fieldPaths = new StringArray(
schemaMetadata.getFields().stream().map(SchemaField::getFieldPath).collect(Collectors.toList()));
return new DatasetDocument().setUrn(urn).setHasSchema(true).setFieldPaths(fieldPaths);
}
@Nonnull
private DatasetDocument getDocumentToUpdateFromAspect(@Nonnull DatasetUrn urn,
@Nonnull UpstreamLineage upstreamLineage) {
return new DatasetDocument().setUrn(urn)
.setUpstreams(new DatasetUrnArray(upstreamLineage.getUpstreams()
.stream()
.map(upstream -> upstream.getDataset())
.collect(Collectors.toList())));
}
@Nonnull
private DatasetDocument getDocumentToUpdateFromAspect(@Nonnull DatasetUrn urn,
@Nonnull GlobalTags globalTags) {
return new DatasetDocument().setUrn(urn)
.setTags(new StringArray(globalTags.getTags()
.stream()
.map(tag -> tag.getTag().getName())
.collect(Collectors.toList())));
}
@Nonnull
private List<DatasetDocument> getDocumentsToUpdateFromSnapshotType(@Nonnull DatasetSnapshot datasetSnapshot) {
final DatasetUrn urn = datasetSnapshot.getUrn();
final List<DatasetDocument> documents = datasetSnapshot.getAspects().stream().map(aspect -> {
if (aspect.isDatasetDeprecation()) {
return getDocumentToUpdateFromAspect(urn, aspect.getDatasetDeprecation());
} else if (aspect.isDatasetProperties()) {
return getDocumentToUpdateFromAspect(urn, aspect.getDatasetProperties());
} else if (aspect.isOwnership()) {
return getDocumentToUpdateFromAspect(urn, aspect.getOwnership());
} else if (aspect.isSchemaMetadata()) {
return getDocumentToUpdateFromAspect(urn, aspect.getSchemaMetadata());
} else if (aspect.isStatus()) {
return getDocumentToUpdateFromAspect(urn, aspect.getStatus());
} else if (aspect.isUpstreamLineage()) {
return getDocumentToUpdateFromAspect(urn, aspect.getUpstreamLineage());
} else if (aspect.isGlobalTags()) {
return getDocumentToUpdateFromAspect(urn, aspect.getGlobalTags());
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
documents.add(setUrnDerivedFields(urn));
return documents;
}
@Override
@Nonnull
public final List<DatasetDocument> getDocumentsToUpdate(@Nonnull RecordTemplate genericSnapshot) {
if (genericSnapshot instanceof DatasetSnapshot) {
return getDocumentsToUpdateFromSnapshotType((DatasetSnapshot) genericSnapshot);
}
return Collections.emptyList();
}
@Override
@Nonnull
public Class<DatasetDocument> getDocumentType() {
return DatasetDocument.class;
}
}