我正在使用 Spring-Data 和 SpringBoot 来填充我的 Neo4j 图形数据库。
我定义了以下 Neo4j 实体:
Source实体 -->
@NodeEntity
public class Source implements Comparable<Source> {
@GraphId private Long id;
private String name;
private SourceType type;
private String dataStoreName;
private String dataStoreDesc;
private Source() {
// Empty constructor required as of Neo4j API 2.0.5
};
public Source(String name, SourceType type, String dataStoreName, String dataStoreDesc) {
this.name = name;
this.type = type;
this.dataStoreName = dataStoreName;
this.dataStoreDesc = dataStoreDesc;
}
@Relationship(type = "CONTAINS", direction = Relationship.UNDIRECTED)
public Set<Field> fields;
public void contains(Field field) {
if (fields == null) {
fields = new HashSet<Field>();
}
fields.add(field);
}
/* Getter and Setters */
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SourceType getType() {
return type;
}
public void setType(SourceType type) {
this.type = type;
}
public String getDataStoreName() {
return dataStoreName;
}
public void setDataStoreName(String dataStoreName) {
this.dataStoreName = dataStoreName;
}
public String getDataStoreDesc() {
return dataStoreDesc;
}
public void setDataStoreDesc(String dataStoreDesc) {
this.dataStoreDesc = dataStoreDesc;
}
public Set<Field> getFields() {
return fields;
}
public void setFields(Set<Field> fields) {
this.fields = fields;
}
@Override
public int compareTo(Source other) {
String name = other.getName();
SourceType type = other.getType();
if(this.name.equalsIgnoreCase(name) && this.type.equals(type))
return 0;
return -1;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Source other = (Source) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (type != other.type)
return false;
return true;
}
}
Field实体-->
@NodeEntity
public class Field implements Comparable<Field> {
@GraphId private Long id;
private String name;
private FieldType fieldType;
private SourceType sourceType;
private String logicalName;
private String dataType;
private String dataSize;
private String description;
private Field() {
// Empty constructor required as of Neo4j API 2.0.5
};
public Field(String name, FieldType fieldType, SourceType sourceType, String logicalName, String dataType, String dataSize, String description) {
this.name = name;
this.fieldType = fieldType;
this.sourceType = sourceType;
this.logicalName = logicalName;
this.dataType = dataType;
this.dataSize = dataSize;
this.description = description;
}
@Relationship(type = "MAPS-TO", direction = Relationship.UNDIRECTED)
public Set<Field> fields;
public void mapsTo(Field field) {
if (fields == null) {
fields = new HashSet<Field>();
}
fields.add(field);
}
/* Getter and Setters */
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public FieldType getFieldType() {
return fieldType;
}
public void setFieldType(FieldType fieldType) {
this.fieldType = fieldType;
}
public SourceType getSourceType() {
return sourceType;
}
public void setSourceType(SourceType sourceType) {
this.sourceType = sourceType;
}
public String getLogicalName() {
return logicalName;
}
public void setLogicalName(String logicalName) {
this.logicalName = logicalName;
}
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
public String getDataSize() {
return dataSize;
}
public void setDataSize(String dataSize) {
this.dataSize = dataSize;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<Field> getFields() {
return fields;
}
public void setFields(Set<Field> fields) {
this.fields = fields;
}
@Override
public int compareTo(Field other) {
String name = other.getName();
FieldType fieldType = other.getFieldType();
SourceType sourceType = other.getSourceType();
if(this.name.equalsIgnoreCase(name) && this.fieldType.equals(fieldType) && this.sourceType.equals(sourceType))
return 0;
return -1;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((fieldType == null) ? 0 : fieldType.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((sourceType == null) ? 0 : sourceType.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Field other = (Field) obj;
if (fieldType != other.fieldType)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sourceType != other.sourceType)
return false;
return true;
}
}
所以,一个Source CONTAINS倍数Fields。而aField是MAPS-TO一个或多个其他Fields。
每个Source都是一个SourceType。
我的不同SourceType是:生产者、入境、分期、中间、出境、消费者。
public enum SourceType {
PRODUCER, INBOUND, STAGING, INTERMEDIATE, OUTBOUND, CONSUMER;
}
每个Field都是一个FieldType。
我不同FieldType的是:FILE_FIELD、DB_COLUMN。
public enum FieldType {
FILE_FIELD, DB_COLUMN;
}
我的数据沿袭如下:生产者 --> 入站 --> 分期 --> 中间 --> 出站 --> 消费者
我现在正在寻找一个高级 Cypher 查询,如果我Field在 CONSUMER中提供一个Source,我可以通过它追踪它的沿袭直到PRODUCER Source。
同样,我也在寻找一个查询,如果我Field在 PRODUCER中提供一个查询Source,我可以向前跟踪它的沿袭直到 CONSUMER Source。
我尝试使用shortestPathandneighbors函数构建查询,但它似乎没有提取我正在寻找的结果。
任何建议/指针将不胜感激。
提前致谢 !
更新-1
我的数据沿袭背景:我的应用程序从外部应用程序 (PRODUCE) 获取文件。我知道哪些数据库表/外部应用程序的列填充了文件中的字段。所以在这里,PRODUCER 将是我的Source节点;外部应用程序(填充文件)的每个 table.column 是一个Field节点,PRODUCERSource节点将CONTAINS与所有Field节点(代表填充文件的外部应用程序数据库表的 table.column)有关系。
来自外部应用程序的文件称为 INBOUND。它是一个逗号分隔的文件。我知道文件中的字段名称和顺序是什么。所以在这里,INBOUND 将是我的Source节点;文件中的每个字段都是一个Field节点,而 INBOUNDSource节点将CONTAINS与所有Field节点有关系(代表入站文件中的文件字段)。此外Field,INBOUND 的每个节点都将与PRODUCER 的一个节点Source有MAPS_TO关系(一对一映射)。FieldSource
继续进行类似的工作流程,我的下一个阶段称为 STAGING,其中我将入站文件字段加载到我的数据库表/列中。所以在这里,STAGING 将是我的Source节点,数据库表的每一列(我将文件字段加载到其中)将代表一个Field节点。FieldSTAGING Source 节点将与所有节点(代表我将文件字段加载到其中的 db 表的 db table.column)具有 CONTAINS 关系。FieldSTAGING的每个节点也将与 INBOUND 的节点Source有MAPS_TO关系(一对一映射)。FieldSource
类似的,我的下一个阶段是中级。在这个阶段,我正在查询加载输入文件字段的表,然后将输出刷新到另一个文件中(根据我的业务用例,我可能选择查询所有或仅查询表列的子集从输入文件填充)。我知道哪些字段以及以什么顺序进入我的中间文件。所以在这里,中间是我的Source节点,进入中间文件的每个字段都代表我的Field节点。INTERMEDIATE也将与代表中间文件中的字段的所有节点Source有CONTAINS关系。Field此外,这些Field节点中的每一个都将MAPS_TO与 STAGING Source 的字段(一对一映射)有关系。
同样,我有 OUTBOUND 阶段,最后是 CONSUMER 阶段。
...(我希望你现在能够形象化血统)
例如,我的查询目标是,如果我给出一个Field名称(代表 PRODUCER 的 table.column)作为输入,那么我应该能够追踪它的沿袭直到 CONSUMER(即,我的沿袭的最后阶段)。