使用Springboot/Kotlin检索Neo4j DB的DB Schema可视化

0sgqnhkj  于 2023-06-05  发布在  Spring
关注(0)|答案(1)|浏览(270)

我想使用Springboot和Kotlin检索“call db.schema.visualization()”查询。
使用我编写的代码,我能够获取模式的所有数据,但只能在调用查询方法后从响应中写入键。请看一下代码并帮助我。
存储库:

@Repository
interface CustomMovieRepository {
    fun schemaVisualization(): Optional<MutableMap<String, Any>>
}

RepositoryImpl:

class CustomMovieRepositoryImpl(@Autowired val client: Neo4jClient) : CustomMovieRepository {

    override fun schemaVisualization(): Optional<MutableMap<String, Any>> {
        val all = client.query("call db.schema.visualization()").fetch().one()
        return all

    }
}

控制器方法:

@GetMapping("/schema-visualization", produces = [MediaType.APPLICATION_JSON_VALUE])
    fun SchemaVisualization(): Optional<MutableMap<String, Any>> {
        return movieRepository.schemaVisualization()
    }

这是我得到的输出:


]

laawzig2

laawzig21#

这看起来是正确的。但是您看不到里面的内容,因为没有属性的访问器。如果你想知道更多的细节,你必须手动Map它。这里有一个小例子可以让你开始。

record Visualization(Collection<VisualizationNode> nodes, Collection<VisualizationRelationship> relationships) {}
record VisualizationNode(Iterable<String> labels, Map<String, Object> properties) {}
record VisualizationRelationship(String type, Map<String, Object> properties) {}

@Test
void testing(@Autowired Neo4jClient client, @Autowired Driver driver) {
    try (var session = driver.session()) {
        session.run("CREATE (u:User)-[:KNOWS]->(:Language)").consume();
    }
    var one = client.query("CALL db.schema.visualization()")
            .fetchAs(Visualization.class)
            .mappedBy((typeSystem, record) -> {
                var nodes = record.get("nodes").asList(Value::asNode)
                        .stream().map(node -> new VisualizationNode(node.labels(), node.asMap())).toList();
                var relationships = record.get("relationships").asList(Value::asRelationship)
                        .stream().map(relationship -> new VisualizationRelationship(relationship.type(), relationship.asMap())).toList();
                return new Visualization(nodes, relationships);
            })
            .one();
    System.out.println(one.get());
}

将输出

Visualization[
nodes=[VisualizationNode[labels=[User], properties={name=User, indexes=[], constraints=[]}], VisualizationNode[labels=[Language], properties={name=Language, indexes=[], constraints=[]}],...]],
relationships=[VisualizationRelationship[type=KNOWS, properties={}]]
]

编辑:还有一个非官方的实验性插件,你可能想检查一下:https://github.com/neo4j/graph-schema-introspector关于此的一些背景:https://medium.com/neo4j/describing-a-property-graph-data-model-d203c389546e

相关问题